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

Code blocks in HTML comments were incorrectly converted #5433

Open
5 tasks done
FZQ0003 opened this issue Mar 12, 2024 · 2 comments
Open
5 tasks done

Code blocks in HTML comments were incorrectly converted #5433

FZQ0003 opened this issue Mar 12, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@FZQ0003
Copy link

FZQ0003 commented Mar 12, 2024

Check List

  • I have already read Docs page & Troubleshooting page.
  • I have already searched existing issues and they are not help to me.
  • I examined error or warning messages and it's difficult to solve.
  • I am using the latest version of Hexo. (run hexo version to check)
  • My Node.js is matched the required version.

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

-->
<p>Text 2</p>

Actual behavior

In my newly installed blog directory:

image

Output:

<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

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
Copy link
Author

FZQ0003 commented Mar 12, 2024

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.

@FZQ0003 FZQ0003 changed the title Fenced code blocks in comments were incorrectly converted Code blocks in HTML comments were incorrectly converted Mar 12, 2024
@D-Sketon
Copy link
Member

D-Sketon commented Mar 12, 2024

hexo wraps the highlighting code in <! -- --> and passes it into the markdown renderer, making it difficult to deal with nested comments.

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

Notice that the end of the comment is escaped as <p>-&gt;</p>, causing a comment tag pairing failure.

@D-Sketon D-Sketon added the bug Something isn't working label Apr 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants