forked from vhaudiquet/rhema-together
feat(web): render messages as markdown
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:
1 parent
d4aac727ad
commit
a8f7e74ba0
5 files changed
+235
-20
No files matched your search
+139
-1
@@ -1158,10 +1158,148 @@ button {
|
||||
.msg-text {
|
||||
font-size: var(--fs-ui);
|
||||
line-height: var(--lh-body);
|
||||
white-space: pre-wrap;
|
||||
white-space: normal;
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
|
||||
.msg-text > :first-child {
|
||||
margin-top: 0;
|
||||
}
|
||||
|
||||
.msg-text > :last-child {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
.msg-text p,
|
||||
.msg-text ul,
|
||||
.msg-text ol,
|
||||
.msg-text pre,
|
||||
.msg-text blockquote,
|
||||
.msg-text table {
|
||||
margin: 0 0 var(--sp-2);
|
||||
}
|
||||
|
||||
.msg-text h1,
|
||||
.msg-text h2,
|
||||
.msg-text h3,
|
||||
.msg-text h4,
|
||||
.msg-text h5,
|
||||
.msg-text h6 {
|
||||
margin: var(--sp-3) 0 var(--sp-1);
|
||||
font-weight: 600;
|
||||
line-height: var(--lh-heading);
|
||||
}
|
||||
|
||||
.msg-text h1 {
|
||||
font-size: var(--fs-title);
|
||||
}
|
||||
|
||||
.msg-text h2 {
|
||||
font-size: var(--fs-section);
|
||||
}
|
||||
|
||||
.msg-text h3,
|
||||
.msg-text h4,
|
||||
.msg-text h5,
|
||||
.msg-text h6 {
|
||||
font-size: var(--fs-ui);
|
||||
}
|
||||
|
||||
.msg-text ul,
|
||||
.msg-text ol {
|
||||
padding-left: var(--sp-5);
|
||||
}
|
||||
|
||||
.msg-text li {
|
||||
margin: var(--sp-1) 0;
|
||||
}
|
||||
|
||||
.msg-text li > p {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.msg-text blockquote {
|
||||
padding-left: var(--sp-3);
|
||||
border-left: 2px solid var(--border-strong);
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.msg-text blockquote > :last-child {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
.msg-text hr {
|
||||
margin: var(--sp-3) 0;
|
||||
border: 0;
|
||||
border-top: 1px solid var(--border);
|
||||
}
|
||||
|
||||
.msg-text a {
|
||||
color: var(--accent);
|
||||
text-decoration: underline;
|
||||
text-underline-offset: 2px;
|
||||
}
|
||||
|
||||
.msg-text a:hover {
|
||||
text-decoration-thickness: 2px;
|
||||
}
|
||||
|
||||
.msg-text code {
|
||||
padding: 0 var(--sp-1);
|
||||
border-radius: 4px;
|
||||
background: var(--surface-2);
|
||||
font-family: var(--font-mono);
|
||||
font-size: var(--fs-code);
|
||||
}
|
||||
|
||||
.msg-text pre {
|
||||
padding: var(--sp-2) var(--sp-3);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: var(--r-control);
|
||||
background: var(--surface-2);
|
||||
overflow-x: auto;
|
||||
overflow-wrap: normal;
|
||||
scrollbar-width: thin;
|
||||
}
|
||||
|
||||
.msg-text pre code {
|
||||
padding: 0;
|
||||
border-radius: 0;
|
||||
background: transparent;
|
||||
font-size: var(--fs-code);
|
||||
line-height: var(--lh-body);
|
||||
white-space: pre;
|
||||
}
|
||||
|
||||
.msg-text img {
|
||||
display: block;
|
||||
max-width: 100%;
|
||||
height: auto;
|
||||
margin: var(--sp-2) 0;
|
||||
border-radius: var(--r-control);
|
||||
}
|
||||
|
||||
.msg-text table {
|
||||
display: block;
|
||||
max-width: 100%;
|
||||
overflow-x: auto;
|
||||
border-collapse: collapse;
|
||||
font-size: var(--fs-meta);
|
||||
}
|
||||
|
||||
.msg-text th,
|
||||
.msg-text td {
|
||||
padding: var(--sp-1) var(--sp-2);
|
||||
border: 1px solid var(--border);
|
||||
text-align: left;
|
||||
vertical-align: top;
|
||||
}
|
||||
|
||||
.msg-text th {
|
||||
background: var(--surface-2);
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.mention {
|
||||
color: var(--accent);
|
||||
font-weight: 600;
|
||||
|
||||
@@ -3,16 +3,11 @@ import { computed } from 'vue'
|
||||
import type { Message } from '~/types'
|
||||
import { fullTime, relativeTime } from '~/utils/format'
|
||||
import { memberColorVar } from '~/utils/member'
|
||||
import { renderMarkdown } from '~/utils/markdown'
|
||||
|
||||
const props = defineProps<{ message: Message; color?: string | null; avatar?: string | null }>()
|
||||
|
||||
const segments = computed(() => {
|
||||
const parts = props.message.text.split(/(@rhema\b)/gi)
|
||||
return parts
|
||||
.filter((part) => part.length > 0)
|
||||
.map((part) => ({ text: part, mention: /^@rhema$/i.test(part) }))
|
||||
})
|
||||
|
||||
const html = computed(() => renderMarkdown(props.message.text))
|
||||
const style = computed(() => ({ '--member': memberColorVar(props.color) }))
|
||||
</script>
|
||||
|
||||
@@ -26,12 +21,7 @@ const style = computed(() => ({ '--member': memberColorVar(props.color) }))
|
||||
<span class="msg-agent-label">rhema</span>
|
||||
<span class="msg-time" :title="fullTime(message.created_at)">{{ relativeTime(message.created_at) }}</span>
|
||||
</div>
|
||||
<div class="msg-text">
|
||||
<template v-for="(segment, index) in segments" :key="index">
|
||||
<span v-if="segment.mention" class="mention">{{ segment.text }}</span>
|
||||
<template v-else>{{ segment.text }}</template>
|
||||
</template>
|
||||
</div>
|
||||
<div class="msg-text" v-html="html"></div>
|
||||
</div>
|
||||
|
||||
<div v-else class="msg msg-human">
|
||||
@@ -41,12 +31,7 @@ const style = computed(() => ({ '--member': memberColorVar(props.color) }))
|
||||
<span class="msg-author member-name" :style="style">{{ message.author_name }}</span>
|
||||
<span class="msg-time" :title="fullTime(message.created_at)">{{ relativeTime(message.created_at) }}</span>
|
||||
</div>
|
||||
<div class="msg-text">
|
||||
<template v-for="(segment, index) in segments" :key="index">
|
||||
<span v-if="segment.mention" class="mention">{{ segment.text }}</span>
|
||||
<template v-else>{{ segment.text }}</template>
|
||||
</template>
|
||||
</div>
|
||||
<div class="msg-text" v-html="html"></div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
@@ -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)
|
||||
}
|
||||
Reference in new issue
Block a user