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: Failed to create post with special character title #5149

Merged
merged 1 commit into from
Feb 6, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/hexo/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '\\"')}"`;
}
}

Expand Down
84 changes: 84 additions & 0 deletions test/scripts/console/new.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});