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

Code blocks in HTML comments were incorrectly converted #5433

Open FZQ0003 opened 3 months ago

FZQ0003 commented 3 months ago

Check List

Expected behavior

In my old blog directory (replaced theme with landscape):

image

Output:

<p>Text 1</p>
<!--

Commented text 1
A commented code block
```

Commented text 2

-->

Text 2


### Actual behavior

In my newly installed blog directory:

![image](https://github.com/hexojs/hexo/assets/56470931/d9a93b61-0430-4ec6-b706-f82e503749d4)

Output:

``` html
<p>Text 1</p>
<!--

Commented text 1

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">A commented code block</span><br></pre></td></tr></table></figure>

<p>Commented text 2</p>
<p>–&gt;</p>
<p>Text 2</p>

...
</html>-->

How to reproduce?

Write a post like this and then start the server:

---
title: Test
layout: post
---

Text 1

<!--

Commented text 1
A commented code block
```

Commented text 2

-->

Text 2


### Is the problem still there under `Safe mode`?

Cannot generate pages under safe mode.

### Your Node.js & npm version

```text
v21.6.1
10.5.0

Your Hexo and Plugin version

hexo-blog-fqilin@0.0.0 D:\Server\hexo-blog-src
├── 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.2.0
├── hexo-renderer-stylus@3.0.1
├── hexo-server@3.0.0
├── hexo-theme-fluid@1.9.7
├── hexo-theme-landscape@1.0.0
└── hexo@7.1.1

Your package.json

{
  "name": "hexo-blog-fqilin",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "build": "hexo generate",
    "clean": "hexo clean",
    "deploy": "hexo deploy",
    "server": "hexo server"
  },
  "hexo": {
    "version": "7.1.1"
  },
  "dependencies": {
    "hexo": "^7.0.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-fluid": "^1.9.7",
    "hexo-theme-landscape": "^1.0.0"
  }
}

Your site's _config.yml (Optional)

No response

Others

Additionally, here is a list of plugins used in my OLD environment:

hexo-site@0.0.0 D:\Server\hexo-fqilin
├── hexo-deployer-git@3.0.0
├── hexo-deployer-sftp@0.4.1
├── hexo-deployer-shell@0.2.3
├── hexo-generator-archive@1.0.0
├── hexo-generator-category@1.0.0
├── hexo-generator-index@2.0.0
├── hexo-generator-search@2.4.3
├── hexo-generator-tag@1.0.0
├── hexo-helper-post-top@0.0.2
├── hexo-helper-qrcode@1.0.2
├── hexo-renderer-ejs@2.0.0
├── hexo-renderer-markdown-it@6.0.1
├── hexo-renderer-stylus@2.0.1
├── hexo-server@2.0.0
├── hexo-theme-landscape@0.0.3
└── hexo@5.4.2

When I was trying to setup a new environment, this issue appeared. I don't know why.

FZQ0003 commented 3 months ago

I have found the reason.
This issue (typing markdown fenced code blocks in html comments) only appears when I enable one of built-in syntax highlight libraries. In my old environment, I disabled them and use Prettify instead, and it worked well.
I'll use another highlighter.

By the way, I have read an article about code blocks https://hexo.io/docs/syntax-highlight.html#How-to-use-code-block-in-posts. It says code block syntax will always be available, so I did additional tests.

Syntax highlighting is disabled.

Source:

Text 1

<!--

Commented text 1

{% codeblock %}
A commented code block
{% endcodeblock %}

Commented text 2

-->

Text 2

Output:

<p>Text 1</p>
<!--

Commented text 1

<pre><code>A commented code block</code></pre>

<p>Commented text 2</p>
<p>–&gt;</p>
<p>Text 2</p>

...
</html>-->

I should edit the title.

D-Sketon commented 3 months ago

hexo wraps the highlighting code in <! -- --> and passes it into the markdown renderer, making it difficult to deal with nested comments. https://github.com/hexojs/hexo/blob/3c7729dbb6f758564ac1853222541ec52087c9e3/lib/hexo/post.ts#L42 Notice that the end of the comment is escaped as <p>-&gt;</p>, causing a comment tag pairing failure.