From 6d880a263d2ce066d8c2e8b1fc39bb3c542b48df Mon Sep 17 00:00:00 2001 From: Uiolee <22849383+uiolee@users.noreply.github.com> Date: Fri, 17 May 2024 17:07:16 +0800 Subject: [PATCH] feat(renderScaffold): deepMerge frontMatter of post and scaffold (#5472) * feat(renderScaffold): deepMerge frontMatter of post and scaffold * test: add test case --- lib/hexo/post.ts | 10 +++------- test/scripts/hexo/post.ts | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/lib/hexo/post.ts b/lib/hexo/post.ts index 8e443b0283..2efeae4dca 100644 --- a/lib/hexo/post.ts +++ b/lib/hexo/post.ts @@ -4,7 +4,7 @@ import Promise from 'bluebird'; import { join, extname, basename } from 'path'; import { magenta } from 'picocolors'; import { load } from 'js-yaml'; -import { slugize, escapeRegExp } from 'hexo-util'; +import { slugize, escapeRegExp, deepMerge} from 'hexo-util'; import { copyDir, exists, listDir, mkdirs, readFile, rmdir, unlink, writeFile } from 'hexo-fs'; import { parse as yfmParse, split as yfmSplit, stringify as yfmStringify } from 'hexo-front-matter'; import type Hexo from './index'; @@ -306,13 +306,9 @@ class Post { const jsonMode = separator.startsWith(';'); // Parse front-matter - const obj = jsonMode ? JSON.parse(`{${frontMatter}}`) : load(frontMatter); + let obj = jsonMode ? JSON.parse(`{${frontMatter}}`) : load(frontMatter); - Object.keys(data) - .filter(key => !preservedKeys.includes(key) && obj[key] == null) - .forEach(key => { - obj[key] = data[key]; - }); + obj = deepMerge(obj, Object.fromEntries(Object.entries(data).filter(([key, value]) => !preservedKeys.includes(key) && value != null))); let content = ''; // Prepend the separator diff --git a/test/scripts/hexo/post.ts b/test/scripts/hexo/post.ts index b01e41d61f..79a4328ce7 100644 --- a/test/scripts/hexo/post.ts +++ b/test/scripts/hexo/post.ts @@ -4,7 +4,7 @@ import { readFile, mkdirs, unlink, rmdir, writeFile, exists, stat, listDir } fro import { spy, useFakeTimers } from 'sinon'; import { parse as yfm } from 'hexo-front-matter'; import { expected, content, expected_disable_nunjucks, content_for_issue_3346, expected_for_issue_3346, content_for_issue_4460 } from '../../fixtures/post_render'; -import { highlight } from 'hexo-util'; +import { highlight, deepMerge } from 'hexo-util'; import Hexo from '../../../lib/hexo'; import chai from 'chai'; const should = chai.should(); @@ -650,6 +650,43 @@ describe('Post', () => { await unlink(data.path); }); + // https:// github.com/hexojs/hexo/issues/5155 + it('publish() - merge front-matter', async () => { + const prefixTags = ['prefixTag1', 'fooo']; + const customTags = ['customTag', 'fooo']; + + await hexo.scaffold.set('customscaff', [ + '---', + 'title: {{ title }}', + 'date: {{ date }}', + `tags: ${JSON.stringify(prefixTags)}`, + 'qwe: 123', + 'zxc: zxc', + '---' + ].join('\n')); + + const path = join(hexo.source_dir, '_posts', 'fooo.md'); + const data = await post.create({ + title: 'fooo', + layout: 'draft', + tags: customTags, + qwe: 456, + asd: 'asd' + }); + const result = await post.publish({ + slug: 'fooo', + layout: 'customscaff' + }); + + const fmt = yfm(result.content); + fmt.tags.sort().should.eql(deepMerge(prefixTags, customTags).sort()); + fmt.qwe.should.eql(456); + fmt.asd.should.eql('asd'); + fmt.zxc.should.eql('zxc'); + + await unlink(path); + }); + it('render()', async () => { // TODO: validate data const beforeHook = spy();