hexojs / hexo

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

Rendering issue with Tag Plugin #5309

Open PeichengLiu opened 8 months ago

PeichengLiu commented 8 months ago

Check List

Expected behavior

Fyi, I'm using https://github.com/next-theme/hexo-theme-next.

with

{% label @v1 %} v2

{% label @v3 %} v4

I would expect two paragraphs generated.

<p><mark class="label ">v1</mark> v2</p>
<p><mark class="label ">v3</mark> v4</p>

Actual behavior

No paragraphs generated.

<mark class="label ">v1</mark> v2

<mark class="label ">v3</mark> v4

How to reproduce?

https://github.com/PeichengLiu/for-hexo-rendering-issue is ready for your convenience.

Is the problem still there under "Safe mode"?

No HTML generated.

Environment & Settings

Node.js & npm version(node -v && npm -v)

v18.18.0
9.8.1

Your site _config.yml (Optional)

The only configuration I changed is theme: next, and no configuration for the theme itself.

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

hexo-site@0.0.0 C:\Users\14517\Desktop\test-blog\blog
├── hexo-generator-archive@2.0.0 -> .\node_modules\.pnpm\hexo-generator-archive@2.0.0\node_modules\hexo-generator-archive
├── hexo-generator-category@2.0.0 -> .\node_modules\.pnpm\hexo-generator-category@2.0.0\node_modules\hexo-generator-category
├── hexo-generator-index@3.0.0 -> .\node_modules\.pnpm\hexo-generator-index@3.0.0\node_modules\hexo-generator-index
├── hexo-generator-tag@2.0.0 -> .\node_modules\.pnpm\hexo-generator-tag@2.0.0\node_modules\hexo-generator-tag
├── hexo-renderer-ejs@2.0.0 -> .\node_modules\.pnpm\hexo-renderer-ejs@2.0.0\node_modules\hexo-renderer-ejs
├── hexo-renderer-marked@6.1.1 -> .\node_modules\.pnpm\hexo-renderer-marked@6.1.1\node_modules\hexo-renderer-marked
├── hexo-renderer-stylus@3.0.0 -> .\node_modules\.pnpm\hexo-renderer-stylus@3.0.0\node_modules\hexo-renderer-stylus
├── hexo-server@3.0.0 -> .\node_modules\.pnpm\hexo-server@3.0.0\node_modules\hexo-server
├── hexo-theme-landscape@1.0.0 -> .\node_modules\.pnpm\hexo-theme-landscape@1.0.0\node_modules\hexo-theme-landscape
├── hexo-theme-next@8.18.2 -> .\node_modules\.pnpm\hexo-theme-next@8.18.2\node_modules\hexo-theme-next
└── hexo@6.3.0 -> .\node_modules\.pnpm\hexo@6.3.0\node_modules\hexo

Your package.json package.json

{
  "name": "hexo-site",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "build": "hexo generate",
    "clean": "hexo clean",
    "deploy": "hexo deploy",
    "server": "hexo server"
  },
  "hexo": {
    "version": "6.3.0"
  },
  "dependencies": {
    "hexo": "^6.3.0",
    "hexo-generator-archive": "^2.0.0",
    "hexo-generator-category": "^2.0.0",
    "hexo-generator-index": "^3.0.0",
    "hexo-generator-tag": "^2.0.0",
    "hexo-renderer-ejs": "^2.0.0",
    "hexo-renderer-marked": "^6.0.0",
    "hexo-renderer-stylus": "^3.0.0",
    "hexo-server": "^3.0.0",
    "hexo-theme-landscape": "^1.0.0",
    "hexo-theme-next": "^8.18.2"
  }
}

Others

I tried to dig out the root cause and fix it. It seems to be related to the way how Hexo implements Tag Plugins. I change

https://github.com/hexojs/hexo/blob/ad056527cee2c55fa961cc84a56f44ea55404c63/lib/hexo/post.js#L16-L17

to

const rSwigPlaceHolder = /swig(?:<|&lt;)!--\uFFFC(\d+)--(?:>|&gt;)/g;
const rCodeBlockPlaceHolder = /code(?:<|&lt;)!--\uFFFC(\d+)--(?:>|&gt;)/g;

, and

https://github.com/hexojs/hexo/blob/ad056527cee2c55fa961cc84a56f44ea55404c63/lib/hexo/post.js#L37-L39

to

  static escapeContent(cache, flag, str) {
    return `${flag}<!--\uFFFC${cache.push(str) - 1}-->`;
  }

. It solves the problem above, while it breaks unit tests. Which is another part I do not quite understand. For example, should not the following test

https://github.com/hexojs/hexo/blob/ad056527cee2c55fa961cc84a56f44ea55404c63/test/scripts/hexo/post.js#L1249-L1276

expect

<p><code>{{ 1 + 1 }}</code> 3 <code>{{ 2 + 2 }}</code><br>Text<br>
Raw1
<br>AnotherText<br>
Raw2
</p>

? With a single \n between texts, how can Another Text be wrapped in a paragraph?

Finally, one another question, why Hexo is using an outdated markedjs with some copied code snippets and uncleared (to me) customization?

Thanks for your work!

uiolee commented 5 months ago

This is probably a feature of hexo and not easy to change. If you don't use <!-- --> to escape the tag, it will be wrapped in <p></p> by the markdown renderer, which may have side effects on some tags or themes.