Skip to content

fix(frontend): skip code spans when linking element names - #3056

Open
taekop wants to merge 2 commits into
Chainlit:mainfrom
taekop:fix/prepare-content-skip-code
Open

taekop wants to merge 2 commits into
Chainlit:mainfrom
taekop:fix/prepare-content-skip-code

Conversation

@taekop

@taekop taekop commented Sep 23, 2026

Copy link
Copy Markdown

prepareContent rewrites element names into [name](target) links on the raw message text, so names inside fenced code blocks and inline code were rewritten too, and so was content that gets wrapped as a code block via language.

It now walks the text line by line and only links names outside code: fenced blocks (``` or ~~~ at the start of a line, including behind > blockquote prefixes; an unclosed fence while streaming counts as code to the end) and inline backtick spans within a single line are left as is. When language wraps the content as code, nothing is linked. Anything not clearly code, like a stray backtick, is linked exactly as before.

I kept this as a text pass in prepareContent to keep the change small. Doing it as a remark plugin in Markdown.tsx would also cover the limitations below, but refElements would have to move to render time, and names written like Sources:source_1 get parsed by remark-directive before a plugin sees them, so it turned into a much larger change. Happy to go that way instead if you prefer.

Known limitations (same as before): fences inside list items indented 4+ spaces, indented code blocks without a fence, and inline code spans whose backticks are on different lines still get names linked.

Tests: cd frontend && pnpm test → 47 passed, 15 of them new in frontend/tests/prepareContent.spec.ts.

Fixes #3055


Summary by cubic

Fixes prepareContent corrupting element names in code blocks when linking them.

Now, names inside fenced (``` or ~~~) and single-line inline backtick code spans are left untouched, and linking is skipped entirely when content is wrapped as a code block via language. Frames are recognized behind blockquotes, and blockquote depth is handled correctly, including a bare > or no-prefix line closing the fence per CommonMark. Inline code uses CommonMark backslash escaping rules, only treating backticks backslash pairs as escaped.

Written for commit 43c6161. Summary will update on new commits.

Review in cubic

prepareContent rewrote element names into markdown links even inside
fenced and inline code blocks, corrupting code content. Skip fenced
(```/~~~) and single-line inline code spans when linking, and skip
linking entirely when content will be wrapped as a code block. Fences
are also recognized behind a blockquote prefix (>), so a quoted fenced
snippet is skipped the same way.

A fence opened behind a blockquote prefix now closes as soon as a
later line no longer carries that same prefix (shallower/different
nesting, or a lazy line with none at all), matching CommonMark: the
container ending closes the fence with it. Previously a mismatched or
missing prefix left the fence open forever, silently dropping links
from all following prose.

Fixes Chainlit#3055

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

@cubic-dev-ai cubic-dev-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.

All reported issues were addressed across 2 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread frontend/src/lib/message.ts Outdated
Comment thread frontend/src/lib/message.ts Outdated
…r code spans

Blockquote fence tracking compared the literal `> ` prefix string, so a
bare `>` line (no trailing space) inside a quoted fence closed it early
and let later code lines get linked. Compare quote depth instead.

The inline-code scanner treated any backtick preceded by a single `\`
as escaped; CommonMark only escapes it behind an odd backslash run.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

prepareContent rewrites element names inside code fences/backticks into markdown links

1 participant