prepareContent rewrites element names inside code fences/backticks into markdown links
What happens
When a message has a side element attached, Chainlit rewrites the message text before rendering: every occurrence of the element's name in the text is replaced with a markdown link [name](target). This is how reference chips work.
The problem is that this replacement ignores markdown context. If the element's name also appears inside a fenced code block or inline code span, it gets linkified there too — corrupting code that was supposed to be displayed verbatim.
Example
Attach a text file as a side element, and mention its path in the message (the natural thing for any file-producing assistant to do):
await cl.Message(
content="Transcript saved to:\n\n```\n/home/user/downloads/video_transcript.txt\n```",
elements=[cl.Text(name="video_transcript.txt", path=..., display="side")],
).send()This is what the user sees inside the code block:
[/home/user/downloads/video_transcript.txt](video_transcript.txt)The path is now a broken markdown link, rendered inside a code fence that should have shown it verbatim.
Why it matters
Assistant messages commonly report the path of a file they just created and attach that file as an element. Since the element name is typically the filename, and the filename is a substring of the reported path, the substitution fires inside the code block every time. In practice, any app that follows the "save to file, mention the path" pattern gets mangled output.
Suggested fix
Skip fenced blocks and inline code spans when doing the substitution. Since a capturing split group alternates between plain text and code segments, only the plain segments need the replaceAll:
const CODE_SPLIT_RE = /(```[\s\S]*?```|`[^`\n]*`)/g;
const segments = preparedContent.split(CODE_SPLIT_RE);
preparedContent = segments
.map((seg, i) => (i % 2 === 1 ? seg : seg.replaceAll(elementRegexp, replacer)))
.join("");The refElements/inlinedElements bookkeeping in the existing replacer works unchanged.
Environment
Chainlit 2.12.0, Python 3.12, any browser.
Source: Chainlit/chainlit