Skip to content

Commit

Permalink
Merge branch 'master' into test/perf
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Sketon committed Jul 6, 2024
2 parents fa58a32 + a3f27b8 commit 04f141c
Show file tree
Hide file tree
Showing 14 changed files with 122 additions and 60 deletions.
1 change: 0 additions & 1 deletion .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ on:
push:
branches:
- "master"
- "v7.0.0"
paths:
- "lib/**"
- "test/**"
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/tester.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ on:
push:
branches:
- "master"
- "v7.0.0"
paths:
- "lib/**"
- "test/**"
- "package.json"
- "tsconfig.json"
- ".github/workflows/tester.yml"
pull_request:
paths:
- "lib/**"
- "test/**"
- "package.json"
- "tsconfig.json"
- ".github/workflows/tester.yml"

permissions:
Expand Down
15 changes: 7 additions & 8 deletions lib/box/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class Box extends EventEmitter {
const src = join(this.base, path);

return Cache.compareFile(
src.substring(ctx.base_dir.length),
escapeBackslash(src.substring(ctx.base_dir.length)),
() => getHash(src),
() => stat(src)
).then(result => ({
Expand All @@ -130,7 +130,7 @@ class Box extends EventEmitter {
if (!stats.isDirectory()) return;

// Check existing files in cache
const relativeBase = base.substring(ctx.base_dir.length);
const relativeBase = escapeBackslash(base.substring(ctx.base_dir.length));
const cacheFiles = Cache.filter(item => item._id.startsWith(relativeBase)).map(item => item._id.substring(relativeBase.length));

// Handle deleted files
Expand All @@ -157,12 +157,11 @@ class Box extends EventEmitter {
});

return BlueBirdPromise.reduce(this.processors, (count, processor) => {
// patten supports *nix style path only, replace backslashes on Windows
const params = processor.pattern.match(escapeBackslash(path));
const params = processor.pattern.match(path);
if (!params) return count;

const file = new File({
// source is used for file system path, keep backslashes on Windows
// source is used for filesystem path, keep backslashes on Windows
source: join(base, path),
// path is used for URL path, replace backslashes on Windows
path: escapeBackslash(path),
Expand Down Expand Up @@ -196,7 +195,7 @@ class Box extends EventEmitter {
const { base } = this;

function getPath(path) {
return path.substring(base.length);
return escapeBackslash(path.substring(base.length));
}

return this.process().then(() => watch(base, this.options)).then(watcher => {
Expand All @@ -216,7 +215,7 @@ class Box extends EventEmitter {

watcher.on('addDir', path => {
let prefix = getPath(path);
if (prefix) prefix += sep;
if (prefix) prefix += '/';

this._readDir(path, prefix);
});
Expand Down Expand Up @@ -289,7 +288,7 @@ function readDirWalker(ctx: Hexo, base: string, results: string[], ignore: strin
const prefixPath = `${prefix}${path}`;
if (stats) {
if (stats.isDirectory()) {
return readDirWalker(ctx, fullPath, results, ignore, prefixPath + sep);
return readDirWalker(ctx, fullPath, results, ignore, `${prefixPath}/`);
}
if (!isIgnoreMatch(fullPath, ignore)) {
results.push(prefixPath);
Expand Down
2 changes: 2 additions & 0 deletions lib/hexo/default_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export = {
exclude_languages: [] as string[],
strip_indent: true
},
use_filename_as_post_title: false,

// Category & Tag
default_category: 'uncategorized',
category_map: {},
Expand Down
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
2 changes: 1 addition & 1 deletion lib/plugins/processor/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function processPage(ctx: Hexo, file: _File) {
}

function processAsset(ctx: Hexo, file: _File) {
const id = relative(ctx.base_dir, file.source);
const id = relative(ctx.base_dir, file.source).replace(/\\/g, '/');
const Asset = ctx.model('Asset');
const doc = Asset.findById(id);

Expand Down
14 changes: 10 additions & 4 deletions lib/plugins/processor/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ function processPost(ctx: Hexo, file: _File) {
const { path } = file.params;
const doc = Post.findOne({source: file.path});
const { config } = ctx;
const { timezone: timezoneCfg } = config;
const updated_option = config.updated_option;
const { timezone: timezoneCfg, updated_option, use_slug_as_post_title } = config;

let categories, tags;

if (file.type === 'skip' && doc) {
Expand Down Expand Up @@ -111,6 +111,12 @@ function processPost(ctx: Hexo, file: _File) {
if (!preservedKeys[key]) data[key] = info[key];
}

// use `slug` as `title` of post when `title` is not specified.
// https://github.com/hexojs/hexo/issues/5372
if (use_slug_as_post_title && !('title' in data)) {
data.title = info.title;
}

if (data.date) {
data.date = toDate(data.date);
} else if (info && info.year && (info.month || info.i_month) && (info.day || info.i_day)) {
Expand Down Expand Up @@ -235,7 +241,7 @@ function scanAssetDir(ctx: Hexo, post) {
if (err && err.code === 'ENOENT') return [];
throw err;
}).filter(item => !isExcludedFile(item, ctx.config)).map(item => {
const id = join(assetDir, item).substring(baseDirLength);
const id = join(assetDir, item).substring(baseDirLength).replace(/\\/g, '/');
const renderablePath = id.substring(sourceDirLength + 1);
const asset = PostAsset.findById(id);

Expand Down Expand Up @@ -269,7 +275,7 @@ function shouldSkipAsset(ctx: Hexo, post, asset) {
function processAsset(ctx: Hexo, file: _File) {
const PostAsset = ctx.model('PostAsset');
const Post = ctx.model('Post');
const id = file.source.substring(ctx.base_dir.length);
const id = file.source.substring(ctx.base_dir.length).replace(/\\/g, '/');
const postAsset = PostAsset.findById(id);

if (file.type === 'delete' || Post.length === 0) {
Expand Down
2 changes: 1 addition & 1 deletion lib/theme/processors/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { _File } from '../../box';

function process(file: _File) {
const Asset = this.model('Asset');
const id = file.source.substring(this.base_dir.length);
const id = file.source.substring(this.base_dir.length).replace(/\\/g, '/');
const { path } = file.params;
const doc = Asset.findById(id);

Expand Down
11 changes: 5 additions & 6 deletions test/scripts/box/box.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ describe('Box', () => {
const box = newBox('test');
const name = 'a.txt';
const path = join(box.base, name);
const cacheId = join('test/', name);
const cacheId = 'test/' + name;

const processor = spy();
box.addProcessor(processor);
Expand All @@ -143,7 +143,7 @@ describe('Box', () => {
const box = newBox('test');
const name = 'a.txt';
const path = join(box.base, name);
const cacheId = join('test/', name);
const cacheId = 'test/' + name;

const processor = spy();
box.addProcessor(processor);
Expand All @@ -166,7 +166,7 @@ describe('Box', () => {
const box = newBox('test');
const name = 'a.txt';
const path = join(box.base, name);
const cacheId = join('test/', name);
const cacheId = 'test/' + name;

const processor = spy();
box.addProcessor(processor);
Expand All @@ -189,7 +189,7 @@ describe('Box', () => {
const box = newBox('test');
const name = 'a.txt';
const path = join(box.base, name);
const cacheId = join('test/', name);
const cacheId = 'test/' + name;

const processor = spy();
box.addProcessor(processor);
Expand All @@ -210,8 +210,7 @@ describe('Box', () => {

it('process() - delete', async () => {
const box = newBox('test');
// join will replace backslashes on Windows
const cacheId = join('test/', 'a.txt');
const cacheId = 'test/a.txt';

const processor = spy();
box.addProcessor(processor);
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
12 changes: 6 additions & 6 deletions test/scripts/processors/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('asset', () => {

await writeFile(file.source, 'foo');
await process(file);
const id = join('source/', file.path);
const id = 'source/' + file.path;
const asset = Asset.findById(id);

asset._id.should.eql(id);
Expand All @@ -103,7 +103,7 @@ describe('asset', () => {

await writeFile(file.source, 'foo');
await process(file);
const id = join('../source/', 'foo.jpg'); // The id should a relative path, because the 'lib/models/assets.js' use asset path by joining base path with "_id" directly.
const id = '../source/foo.jpg'; // The id should a relative path,because the 'lib/models/assets.js' use asset path by joining base path with "_id" directly.
const asset = Asset.findById(id);
asset._id.should.eql(id);
asset.path.should.eql(file.path);
Expand All @@ -122,7 +122,7 @@ describe('asset', () => {
renderable: false
});

const id = join('source/', file.path);
const id = 'source/' + file.path;

await Promise.all([
writeFile(file.source, 'test'),
Expand Down Expand Up @@ -153,7 +153,7 @@ describe('asset', () => {
renderable: false
});

const id = join('source/', file.path);
const id = 'source/' + file.path;

await Promise.all([
writeFile(file.source, 'test'),
Expand All @@ -179,7 +179,7 @@ describe('asset', () => {
renderable: false
});

const id = join('source/', file.path);
const id = 'source/' + file.path;

await Asset.insert({
_id: id,
Expand All @@ -197,7 +197,7 @@ describe('asset', () => {
renderable: false
});

const id = join('source/', file.path);
const id = 'source/' + file.path;
await process(file);

should.not.exist(Asset.findById(id));
Expand Down
Loading

0 comments on commit 04f141c

Please sign in to comment.