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)
|
||||
}
|
||||
@@ -13,6 +13,7 @@
|
||||
"@xterm/addon-fit": "^0.11.0",
|
||||
"@xterm/xterm": "^6.0.0",
|
||||
"lucide-vue-next": "^1.0.0",
|
||||
"markdown-it": "^15.0.3",
|
||||
"nuxt": "^4.7.0",
|
||||
"vue": "^3.5.0"
|
||||
},
|
||||
|
||||
Generated
+50
@@ -17,6 +17,9 @@ importers:
|
||||
lucide-vue-next:
|
||||
specifier: ^1.0.0
|
||||
version: 1.0.0(vue@3.5.46(typescript@5.9.3))
|
||||
markdown-it:
|
||||
specifier: ^15.0.3
|
||||
version: 15.0.3
|
||||
nuxt:
|
||||
specifier: ^4.7.0
|
||||
version: 4.7.0(@babel/plugin-syntax-jsx@7.29.7(@babel/core@7.29.13(supports-color@10.2.2)))(@babel/plugin-syntax-typescript@7.29.7(@babel/core@7.29.13(supports-color@10.2.2)))(@oxc-project/types@0.160.0)(@vue/compiler-sfc@3.5.46)(acorn@8.19.0)(bare-buffer@3.7.3)(confbox@0.3.2)(db0@0.3.4)(dotenv@17.4.2)(esbuild@0.28.2)(giget@3.3.1)(lightningcss@1.33.0)(mlly@1.8.2)(rolldown@1.2.19)(rollup-plugin-visualizer@7.3.0(rolldown@1.2.19)(rollup@4.68.8))(rollup@4.68.8)(srvx@1.0.8)(supports-color@10.2.2)(terser@5.51.4)(typescript@5.9.3)(vite@8.4.0(esbuild@0.28.2)(jiti@2.7.0)(terser@5.51.4))(vue-tsc@2.2.12(typescript@5.9.3))
|
||||
@@ -1210,6 +1213,9 @@ packages:
|
||||
resolution: {integrity: sha512-fV1orZfsnPn9BaSByR/qE67rJCLJEy2Ox5bq7nJh+jquWaNh6Sfec75kJ2T6PtdGUbPQlrVoSVCEOa5SdiTQ1g==}
|
||||
engines: {node: '>=18'}
|
||||
|
||||
argparse@3.0.4:
|
||||
resolution: {integrity: sha512-fwWK3Zq22l4XJJjx5mcBHGikIk+hJqO+32dPm0Jur7+rtG5x1scOjSfdT9ZKeWHPpsya/l0HHK4tDImBkTrtvA==}
|
||||
|
||||
args-tokenizer@0.3.0:
|
||||
resolution: {integrity: sha512-xXAd7G2Mll5W8uo37GETpQ2VrE84M181Z7ugHFGQnJZ50M2mbOv0osSZ9VsSgPfJQ+LVG0prSi0th+ELMsno7Q==}
|
||||
|
||||
@@ -1550,6 +1556,10 @@ packages:
|
||||
resolution: {integrity: sha512-TWrgLOFUQTH994YUyl1yT4uyavY5nNB5muff+RtWaqNVCAK408b5ZnnbNAUEWLTCpum9w6arT70i1XdQ4UeOPA==}
|
||||
engines: {node: '>=0.12'}
|
||||
|
||||
entities@8.1.0:
|
||||
resolution: {integrity: sha512-kxL7msIffSuh9aaFAMD7rxAIuTRMAHMeBtgHW2yUdWw732ZNh4MehkF2gdjvtdmikkaIP9bFDDJOPlsvm7avrA==}
|
||||
engines: {node: '>=20.19.0'}
|
||||
|
||||
error-stack-parser-es@1.0.5:
|
||||
resolution: {integrity: sha512-5qucVt2XcuGMcEGgWI7i+yZpmpByQ8J1lHhcL7PwqCwu9FPP3VUXzT4ltHe5i2z9dePwEHcDVOAfSnHsOlCXRA==}
|
||||
|
||||
@@ -1921,6 +1931,9 @@ packages:
|
||||
resolution: {integrity: sha512-WkUDrojuJs0xkgGf2udWxa3yGBRxPtxUkB79i6aCZLRgc7PM8fZe9TosfPDcvEpQZbuFASnHYmRLBLUbmLOIIA==}
|
||||
engines: {node: '>= 12.0.0'}
|
||||
|
||||
linkify-it@6.1.0:
|
||||
resolution: {integrity: sha512-wJ/TwpSDTLepCrQoYWYIExIKg5Zchex2Nn5yk2mFnB+6PtdkHtyLx742md9csRjjOnGkKIS/RrbY7l8D6gT9Vw==}
|
||||
|
||||
listhen@1.10.1:
|
||||
resolution: {integrity: sha512-6nt/86SkqUQSLW1ofz8MxC6RhRMqOl3ONISe6qqvJ3xj09aJWQx6DhgSZpugs3PX4PXdOas/WD6A9jx6J2N19A==}
|
||||
hasBin: true
|
||||
@@ -1968,6 +1981,13 @@ packages:
|
||||
magicast@0.5.5:
|
||||
resolution: {integrity: sha512-UicdXN8zQ3JHlxVq+28afMXPr1z7WNY6+7EJnzTdQWkTAlMLF5fNCCKxJHBQwGaNGR11581EiQmQzx73+MvszA==}
|
||||
|
||||
markdown-it@15.0.3:
|
||||
resolution: {integrity: sha512-+Az7Ojz3iRGu8KYMvJ6xVhvrxP3eE+vEtJX75Ow88XBpcQNV/0lGX5ZynIIBftREo4DC2jsveAMDaeltmm8QvQ==}
|
||||
hasBin: true
|
||||
|
||||
mdurl@2.1.0:
|
||||
resolution: {integrity: sha512-1+HBaOx0zi/dQWht8rNv9MYf9qqpqL/kxI0hXImU6Y547zM6Sni8BQibt7ifgMcYtQg41ao3Ivd6cnSM86inpg==}
|
||||
|
||||
merge2@1.4.1:
|
||||
resolution: {integrity: sha512-8q7VEgMJW4J8tcfVPy8g09NcQwZdbwFEqhe/WZkoIzjn/3TGDwtOCYtXGxA3O8tPzpczCCDgv+P2P5y00ZJOOg==}
|
||||
engines: {node: '>= 8'}
|
||||
@@ -2229,6 +2249,10 @@ packages:
|
||||
resolution: {integrity: sha512-cdGef/drWFoydD1JsMzuFf8100nZl+GT+yacc2bEced5f9Rjk4z+WtFUTBu9PhOi9j/jfmBPu0mMEY4wIdAF8A==}
|
||||
engines: {node: '>= 0.6.0'}
|
||||
|
||||
punycode.js@2.3.1:
|
||||
resolution: {integrity: sha512-uxFIHU0YlHYhDQtV4R9J6a52SLx28BCjT+4ieh7IGbgwVJWO+km431c4yRlREUAsAmt/uMjQUyQHNEPf0M39CA==}
|
||||
engines: {node: '>=6'}
|
||||
|
||||
quansync@0.2.11:
|
||||
resolution: {integrity: sha512-AifT7QEbW9Nri4tAwR5M/uzpBuqfZf+zwaEM/QkzEjj7NBuFD2rBuy0K3dE+8wltbezDV7JMA0WfnCPYRSYbXA==}
|
||||
|
||||
@@ -2499,6 +2523,9 @@ packages:
|
||||
engines: {node: '>=14.17'}
|
||||
hasBin: true
|
||||
|
||||
uc.micro@3.0.0:
|
||||
resolution: {integrity: sha512-U3PppEkleoTnIfi8BozMx3yju3qc/L6SwqWo2Sw+54PX+PX0q9I+r1Um5HCmqD7n9VDX5/v3vQH/AjA6deDdtw==}
|
||||
|
||||
ufo@1.6.4:
|
||||
resolution: {integrity: sha512-JFNbkD1Svwe0KvGi8GOeLcP4kAWQ609twvCdcHxq1oSL8svv39ZuSvajcD8B+5D0eL4+s1Is2D/O6KN3qcTeRA==}
|
||||
|
||||
@@ -4186,6 +4213,8 @@ snapshots:
|
||||
- bare-buffer
|
||||
- react-native-b4a
|
||||
|
||||
argparse@3.0.4: {}
|
||||
|
||||
args-tokenizer@0.3.0: {}
|
||||
|
||||
ast-kit@2.2.0:
|
||||
@@ -4459,6 +4488,8 @@ snapshots:
|
||||
|
||||
entities@7.0.1: {}
|
||||
|
||||
entities@8.1.0: {}
|
||||
|
||||
error-stack-parser-es@1.0.5: {}
|
||||
|
||||
errx@0.2.2: {}
|
||||
@@ -4792,6 +4823,10 @@ snapshots:
|
||||
lightningcss-win32-arm64-msvc: 1.33.0
|
||||
lightningcss-win32-x64-msvc: 1.33.0
|
||||
|
||||
linkify-it@6.1.0:
|
||||
dependencies:
|
||||
uc.micro: 3.0.0
|
||||
|
||||
listhen@1.10.1(srvx@1.0.8):
|
||||
dependencies:
|
||||
'@parcel/watcher-wasm': 2.6.0
|
||||
@@ -4855,6 +4890,17 @@ snapshots:
|
||||
'@babel/types': 7.29.15
|
||||
source-map-js: 1.2.4
|
||||
|
||||
markdown-it@15.0.3:
|
||||
dependencies:
|
||||
argparse: 3.0.4
|
||||
entities: 8.1.0
|
||||
linkify-it: 6.1.0
|
||||
mdurl: 2.1.0
|
||||
punycode.js: 2.3.1
|
||||
uc.micro: 3.0.0
|
||||
|
||||
mdurl@2.1.0: {}
|
||||
|
||||
merge2@1.4.1: {}
|
||||
|
||||
microdiff@1.6.0: {}
|
||||
@@ -5303,6 +5349,8 @@ snapshots:
|
||||
|
||||
process@0.11.10: {}
|
||||
|
||||
punycode.js@2.3.1: {}
|
||||
|
||||
quansync@0.2.11: {}
|
||||
|
||||
quansync@1.0.0: {}
|
||||
@@ -5632,6 +5680,8 @@ snapshots:
|
||||
|
||||
typescript@5.9.3: {}
|
||||
|
||||
uc.micro@3.0.0: {}
|
||||
|
||||
ufo@1.6.4: {}
|
||||
|
||||
uint8array-extras@1.6.0: {}
|
||||
|
||||
Reference in new issue
Block a user