hexojs / hexo

A fast, simple & powerful blog framework, powered by Node.js.
https://hexo.io
MIT License
38.86k stars 4.76k forks source link

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

Open kristofzerbe opened 3 years ago

kristofzerbe commented 3 years ago

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[bot] commented 2 years ago

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 commented 1 year ago

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 commented 1 year ago

For example, hexo.render.render is called in line 403 to 418 of /lib/hexo/post.js: https://github.com/hexojs/hexo/blob/00bcce521678ca168b3565ed2a7d0512be07a2ed/lib/hexo/post.js#L403-L418

However, the processing of code blocks and tag plugins are as follows, from line 362 to 425: https://github.com/hexojs/hexo/blob/00bcce521678ca168b3565ed2a7d0512be07a2ed/lib/hexo/post.js#L362-L425

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