-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from dailyrandomphoto/custom-function
- Loading branch information
Showing
20 changed files
with
416 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict'; | ||
|
||
const cuid = require('cuid'); | ||
|
||
module.exports = function(option) { | ||
return () => cuid.slug(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict'; | ||
|
||
const cuid = require('cuid'); | ||
|
||
module.exports = function(option) { | ||
return cuid; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict'; | ||
|
||
const store = {}; | ||
|
||
function register(type, fn) { | ||
if (typeof fn !== 'function') throw new TypeError('fn must be a function'); | ||
if (typeof store[type] === 'function') throw new TypeError('already registered as type: ' + type); | ||
store[type] = fn; | ||
} | ||
|
||
function get(type) { | ||
return store[type] || defaultFn(type); | ||
} | ||
|
||
function defaultFn(type) { | ||
return function() { | ||
throw new TypeError('can\'t find generator with type: ' + type); | ||
}; | ||
} | ||
|
||
module.exports = { | ||
register, | ||
get | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict'; | ||
|
||
const generators = require('./id-generators'); | ||
const cuid = require('./cuid'); | ||
|
||
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')); | ||
|
||
module.exports = generators; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict'; | ||
|
||
/** | ||
* https://github.com/ai/nanoid | ||
*/ | ||
const generate = require('nanoid/generate'); | ||
const alphabet = 'abcdefghijklmnopqrstuvwxyz'; | ||
|
||
module.exports = function(option) { | ||
let size = option.size || 26; | ||
return () => generate(alphabet, size); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict'; | ||
|
||
/** | ||
* https://github.com/ai/nanoid | ||
*/ | ||
const generate = require('nanoid/generate'); | ||
const alphabet = '0123456789abcdefghijklmnopqrstuvwxyz'; | ||
|
||
module.exports = function(option) { | ||
let size = option.size || 24; | ||
return () => generate(alphabet, size); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
/** | ||
* https://github.com/ai/nanoid | ||
*/ | ||
const nanoid = require('nanoid'); | ||
|
||
module.exports = function(option) { | ||
let size = option.size || 21; | ||
return () => nanoid(size); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use strict'; | ||
|
||
module.exports = function(option) { | ||
option = option || {}; | ||
let start = typeof option.start !== 'undefined' ? option.start : 1; | ||
let prefix = typeof option.prefix !== 'undefined' ? option.prefix : 'prefix-'; | ||
return () => prefix + start++; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict'; | ||
|
||
module.exports = function(option) { | ||
option = option || {}; | ||
let start = typeof option.start !== 'undefined' ? option.start : 1; | ||
return () => '' + start++; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,36 @@ | ||
'use strict'; | ||
const generators = require('./id-generators'); | ||
|
||
(function(ctx) { | ||
const { config, extend } = ctx; | ||
const { console, filter } = extend; | ||
const myconfig = Object.assign({ | ||
auto: false, | ||
title_default: ' ' | ||
}, config.unique_post_path); | ||
if (typeof hexo !== 'undefined') { | ||
(function(ctx) { | ||
const { config, extend } = ctx; | ||
const { console, filter } = extend; | ||
const myconfig = Object.assign({ | ||
auto: false, | ||
title_default: ' ', | ||
path_type: 'default' | ||
}, config.unique_post_path); | ||
|
||
console.register('new2', 'Create a new post with a auto generated unique path.', { | ||
usage: '[layout] [title]', | ||
arguments: [ | ||
{name: 'layout', desc: 'Post layout. Use post, page, draft or whatever you want.'}, | ||
{name: 'title', desc: 'Post title. Wrap it with quotations to escape.'} | ||
], | ||
options: [ | ||
{name: '-r, --replace', desc: 'Replace the current post if existed.'}, | ||
{name: '-s, --slug', desc: 'Post slug. Customize the URL of the post.'} | ||
] | ||
}, require('./new2')(myconfig)); | ||
console.register('new2', 'Create a new post with a auto generated unique path.', { | ||
usage: '[layout] [title]', | ||
arguments: [ | ||
{name: 'layout', desc: 'Post layout. Use post, page, draft or whatever you want.'}, | ||
{name: 'title', desc: 'Post title. Wrap it with quotations to escape.'} | ||
], | ||
options: [ | ||
{name: '-r, --replace', desc: 'Replace the current post if existed.'}, | ||
{name: '-s, --slug', desc: 'Post slug. Customize the URL of the post.'} | ||
] | ||
}, require('./new2')(myconfig)); | ||
|
||
if (myconfig.auto) { | ||
// register a filter with highest priority. | ||
filter.register('new_post_path', require('./unique_post_path_filter'), 1); | ||
} | ||
if (myconfig.auto) { | ||
// register a filter with highest priority. | ||
filter.register('new_post_path', require('./unique_post_path_filter')(myconfig), 1); | ||
} | ||
|
||
}(hexo)); | ||
}(hexo)); | ||
} | ||
|
||
module.exports = { | ||
register: generators.register | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
'use strict'; | ||
|
||
const cuid = require('cuid'); | ||
const generators = require('./id-generators'); | ||
|
||
module.exports = function(data, replace) { | ||
// set path value as a value of cuid. | ||
data.path = cuid(); | ||
return data; | ||
module.exports = function(config) { | ||
return function(data, replace) { | ||
const generator = generators.get(config.path_type || 'default'); | ||
const generate = generator(config); | ||
// generate a unique path. | ||
if (data.layout !== 'page') { | ||
data.slug = generate(data.title); | ||
} | ||
return data; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"extends": "hexo/test", | ||
"env": { | ||
"es6": true | ||
}, | ||
"globals": { | ||
"should": true | ||
}, | ||
"rules": { | ||
"node/no-unpublished-require": ["error", { | ||
"allowModules": ["cheerio", "hexo"] | ||
}] | ||
} | ||
} |
Oops, something went wrong.