Skip to content

Commit

Permalink
fix(hexojs#3464): override permalink use the front-matter
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Jun 16, 2020
1 parent edef5c2 commit 0c550d9
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
19 changes: 15 additions & 4 deletions lib/plugins/filter/post_permalink.js
Expand Up @@ -6,7 +6,13 @@ let permalink;

function postPermalinkFilter(data) {
const { config } = this;
const { id, _id, slug, title, date } = data;
const { id, _id, slug, title, date, __permalink } = data;

if (__permalink) {
if (!__permalink.startsWith('/')) return `/${__permalink}`;
return __permalink;
}

const hash = slug && date
? createSha1Hash().update(slug + date.unix().toString()).digest('hex').slice(0, 12)
: null;
Expand Down Expand Up @@ -40,7 +46,9 @@ function postPermalinkFilter(data) {

const keys = Object.keys(data);

for (const key of keys) {
for (let i = 0, len = keys.length; i < len; i++) {
const key = keys[i];

if (Object.prototype.hasOwnProperty.call(meta, key)) continue;

// Use Object.getOwnPropertyDescriptor to copy getters to avoid "Maximum call
Expand All @@ -51,14 +59,17 @@ function postPermalinkFilter(data) {
if (config.permalink_defaults) {
const keys2 = Object.keys(config.permalink_defaults);

for (const key of keys2) {
for (let i = 0, len = keys2.length; i < len; i++) {
const key = keys2[i];

if (Object.prototype.hasOwnProperty.call(meta, key)) continue;

meta[key] = config.permalink_defaults[key];
}
}


// console.log(meta);
// console.log(permalink.stringify(meta));
return permalink.stringify(meta);
}

Expand Down
1 change: 1 addition & 0 deletions lib/plugins/processor/post.js
Expand Up @@ -122,6 +122,7 @@ module.exports = ctx => {

if (data.permalink) {
data.slug = data.permalink;
data.__permalink = data.permalink;
delete data.permalink;
}

Expand Down
23 changes: 23 additions & 0 deletions test/scripts/filters/post_permalink.js
Expand Up @@ -148,4 +148,27 @@ describe('post_permalink', () => {

await Promise.all(posts.map(post => Post.removeById(post._id)));
});

it('permalink - should override everything', async () => {
hexo.config.permalink = ':year/:month/:day/:title/';

const posts = await Post.insert([{
source: 'my-new-post.md',
slug: 'hexo/permalink-test',
__permalink: 'hexo/permalink-test',
title: 'Permalink Test',
date: moment('2014-01-02')
}, {
source: 'another-new-post.md',
slug: '/hexo-hexo/permalink-test-2',
__permalink: 'hexo-hexo/permalink-test-2',
title: 'Permalink Test',
date: moment('2014-01-02')
}]);

postPermalink(posts[0]).should.eql('/hexo/permalink-test');
postPermalink(posts[1]).should.eql('/hexo-hexo/permalink-test-2');

await Promise.all(posts.map(post => Post.removeById(post._id)));
});
});
2 changes: 2 additions & 0 deletions test/scripts/processors/post.js
Expand Up @@ -979,7 +979,9 @@ describe('post', () => {
await writeFile(file.source, body);
await process(file);
const post = Post.findOne({source: file.path});

post.slug.should.eql('foooo');
post.__permalink.should.eql('foooo');

post.remove();
unlink(file.source);
Expand Down

0 comments on commit 0c550d9

Please sign in to comment.