feat(web): render messages as markdown
ci / checks (push) Successful in 1m58s
ci / test (push) Successful in 1m54s
ci / chart (push) Successful in 5s
ci / web (push) Successful in 18s
ci / docker (push) Successful in 16s
ci / publish-edge-image (push) Successful in 9s
ci / publish-edge-chart (push) Successful in 3s

Messages were plain text, so headings, lists, code, tables, and links
from people and the agent showed as literal markup. Render them with
markdown-it, with raw HTML off and unsafe link schemes blocked.
External links open in a new tab, and @rhema stays highlighted.

Code, tables, quotes, and images get styles that fit the message body.
This commit is contained in:
vhaudiquet committed 2026-11-28 22:23:41 +01:00
1 parent d4aac727ad
commit a8f7e74ba0
5 files changed
+235 -20

No files matched your search

+41
View File
@@ -0,0 +1,41 @@
import MarkdownIt from 'markdown-it'
// Messages come from other people and the model, so raw HTML stays off
// and unsafe link targets stay unlinked.
const md = new MarkdownIt({
html: false,
linkify: true,
breaks: true,
typographer: false,
})
const renderText = md.renderer.rules.text
md.renderer.rules.text = (tokens, idx, options, env, self) => {
const token = tokens[idx]
if (!token) return ''
const escaped = renderText
? renderText(tokens, idx, options, env, self)
: md.utils.escapeHtml(token.content)
return escaped.replace(/@rhema\b/gi, (mention: string) => `<span class="mention">${mention}</span>`)
}
const renderLinkOpen = md.renderer.rules.link_open
md.renderer.rules.link_open = (tokens, idx, options, env, self) => {
const token = tokens[idx]
if (token) {
const href = String(token.attrGet('href') ?? '')
if (/^https?:\/\//i.test(href)) {
token.attrSet('target', '_blank')
token.attrSet('rel', 'noopener noreferrer')
}
}
return renderLinkOpen
? renderLinkOpen(tokens, idx, options, env, self)
: self.renderToken(tokens, idx, options)
}
export function renderMarkdown(text: string): string {
return md.render(text)
}