Skip to content

Commit

Permalink
Merge pull request #6 from dailyrandomphoto/use-id-generators
Browse files Browse the repository at this point in the history
use id-generators module
  • Loading branch information
dailyrandomphoto authored Oct 31, 2019
2 parents a935091 + f5b137e commit ba750e7
Show file tree
Hide file tree
Showing 19 changed files with 37 additions and 188 deletions.
62 changes: 2 additions & 60 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,62 +1,4 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# next.js build output
.next
package-lock.json
yarn.lock
.nyc_output/
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
10 changes: 2 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
language: node_js

sudo: false

cache:
apt: true
directories:
- node_modules

node_js:
- "8"
- "10"
- "node"
- "12"
- "13"

script:
- npm run eslint
Expand Down
7 changes: 0 additions & 7 deletions lib/id-generators/cuid-slug.js

This file was deleted.

7 changes: 0 additions & 7 deletions lib/id-generators/cuid.js

This file was deleted.

8 changes: 4 additions & 4 deletions lib/id-generators/date-seq.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const prefixSeq = require('./prefix-seq');

module.exports = function(option) {
option = option || {};
// date prefix sample:
// Date prefix sample:
// YYYYMMDD (default)
// YYYY-MM-DD-
// YYMMDD-
Expand All @@ -13,9 +13,9 @@ module.exports = function(option) {
const pattern = typeof option.prefix === 'string' ? option.prefix : 'YYYYMMDD';
const date = new Date();
const prefix = pattern.replace(/YYYY/, date.getFullYear())
.replace(/YY/, ('' + date.getFullYear()).substring(2))
.replace(/MM/, ('0' + (date.getMonth() + 1)).substr(-2))
.replace(/DD/, ('0' + date.getDate()).substr(-2));
.replace(/YY/, String(date.getFullYear()).slice(2))
.replace(/MM/, ('0' + (date.getMonth() + 1)).slice(-2))
.replace(/DD/, ('0' + date.getDate()).slice(-2));
option.prefix = prefix;
option.size = parseInt(option.size, 10) || 2;
return prefixSeq(option);
Expand Down
24 changes: 0 additions & 24 deletions lib/id-generators/id-generators.js

This file was deleted.

9 changes: 1 addition & 8 deletions lib/id-generators/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
'use strict';

const generators = require('./id-generators');
const cuid = require('./cuid');
const generators = require('id-generators');

generators.register('default', cuid);
generators.register('cuid', cuid);
generators.register('cuid-slug', require('./cuid-slug'));
generators.register('nanoid', require('./nanoid'));
generators.register('nanoid-simple', require('./nanoid-simple'));
generators.register('nanoid-lowercase', require('./nanoid-lowercase'));
generators.register('seq', require('./seq'));
generators.register('prefix-seq', require('./prefix-seq'));
generators.register('date-seq', require('./date-seq'));
Expand Down
12 changes: 0 additions & 12 deletions lib/id-generators/nanoid-lowercase.js

This file was deleted.

12 changes: 0 additions & 12 deletions lib/id-generators/nanoid-simple.js

This file was deleted.

11 changes: 0 additions & 11 deletions lib/id-generators/nanoid.js

This file was deleted.

16 changes: 8 additions & 8 deletions lib/id-generators/prefix-seq.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
const {join, basename} = require('path');
const chalk = require('chalk');
const { join, basename } = require('path');
const { listDirSync } = require('hexo-fs');
const {listDirSync} = require('hexo-fs');
module.exports = function(option) {
option = option || {};
const log = option.context ? option.context.log : console;
Expand All @@ -16,11 +16,11 @@ module.exports = function(option) {

return function() {
let files = listDirSync(postDir).concat(listDirSync(draftDir));
files = files.map((file) => basename(file))
.filter((file) => pattern.test(file))
.map((file) => parseInt(file.replace(prefix, '').replace('.md', ''), 10))
.sort(function(a, b) { return b - a; });
let max = Math.max((files[0] || 0) + 1, start);
return prefix + ('' + max).padStart(size, '0');
files = files.map(file => basename(file))
.filter(file => pattern.test(file))
.map(file => parseInt(file.replace(prefix, '').replace('.md', ''), 10))
.sort((a, b) => b - a);
const max = Math.max((files[0] || 0) + 1, start);
return prefix + String(max).padStart(size, '0');
};
};
8 changes: 4 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
const generators = require('./id-generators');

const {register} = require('./id-generators');

if (typeof hexo !== 'undefined') {
(function(ctx) {
Expand All @@ -25,13 +26,12 @@ if (typeof hexo !== 'undefined') {
}, require('./new2')(myconfig));

if (myconfig.auto) {
// register a filter with highest priority.
// Register a filter with highest priority.
filter.register('new_post_path', require('./unique_post_path_filter')(myconfig), 1);
}

}(hexo));
}

module.exports = {
register: generators.register
register
};
4 changes: 2 additions & 2 deletions lib/new2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const tildify = require('tildify');
const chalk = require('chalk');
const generators = require('./id-generators');
const {get} = require('id-generators');

const reservedKeys = {
_: true,
Expand All @@ -20,7 +20,7 @@ const reservedKeys = {

module.exports = function(config) {
return function newConsole(args) {
const generator = generators.get(config.path_type || 'default');
const generator = get(config.path_type || 'default');
const generate = generator(config);
// Display help message if user didn't input any arguments
// if (!args._.length) {
Expand Down
7 changes: 4 additions & 3 deletions lib/unique_post_path_filter.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
'use strict';

const generators = require('./id-generators');
const {get} = require('id-generators');

module.exports = function(config) {
return function(data, replace) {
const generator = generators.get(config.path_type || 'default');
const generator = get(config.path_type || 'default');
const generate = generator(config);
// generate a unique path.
// Generate a unique path.
if (data.layout !== 'page') {
data.slug = generate(data.title);
}

return data;
};
};
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hexo-unique-post-path",
"version": "0.0.3",
"version": "1.0.0",
"description": "This plug-in helps Hexo create new posts with unique auto-generated paths.",
"main": "lib/index.js",
"scripts": {
Expand All @@ -10,9 +10,8 @@
},
"dependencies": {
"chalk": "^2.4.2",
"cuid": "^2.1.6",
"hexo-fs": "^2.0.0",
"nanoid": "^2.1.6",
"id-generators": "^1.0.0",
"tildify": "^2.0.0"
},
"devDependencies": {
Expand Down
5 changes: 1 addition & 4 deletions test/id-generators/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
const generators = require('../../lib/id-generators');

describe('id-generators', () => {

// before(() => hexo.init());

it('should return a function', () => {
const generator = generators.get('cuid');
const generator = generators.get();
generator.should.be.a('function');
const gen = generator({});
gen.should.be.a('function');
Expand Down
15 changes: 6 additions & 9 deletions test/register.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
'use strict';


const unique_post_path_filter = require('../lib/unique_post_path_filter');
const { register } = require('../');
const {register} = require('..');

describe('register', () => {

it('should use the custom function to generate a value of path', () => {
register('my_path_gen', function(option) {
return (title) => title.toLowerCase().replace(/[^\w]/g, '');
register('my_path_gen', option => {
return title => title.toLowerCase().replace(/[^\w]/g, '');
});

const config = {path_type: 'my_path_gen'};
Expand All @@ -18,9 +16,9 @@ describe('register', () => {
});

it('should use option in the custom function', () => {
register('my_custom_path', function(option) {
let size = option.size || 8;
let prefix = option.prefix || 'items-';
register('my_custom_path', option => {
const size = option.size || 8;
const prefix = option.prefix || 'items-';
return function(title) {
return prefix + title.toLowerCase().replace(/[^\w]/g, '').substring(0, size);
};
Expand All @@ -31,5 +29,4 @@ describe('register', () => {
unique_post_path_filter(config)(data);
data.slug.should.eql('items-hellow');
});

});
2 changes: 0 additions & 2 deletions test/unique_post_path_filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const unique_post_path_filter = require('../lib/unique_post_path_filter');

describe('unique_post_path_filter', () => {

it('should set path: path_type = undefined', () => {
const config = {};
const data = {title: 'title'};
Expand All @@ -24,5 +23,4 @@ describe('unique_post_path_filter', () => {
unique_post_path_filter(config)(data);
data.slug.should.be.a('string');
});

});

0 comments on commit ba750e7

Please sign in to comment.