Skip to content

Commit

Permalink
feat(renderScaffold): deepMerge frontMatter of post and scaffold (#5472)
Browse files Browse the repository at this point in the history
* feat(renderScaffold): deepMerge frontMatter of post and scaffold

* test: add test case
  • Loading branch information
uiolee committed May 17, 2024
1 parent 22e81e4 commit 6d880a2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
10 changes: 3 additions & 7 deletions lib/hexo/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down
39 changes: 38 additions & 1 deletion test/scripts/hexo/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 6d880a2

Please sign in to comment.