fix(markdown): only highlight code blocks with explicit language class#559
fix(markdown): only highlight code blocks with explicit language class#559lezi-fun wants to merge 2 commits into
Conversation
hljs.highlightAll() applies auto-detection to all <pre><code> blocks, including those without a language-* class from markdown code fences with no language specified. Replace with selective highlightElement(): only target code blocks that have an explicit language-* class, skip language-plain. Closes OpenListTeam/OpenList#2342
jyxjjj
left a comment
There was a problem hiding this comment.
I don’t think this fixes the root cause. When I investigated the original issue, many fenced code blocks without an explicit language had already been assigned generated language-* classes, so selecting pre code[class*="language-"] will still match those incorrectly classified blocks.
Also, the PR description does not comply with the repository’s Pull Request template. Please keep the template intact and fill in the required fields instead of modifying its structure or content.
|
@jyxjjj Thanks for the review!
This is actually a misunderstanding — let me clarify the rendering pipeline:
The I also tested on both versions to verify:
|
|
One minor follow-up: should we also skip Also, it may be safer to check |
- Add language-plaintext and language-text to the skip set - Use hljs.getLanguage() to verify the language is supported by Highlight.js before calling highlightElement(), preventing unnecessary handling of unknown language classes
|
Good suggestions — updated in 99844dc:
|
Summary / 摘要
hljs.highlightAll()applies auto-detection to all<pre><code>blocks,including those without a
language-*class from code fences with nolanguage specified. This causes random highlighting on plain text blocks.
Replaced with selective
highlightElement()— only target code blocksthat have an explicit
language-*class, skiplanguage-plain./ 此 PR 包含破坏性变更。
/ 此 PR 修改了公开 API、配置、存储格式或迁移行为。
/ 此 PR 需要关联仓库同步修改。
Related repository PRs / 关联仓库 PR:
Related Issues / 关联 Issue
Closes OpenListTeam/OpenList#2342
Testing / 测试
go test ./.../tmp/testdirectory containing the exact code blocks from the issue
(SQL Server keys, rarreg.key, VMware keys). Plain code blocks
no longer show random highlighting.
Checklist / 检查清单
/ 我已阅读 CONTRIBUTING。
/ 我确认此贡献符合仓库许可证、贡献规范和行为准则。
prettierwhere applicable./ 我已按适用情况使用
prettier格式化变更代码。/ 我已在适用情况下请求相关维护者或代码所有者审查。
AI Disclosure / AI 使用声明
/ 此 PR 包含 AI 辅助内容。
Tools used / 使用工具:
Usage scope / 使用范围:
Code generation / 代码生成
Refactoring / 重构
Documentation / 文档
Tests / 测试
Translation / 翻译
Review assistance / 审查辅助
I have reviewed and validated all AI-assisted content included in this PR.
/ 我已审核并验证此 PR 中的所有 AI 辅助内容。
I have ensured that all AI-assisted commits include
Co-Authored-Byattribution./ 我已确保所有 AI 辅助提交都包含
Co-Authored-By归属信息。I can reproduce all AI-assisted content included in this PR without any AI tools.
/ 我可以在没有任何 AI 工具的情况下重现此 PR 中包含的所有 AI 辅助内容。