Skip to content

fix(markdown): disable indented code blocks in comment rendering#958

Merged
imaegoo merged 1 commit into
twikoojs:mainfrom
wyf027:fix/disable-indented-code-blocks
May 28, 2026
Merged

fix(markdown): disable indented code blocks in comment rendering#958
imaegoo merged 1 commit into
twikoojs:mainfrom
wyf027:fix/disable-indented-code-blocks

Conversation

@wyf027

@wyf027 wyf027 commented May 25, 2026

Copy link
Copy Markdown
Contributor

Summary

Disable Markdown's legacy 4-space indented code blocks in Twikoo comment rendering. Fenced ``` code blocks remain supported.

Problem

Fixes #855. Users who prefix lines with four spaces for visual indentation were getting code-block layout instead of normal paragraph text.

Test plan

  • Comment with lines starting with four spaces renders as paragraph text
  • Fenced ``` code blocks still render correctly
  • Preview mode behaves the same as submitted comments

Summary by Sourcery

Bug Fixes:

  • 防止注释中以四个空格开头的行被错误渲染为缩进代码块。
Original summary in English

Summary by Sourcery

Bug Fixes:

  • Prevent lines starting with four spaces in comments from being rendered as unintended indented code blocks.

Lines prefixed with four spaces were parsed as indented code blocks,
breaking paragraph layout when users tried to indent text with spaces.

Keep fenced ``` code blocks; only disable the legacy 4-space rule.

Fixes #855

Co-authored-by: Cursor <cursoragent@cursor.com>
@sourcery-ai

sourcery-ai Bot commented May 25, 2026

Copy link
Copy Markdown
Contributor
审阅者指南(在小型 PR 上折叠显示)

审阅者指南

通过重写 marked 的 tokenizer 中的 code 处理函数,禁用 Markdown 旧版的 4 空格缩进代码块渲染,同时保持围栏代码块(fenced code block)的行为不变。

取消缩进代码块后的评论 Markdown 渲染时序图

sequenceDiagram
  title Sequence diagram for comment markdown rendering without indented code blocks
  actor User
  participant CommentUI
  participant marked
  participant tokenizer_code

  User->>CommentUI: submitComment(textWithLeadingSpaces)
  CommentUI->>marked: parse(textWithLeadingSpaces)
  marked->>tokenizer_code: code(src, tokens)
  tokenizer_code-->>marked: undefined
  marked-->>CommentUI: htmlWithParagraphs
  CommentUI-->>User: renderComment(htmlWithParagraphs)
Loading

文件级变更

变更 详情 文件
重写 marked 的缩进代码块 tokenizer,使以 4 个空格缩进的行在评论中不再被渲染为代码块。
  • 调整 marked 全局选项,通过 marked.use 插入自定义插件
  • 提供一个 tokenizer 覆写:其 code() 方法返回 undefined,以禁用默认的缩进代码块检测,同时保留围栏代码块的行为
  • 添加内联文档,说明为什么在评论 UI 中禁用了缩进代码块,并引用 issue 在行首添加四个空格导致排版失效 #855
src/client/utils/marked.js

与关联 issue 的对照评估

Issue 目标 是否解决 说明
#855 确保以四个前置空格开头的评论行渲染为普通段落文本(而不是缩进的 Markdown 代码块),从而使视觉缩进不会破坏布局。

可能相关的 issues


技巧与命令

与 Sourcery 互动

  • 触发新的审阅: 在 pull request 中评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的审阅评论。
  • 从审阅评论生成 GitHub issue: 在某条审阅评论下回复,要求 Sourcery 从该评论创建 issue。你也可以直接回复 @sourcery-ai issue,从这条评论创建 issue。
  • 生成 pull request 标题: 在 pull request 标题中的任意位置写上 @sourcery-ai,即可随时生成标题。你也可以在 pull request 中评论 @sourcery-ai title 随时(重新)生成标题。
  • 生成 pull request 摘要: 在 pull request 正文中的任意位置写上 @sourcery-ai summary,即可在你期望的位置生成 PR 摘要。你也可以在 pull request 中评论 @sourcery-ai summary 随时(重新)生成摘要。
  • 生成审阅者指南: 在 pull request 中评论 @sourcery-ai guide,即可随时(重新)生成审阅者指南。
  • 批量解决所有 Sourcery 评论: 在 pull request 中评论 @sourcery-ai resolve,即可将所有 Sourcery 评论标记为已解决。如果你已经处理完所有评论且不想再看到它们,这会很有用。
  • 批量取消所有 Sourcery 审阅: 在 pull request 中评论 @sourcery-ai dismiss,即可取消所有现有的 Sourcery 审阅。特别适合在你想从头开始新的审阅时使用——别忘了再评论 @sourcery-ai review 来触发新的审阅!

自定义使用体验

访问你的 控制面板 以:

  • 启用或禁用审阅功能,例如 Sourcery 自动生成的 pull request 摘要、审阅者指南等。
  • 更改审阅语言。
  • 添加、移除或编辑自定义审阅指令。
  • 调整其他审阅设置。

获取帮助

Original review guide in English
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Disables Markdown’s legacy 4-space indented code blocks in comment rendering by overriding the marked tokenizer’s code handler while keeping fenced code blocks intact.

Sequence diagram for comment markdown rendering without indented code blocks

sequenceDiagram
  title Sequence diagram for comment markdown rendering without indented code blocks
  actor User
  participant CommentUI
  participant marked
  participant tokenizer_code

  User->>CommentUI: submitComment(textWithLeadingSpaces)
  CommentUI->>marked: parse(textWithLeadingSpaces)
  marked->>tokenizer_code: code(src, tokens)
  tokenizer_code-->>marked: undefined
  marked-->>CommentUI: htmlWithParagraphs
  CommentUI-->>User: renderComment(htmlWithParagraphs)
Loading

File-Level Changes

Change Details Files
Override marked’s indented code-block tokenizer so 4-space-indented lines no longer render as code blocks in comments.
  • Adjust marked global options to plug in a custom plugin via marked.use
  • Provide a tokenizer override where the code() method returns undefined to disable default indented code block detection while preserving fenced code block behavior
  • Add inline documentation explaining why indented code blocks are disabled in the comment UI and referencing issue 在行首添加四个空格导致排版失效 #855
src/client/utils/marked.js

Assessment against linked issues

Issue Objective Addressed Explanation
#855 Ensure that comment lines starting with four leading spaces are rendered as normal paragraph text (not as indented Markdown code blocks), so that visual indentation does not break layout.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - 我发现了 1 个问题,并留下了一些总体反馈:

  • tokenizer.code 覆写为始终返回 undefined 也会禁用围栏代码块,这与 PR 描述相矛盾;建议检查源码,只跳过缩进(4 空格)代码块,同时保留围栏代码块。
  • 如果这个 marked 实例在评论渲染之外也会被重用,那么在这里全局禁用代码块可能会带来意料之外的副作用;建议把这个 tokenizer 覆写限定在用于评论的专用 Markdown 处理流程中,而不是放在共享工具里。
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Overriding `tokenizer.code` to always return `undefined` will also disable fenced code blocks, which contradicts the PR description; consider inspecting the source to skip only indented (4-space) code blocks while preserving fenced blocks.
- If this `marked` instance is reused outside of comment rendering, globally disabling code blocks here may have unintended side effects; consider scoping this tokenizer override to the comment-specific Markdown pipeline instead of the shared utility.

## Individual Comments

### Comment 1
<location path="src/client/utils/marked.js" line_range="21-22" />
<code_context>
+// otherwise render as a code block (#855).
+marked.use({
+  tokenizer: {
+    code () {
+      return undefined
+    }
+  }
</code_context>
<issue_to_address>
**issue (bug_risk):**`return undefined` 覆写 `tokenizer.code` 很可能并不能按预期禁用 4 空格代码块。

在 marked 的扩展 API 中,从 tokenizer 方法返回 `undefined` 通常意味着回退到默认 tokenizer,所以这可能实际上并不会禁用缩进代码块。要可靠地关闭 4 空格缩进代码,你很可能需要要么将这些行转换成其他 token 类型(例如 paragraph/text),要么调整块级规则,使得缩进行永远不会到达 `code()`。请确认你所使用的 marked 版本中 `code()` tokenizer 的约定,以避免悄悄地保持原有行为不变。
</issue_to_address>

Sourcery 对开源项目免费——如果你觉得我们的评审有帮助,请考虑分享 ✨
帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈改进评审质量。
Original comment in English

Hey - I've found 1 issue, and left some high level feedback:

  • Overriding tokenizer.code to always return undefined will also disable fenced code blocks, which contradicts the PR description; consider inspecting the source to skip only indented (4-space) code blocks while preserving fenced blocks.
  • If this marked instance is reused outside of comment rendering, globally disabling code blocks here may have unintended side effects; consider scoping this tokenizer override to the comment-specific Markdown pipeline instead of the shared utility.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Overriding `tokenizer.code` to always return `undefined` will also disable fenced code blocks, which contradicts the PR description; consider inspecting the source to skip only indented (4-space) code blocks while preserving fenced blocks.
- If this `marked` instance is reused outside of comment rendering, globally disabling code blocks here may have unintended side effects; consider scoping this tokenizer override to the comment-specific Markdown pipeline instead of the shared utility.

## Individual Comments

### Comment 1
<location path="src/client/utils/marked.js" line_range="21-22" />
<code_context>
+// otherwise render as a code block (#855).
+marked.use({
+  tokenizer: {
+    code () {
+      return undefined
+    }
+  }
</code_context>
<issue_to_address>
**issue (bug_risk):** Overriding `tokenizer.code` with `return undefined` likely does not disable 4‑space code blocks as intended.

In marked’s extension API, returning `undefined` from a tokenizer method usually defers to the default tokenizer, so this may not actually disable indented code blocks. To reliably turn off 4-space-indented code, you’ll likely need to either transform those lines into a different token type (e.g., paragraph/text) or adjust the block rules so indented lines never reach `code()`. Please confirm the `code()` tokenizer contract for your marked version so this doesn’t silently leave the original behavior unchanged.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +21 to +22
code () {
return undefined

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk):return undefined 覆写 tokenizer.code 很可能并不能按预期禁用 4 空格代码块。

在 marked 的扩展 API 中,从 tokenizer 方法返回 undefined 通常意味着回退到默认 tokenizer,所以这可能实际上并不会禁用缩进代码块。要可靠地关闭 4 空格缩进代码,你很可能需要要么将这些行转换成其他 token 类型(例如 paragraph/text),要么调整块级规则,使得缩进行永远不会到达 code()。请确认你所使用的 marked 版本中 code() tokenizer 的约定,以避免悄悄地保持原有行为不变。

Original comment in English

issue (bug_risk): Overriding tokenizer.code with return undefined likely does not disable 4‑space code blocks as intended.

In marked’s extension API, returning undefined from a tokenizer method usually defers to the default tokenizer, so this may not actually disable indented code blocks. To reliably turn off 4-space-indented code, you’ll likely need to either transform those lines into a different token type (e.g., paragraph/text) or adjust the block rules so indented lines never reach code(). Please confirm the code() tokenizer contract for your marked version so this doesn’t silently leave the original behavior unchanged.

@imaegoo imaegoo merged commit 6be2295 into twikoojs:main May 28, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

在行首添加四个空格导致排版失效

2 participants