diff --git a/lib/plugins/filter/post_permalink.js b/lib/plugins/filter/post_permalink.js index 12b57ea207..622b18b6fc 100644 --- a/lib/plugins/filter/post_permalink.js +++ b/lib/plugins/filter/post_permalink.js @@ -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; @@ -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 @@ -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); } diff --git a/lib/plugins/processor/post.js b/lib/plugins/processor/post.js index fd0721668f..e8eb7a64f0 100644 --- a/lib/plugins/processor/post.js +++ b/lib/plugins/processor/post.js @@ -122,6 +122,7 @@ module.exports = ctx => { if (data.permalink) { data.slug = data.permalink; + data.__permalink = data.permalink; delete data.permalink; } diff --git a/test/scripts/filters/post_permalink.js b/test/scripts/filters/post_permalink.js index 9b82ca8952..2f582498b6 100644 --- a/test/scripts/filters/post_permalink.js +++ b/test/scripts/filters/post_permalink.js @@ -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))); + }); }); diff --git a/test/scripts/processors/post.js b/test/scripts/processors/post.js index ebdf173097..aa2ac999fc 100644 --- a/test/scripts/processors/post.js +++ b/test/scripts/processors/post.js @@ -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);