Skip to content

Commit

Permalink
refactor(post): async/await
Browse files Browse the repository at this point in the history
  • Loading branch information
curbengh committed Dec 15, 2019
1 parent 0bebf60 commit 2af5f54
Showing 1 changed file with 31 additions and 34 deletions.
65 changes: 31 additions & 34 deletions lib/hexo/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,55 +123,52 @@ 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);
let frontMatter = prepareFrontMatter({...data});
const yfmSplit = yfm.split(scaffold);

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

// Parse front-matter
const obj = jsonMode ? JSON.parse(`{${frontMatter}}`) : load(frontMatter);
const { separator } = yfmSplit;
const jsonMode = separator[0] === ';';

// 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];
}
// 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];
}
}

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

0 comments on commit 2af5f54

Please sign in to comment.