Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#3464): override permalink use the front-matter #4359

Merged
merged 1 commit into from Jun 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 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;
}
Comment on lines +11 to +14
Copy link
Member Author

@SukkaW SukkaW Jun 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a __permalink is detected (which is added by post processor from permalink inside the front-matter), it will be return directly.

BREAKING CHANGES:

permalink inside the front-matter is no longer controlled by hexo.config.permalink.


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


return permalink.stringify(meta);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/processor/post.js
Expand Up @@ -121,7 +121,7 @@ module.exports = ctx => {
}

if (data.permalink) {
data.slug = data.permalink;
data.__permalink = data.permalink;
Copy link
Member Author

@SukkaW SukkaW Jun 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BREAKING CHANGES.

data.slug won't be override by data.permalink. That means data.slug will still come from the original path relative to source/_posts.

Also, a new __permalink is created to avoid overriding Post.virtual('permalink').get.

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)));
});
});
3 changes: 2 additions & 1 deletion test/scripts/processors/post.js
Expand Up @@ -979,7 +979,8 @@ 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