diff --git a/lib/plugins/tag/asset_link.js b/lib/plugins/tag/asset_link.js index 8cf335c30b..fc631fa35d 100644 --- a/lib/plugins/tag/asset_link.js +++ b/lib/plugins/tag/asset_link.js @@ -1,12 +1,13 @@ 'use strict'; const url = require('url'); +const { escapeHTML } = require('hexo-util'); /** * Asset link tag * * Syntax: - * {% asset_link slug [title] %} + * {% asset_link slug [title] [escape] %} */ module.exports = ctx => { const PostAsset = ctx.model('PostAsset'); @@ -18,8 +19,17 @@ module.exports = ctx => { const asset = PostAsset.findOne({post: this._id, slug}); if (!asset) return; - const title = args.length ? args.join(' ') : asset.slug; + let escape = args[args.length - 1]; + if (escape === 'true' || escape === 'false') { + args.pop(); + } else { + escape = 'true'; + } - return `${title}`; + let title = args.length ? args.join(' ') : asset.slug; + const attrTitle = escapeHTML(title); + if (escape === 'true') title = attrTitle; + + return `${title}`; }; }; diff --git a/lib/plugins/tag/post_link.js b/lib/plugins/tag/post_link.js index 87cf248ceb..5ba86e62b3 100644 --- a/lib/plugins/tag/post_link.js +++ b/lib/plugins/tag/post_link.js @@ -1,10 +1,12 @@ 'use strict'; +const { escapeHTML } = require('hexo-util'); + /** * Post link tag * * Syntax: - * {% post_link slug [title] %} + * {% post_link slug [title] [escape] %} */ module.exports = ctx => { const Post = ctx.model('Post'); @@ -13,11 +15,20 @@ module.exports = ctx => { const slug = args.shift(); if (!slug) return; + let escape = args[args.length - 1]; + if (escape === 'true' || escape === 'false') { + args.pop(); + } else { + escape = 'true'; + } + const post = Post.findOne({slug}); if (!post) return; - const title = args.length ? args.join(' ') : post.title; + let title = args.length ? args.join(' ') : post.title; + const attrTitle = escapeHTML(title); + if (escape === 'true') title = attrTitle; - return `${title}`; + return `${title}`; }; }; diff --git a/test/scripts/tags/asset_link.js b/test/scripts/tags/asset_link.js index d95697b724..4531bcd9b4 100644 --- a/test/scripts/tags/asset_link.js +++ b/test/scripts/tags/asset_link.js @@ -44,6 +44,18 @@ describe('asset_link', () => { assetLink('bar Hello world').should.eql('Hello world'); }); + it('should escape tag in title by default', () => { + assetLink('bar "Hello" ').should.eql('"Hello" <world>'); + }); + + it('should escape tag in title', () => { + assetLink('bar "Hello" true').should.eql('"Hello" <world>'); + }); + + it('should not escape tag in title', () => { + assetLink('bar "Hello" world false').should.eql('"Hello" world'); + }); + it('with space', () => { // {% asset_link "spaced asset" "spaced title" %} assetLinkTag.call(post, ['spaced asset', 'spaced title']) diff --git a/test/scripts/tags/post_link.js b/test/scripts/tags/post_link.js index af7db1f435..b2b6f6dd42 100644 --- a/test/scripts/tags/post_link.js +++ b/test/scripts/tags/post_link.js @@ -8,11 +8,16 @@ describe('post_link', () => { hexo.config.permalink = ':title/'; - before(() => hexo.init().then(() => Post.insert({ + before(() => hexo.init().then(() => Post.insert([{ source: 'foo', slug: 'foo', title: 'Hello world' - }))); + }, + { + source: 'title-with-tag', + slug: 'title-with-tag', + title: '"Hello" !' + }]))); it('default', () => { postLink(['foo']).should.eql('Hello world'); @@ -22,6 +27,27 @@ describe('post_link', () => { postLink(['foo', 'test']).should.eql('test'); }); + it('should escape tag in title by default', () => { + postLink(['title-with-tag']).should.eql('"Hello" <new world>!'); + }); + + it('should escape tag in title', () => { + postLink(['title-with-tag', 'true']).should.eql('"Hello" <new world>!'); + }); + + it('should escape tag in custom title', () => { + postLink(['title-with-tag', '', 'title', 'true']).should.eql('<test> title'); + }); + + it('should not escape tag in title', () => { + postLink(['title-with-tag', 'false']).should.eql('"Hello" !'); + }); + + it('should not escape tag in custom title', () => { + postLink(['title-with-tag', 'This is a Bold "statement"', 'false']) + .should.eql('This is a Bold "statement"'); + }); + it('no slug', () => { should.not.exist(postLink([])); });