Skip to content

Commit

Permalink
use --type option on 'hexo new' command
Browse files Browse the repository at this point in the history
  • Loading branch information
dailyrandomphoto committed Nov 5, 2019
1 parent 6ea32b0 commit 3c74e3a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 20 deletions.
6 changes: 2 additions & 4 deletions lib/index.js
Expand Up @@ -43,10 +43,8 @@ function init(hexo) {
]
}, require('./new2')(moduleConfig));

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

module.exports = {
Expand Down
13 changes: 8 additions & 5 deletions lib/unique_post_path_filter.js
Expand Up @@ -4,11 +4,14 @@ const {get} = require('id-generators');

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

return data;
Expand Down
8 changes: 4 additions & 4 deletions test/register.js
@@ -1,15 +1,15 @@
'use strict';

const unique_post_path_filter = require('../lib/unique_post_path_filter');
const {register} = require('..');
const unique_post_path_filter = require('hexo-unique-post-path/lib/unique_post_path_filter');
const {register} = require('hexo-unique-post-path');

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

const config = {path_type: 'my_path_gen'};
const config = {auto: true, path_type: 'my_path_gen'};
const data = {title: 'Hello World!'};
unique_post_path_filter(config)(data);
data.slug.should.eql('helloworld');
Expand All @@ -24,7 +24,7 @@ describe('register', () => {
};
});

const config = {path_type: 'my_custom_path', size: 6};
const config = {auto: true, path_type: 'my_custom_path', size: 6};
const data = {title: 'Hello World!'};
unique_post_path_filter(config)(data);
data.slug.should.eql('items-hellow');
Expand Down
50 changes: 43 additions & 7 deletions test/unique_post_path_filter.js
@@ -1,26 +1,62 @@
'use strict';

const unique_post_path_filter = require('../lib/unique_post_path_filter');
const {expect} = require('chai');
require('hexo-unique-post-path/lib/id-generators/index.js');
const unique_post_path_filter = require('hexo-unique-post-path/lib/unique_post_path_filter.js');

describe('unique_post_path_filter', () => {
it('should set path: path_type = undefined', () => {
it('should not set slug if config.auto is undefined', () => {
const config = {};
const data = {title: 'title'};
unique_post_path_filter(config)(data);
expect(data.slug).to.be.an('undefined');
});

it('should not set slug if config.auto is false', () => {
const config = {auto: false};
const data = {title: 'title'};
unique_post_path_filter(config)(data);
expect(data.slug).to.be.an('undefined');
});

it('should set slug: path_type = undefined', () => {
const config = {auto: true};
const data = {title: 'title'};
unique_post_path_filter(config)(data);
data.slug.should.be.a('string');
});

it('should set path: path_type = cuid', () => {
const config = {path_type: 'cuid'};
it('should set slug: path_type = cuid', () => {
const config = {auto: true, path_type: 'cuid'};
const data = {title: 'title'};
unique_post_path_filter(config)(data);
data.slug.should.be.a('string');
});

it('should set path: path_type = nanoid', () => {
const config = {path_type: 'nanoid'};
it('should set slug: path_type = nanoid', () => {
const config = {auto: true, path_type: 'nanoid'};
const data = {title: 'title'};
unique_post_path_filter(config)(data);
data.slug.should.be.a('string');
});

it('should set slug: path_type = seq', () => {
const config = {auto: true, path_type: 'seq'};
const data = {title: 'title'};
unique_post_path_filter(config)(data);
data.slug.should.be.a('string').eql('19');
});

it('should set slug: --type = seq', () => {
const config = {};
const data = {title: 'title', type: 'seq'};
unique_post_path_filter(config)(data);
data.slug.should.be.a('string').eql('19');
});

it('should set slug: --type = seq', () => {
const config = {auto: true, path_type: 'nanoid'};
const data = {title: 'title', type: 'seq'};
unique_post_path_filter(config)(data);
data.slug.should.be.a('string').eql('19');
});
});

0 comments on commit 3c74e3a

Please sign in to comment.