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

feat(renderScaffold): deepMerge frontMatter of post and scaffold #5472

Merged
merged 2 commits into from
May 17, 2024
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
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
Loading