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

refactor(post): async/await #3939

Closed
wants to merge 3 commits into from
Closed
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
70 changes: 32 additions & 38 deletions lib/hexo/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,13 @@ const removeExtname = str => {
return str.substring(0, str.length - extname(str).length);
};

const createAssetFolder = (path, assetFolder) => {
const createAssetFolder = async(path, assetFolder) => {
if (!assetFolder) return Promise.resolve();

const target = removeExtname(path);

return exists(target).then(exist => {
if (!exist) return mkdirs(target);
});
const exist = await exists(target);
if (!exist) return mkdirs(target);
};

class Post {
Expand Down Expand Up @@ -123,55 +122,50 @@ class Post {
}).asCallback(callback);
}

_getScaffold(layout) {
async _getScaffold(layout) {
const ctx = this.context;
const result = await ctx.scaffold.get(layout);

return ctx.scaffold.get(layout).then(result => {
if (result != null) return result;
return ctx.scaffold.get('normal');
});
if (result != null) return result;
return ctx.scaffold.get('normal');
}

_renderScaffold(data) {
async _renderScaffold(data) {
const { tag } = this.context.extend;
let yfmSplit;

return this._getScaffold(data.layout).then(scaffold => {
const frontMatter = prepareFrontMatter({...data});
yfmSplit = yfm.split(scaffold);
const scaffold = await this._getScaffold(data.layout);
const yfmSplit = yfm.split(scaffold);
const frontMatter = await tag.render(yfmSplit.data, prepareFrontMatter({...data}));

return tag.render(yfmSplit.data, frontMatter);
}).then(frontMatter => {
const { separator } = yfmSplit;
const jsonMode = separator[0] === ';';
const { separator } = yfmSplit;
const jsonMode = separator[0] === ';';

// Parse front-matter
const obj = jsonMode ? JSON.parse(`{${frontMatter}}`) : load(frontMatter);
// Parse front-matter
const obj = jsonMode ? JSON.parse(`{${frontMatter}}`) : load(frontMatter);

// Add data which are not in the front-matter
for (const key of Object.keys(data)) {
if (!preservedKeys.includes(key) && obj[key] == null) {
obj[key] = data[key];
}
// Add data which are not in the front-matter
for (const key of Object.keys(data)) {
if (!preservedKeys.includes(key) && obj[key] == null) {
obj[key] = data[key];
}
}

let content = '';
// Prepend the separator
if (yfmSplit.prefixSeparator) content += `${separator}\n`;
let content = '';
// Prepend the separator
if (yfmSplit.prefixSeparator) content += `${separator}\n`;

content += yfm.stringify(obj, {
mode: jsonMode ? 'json' : ''
});
content += yfm.stringify(obj, {
mode: jsonMode ? 'json' : ''
});

// Concat content
content += yfmSplit.content;
// Concat content
content += yfmSplit.content;

if (data.content) {
content += `\n${data.content}`;
}
if (data.content) {
content += `\n${data.content}`;
}

return content;
});
return content;
}

publish(data, replace, callback) {
Expand Down