From b64a7f3dbef72a2f7dd364dd737ab364f35b7fd9 Mon Sep 17 00:00:00 2001 From: D-Sketon <2055272094@qq.com> Date: Thu, 19 Jan 2023 21:07:31 +0800 Subject: [PATCH] fix: Failed to create post with special character title --- lib/hexo/post.js | 4 +- test/scripts/console/new.js | 84 +++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) diff --git a/lib/hexo/post.js b/lib/hexo/post.js index 2b91d56187..2df362d2fa 100644 --- a/lib/hexo/post.js +++ b/lib/hexo/post.js @@ -191,7 +191,9 @@ const prepareFrontMatter = (data, jsonMode) => { } else if (moment.isDate(item)) { data[key] = moment.utc(item).format('YYYY-MM-DD HH:mm:ss'); } else if (typeof item === 'string') { - if (jsonMode || item.includes(':') || item.startsWith('#') || item.startsWith('!!')) data[key] = `"${item}"`; + if (jsonMode || item.includes(':') || item.startsWith('#') || item.startsWith('!!') + || item.includes('{') || item.includes('}') || item.includes('[') || item.includes(']') + || item.includes('\'') || item.includes('"')) data[key] = `"${item.replace(/"/g, '\\"')}"`; } } diff --git a/test/scripts/console/new.js b/test/scripts/console/new.js index e578a2d047..8e64429ae0 100644 --- a/test/scripts/console/new.js +++ b/test/scripts/console/new.js @@ -247,4 +247,88 @@ describe('new', () => { await unlink(path); }); + + it('special character - 1', async () => { + const date = moment(now); + const path = join(hexo.source_dir, '_posts', 'Hello-World.md'); + const body = [ + 'title: \'[Hello] World\'', + 'foo: bar', + 'date: ' + date.format('YYYY-MM-DD HH:mm:ss'), + 'tags:', + '---' + ].join('\n') + '\n'; + + await n({ + _: ['[Hello] World'], + foo: 'bar' + }); + const content = await readFile(path); + content.should.eql(body); + + await unlink(path); + }); + + it('special character - 2', async () => { + const date = moment(now); + const path = join(hexo.source_dir, '_posts', 'Hello-World.md'); + const body = [ + 'title: \'{Hello} World\'', + 'foo: bar', + 'date: ' + date.format('YYYY-MM-DD HH:mm:ss'), + 'tags:', + '---' + ].join('\n') + '\n'; + + await n({ + _: ['{Hello} World'], + foo: 'bar' + }); + const content = await readFile(path); + content.should.eql(body); + + await unlink(path); + }); + + it('special character - 3', async () => { + const date = moment(now); + const path = join(hexo.source_dir, '_posts', 'Hello-World.md'); + const body = [ + 'title: \'\'\'Hello\'\' World\'', + 'foo: bar', + 'date: ' + date.format('YYYY-MM-DD HH:mm:ss'), + 'tags:', + '---' + ].join('\n') + '\n'; + + await n({ + _: ['\'Hello\' World'], + foo: 'bar' + }); + const content = await readFile(path); + content.should.eql(body); + + await unlink(path); + }); + + it('special character - 4', async () => { + const date = moment(now); + const path = join(hexo.source_dir, '_posts', 'Hello-World.md'); + const body = [ + 'title: \'"Hello" World\'', + 'foo: bar', + 'date: ' + date.format('YYYY-MM-DD HH:mm:ss'), + 'tags:', + '---' + ].join('\n') + '\n'; + + await n({ + _: ['"Hello" World'], + foo: 'bar' + }); + const content = await readFile(path); + content.should.eql(body); + + await unlink(path); + }); });