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

Using hexo.render.renderSync, Tag Plugins are not resolved #4713

Open
4 of 5 tasks
kristofzerbe opened this issue Jun 5, 2021 · 3 comments
Open
4 of 5 tasks

Using hexo.render.renderSync, Tag Plugins are not resolved #4713

kristofzerbe opened this issue Jun 5, 2021 · 3 comments

Comments

@kristofzerbe
Copy link
Contributor

kristofzerbe commented Jun 5, 2021

Check List

Question

I have created a generator, which loads extra Markdown content while processing. Beside parsing the Frontmatter, it renders the content with renderSync:

...
const md = fs.readFileSync(mdSource);
data = front.parse(md);
data.content = hexo.render.renderSync({ text: data._content, engine: 'markdown' });
...

Works like charm ... but tag plugins contained in the content (which are working properly in pages/posts) are not resolved.

Code: https://github.com/kristofzerbe/hexo-generator-anything/blob/main/lib/generator.js

Environment & Settings

Node.js & npm version

Node: v12.16.3
NPM: 6.14.4

Hexo and Plugin version(npm ls --depth 0)

+-- hexo@5.4.0
+-- hexo-asset-link@2.1.0
+-- hexo-browsersync@0.3.0
+-- hexo-cli@4.2.0
+-- hexo-feed@1.1.0
+-- hexo-fontawesome@2.2.1
+-- hexo-front-matter@2.0.0
+-- hexo-fs@3.1.0
+-- hexo-generator-alias@1.0.0
+-- hexo-generator-anything@1.0.4
+-- hexo-generator-archive@1.0.0
+-- hexo-generator-category@1.0.0
+-- hexo-generator-copy@0.1.0
+-- hexo-generator-index@2.0.0
+-- hexo-generator-json-content@4.2.3
+-- hexo-generator-search@2.4.2
+-- hexo-generator-sitemap@2.1.0
+-- hexo-generator-tag@1.0.0
+-- hexo-hide-posts@0.1.1
+-- hexo-pwa@0.1.3
+-- hexo-renderer-ejs@1.0.0
+-- hexo-renderer-marked@4.0.0
+-- hexo-renderer-stylus@2.0.1
+-- hexo-server@2.0.0

Your package.json package.json

{
  "name": "hexo-site",
  "version": "0.0.0",
  "private": true,
  "hexo": {
    "version": "5.4.0"
  },
  "dependencies": {
    "hexo": "^5.4.0",
    "hexo-asset-link": "^2.1.0",
    "hexo-browsersync": "^0.3.0",
    "hexo-fontawesome": "^2.2.1",
    "hexo-generator-alias": "^1.0.0",
    "hexo-generator-anything": "^1.0.4",
    "hexo-generator-archive": "^1.0.0",
    "hexo-generator-category": "^1.0.0",
    "hexo-generator-copy": "^0.1.0",
    "hexo-generator-index": "^2.0.0",
    "hexo-generator-json-content": "^4.2.3",
    "hexo-generator-search": "^2.4.2",
    "hexo-generator-sitemap": "^2.1.0",
    "hexo-generator-tag": "^1.0.0",
    "hexo-hide-posts": "^0.1.1",
    "hexo-pwa": "^0.1.3",
    "hexo-renderer-ejs": "^1.0.0",
    "hexo-renderer-marked": "^4.0.0",
    "hexo-renderer-stylus": "^2.0.1",
    "hexo-server": "^2.0.0",
    "object-assign": "^4.1.1"
  },
  "devDependencies": {
    "hexo-cli": "^4.2.0",
    "hexo-feed": "^1.1.0",
    "hexo-front-matter": "^2.0.0",
    "hexo-fs": "^3.1.0"
  }
}
@github-actions
Copy link

github-actions bot commented Aug 5, 2021

This issue has been automatically marked as stale because lack of recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@kristofzerbe
Copy link
Contributor Author

Thanks for reopening the issue...

What I've mentioned, that code blocks in the content are also not converted through my configured highlight.js

Is there any chance to get a solution or an hint how to achieve my goal?

@stevenjoezhang
Copy link
Member

For example, hexo.render.render is called in line 403 to 418 of /lib/hexo/post.js:

hexo/lib/hexo/post.js

Lines 403 to 418 in 00bcce5

return ctx.render.render({
text: data.content,
path: source,
engine: data.engine,
toString: true,
onRenderEnd(content) {
// Replace cache data with real contents
data.content = cacheObj.restoreAllSwigTags(content);
// Return content after replace the placeholders
if (disableNunjucks) return data.content;
// Render with Nunjucks
return tag.render(data.content, data);
}
}, options);

However, the processing of code blocks and tag plugins are as follows, from line 362 to 425:

hexo/lib/hexo/post.js

Lines 362 to 425 in 00bcce5

render(source, data = {}, callback) {
const ctx = this.context;
const { config } = ctx;
const { tag } = ctx.extend;
const ext = data.engine || (source ? extname(source) : '');
let promise;
if (data.content != null) {
promise = Promise.resolve(data.content);
} else if (source) {
// Read content from files
promise = readFile(source);
} else {
return Promise.reject(new Error('No input file or string!')).asCallback(callback);
}
// disable Nunjucks when the renderer specify that.
let disableNunjucks = ext && ctx.render.renderer.get(ext) && !!ctx.render.renderer.get(ext).disableNunjucks;
// front-matter overrides renderer's option
if (typeof data.disableNunjucks === 'boolean') disableNunjucks = data.disableNunjucks;
const cacheObj = new PostRenderEscape();
return promise.then(content => {
data.content = content;
// Run "before_post_render" filters
return ctx.execFilter('before_post_render', data, { context: ctx });
}).then(() => {
data.content = cacheObj.escapeCodeBlocks(data.content);
// Escape all Nunjucks/Swig tags
if (disableNunjucks === false) {
data.content = cacheObj.escapeAllSwigTags(data.content);
}
const options = data.markdown || {};
if (!config.highlight.enable) options.highlight = null;
ctx.log.debug('Rendering post: %s', magenta(source));
// Render with markdown or other renderer
return ctx.render.render({
text: data.content,
path: source,
engine: data.engine,
toString: true,
onRenderEnd(content) {
// Replace cache data with real contents
data.content = cacheObj.restoreAllSwigTags(content);
// Return content after replace the placeholders
if (disableNunjucks) return data.content;
// Render with Nunjucks
return tag.render(data.content, data);
}
}, options);
}).then(content => {
data.content = cacheObj.restoreCodeBlocks(content);
// Run "after_post_render" filters
return ctx.execFilter('after_post_render', data, { context: ctx });
}).asCallback(callback);
}

In other words, hexo.render.renderSync (or hexo.render.render) is just one step of post-rendering. You need to include the pre-processing code of Hexo in your scripts to make tag plugins work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants