From d4c1ad54fc84571bf514949653b5d7215710a682 Mon Sep 17 00:00:00 2001 From: Tim <135014430+nagisa77@users.noreply.github.com> Date: Thu, 31 Jul 2025 20:03:23 +0800 Subject: [PATCH] Add mention link rendering in Markdown --- open-isle-cli/src/utils/markdown.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/open-isle-cli/src/utils/markdown.js b/open-isle-cli/src/utils/markdown.js index 227c751ff..a3ed32ec7 100644 --- a/open-isle-cli/src/utils/markdown.js +++ b/open-isle-cli/src/utils/markdown.js @@ -3,6 +3,30 @@ import hljs from 'highlight.js' import 'highlight.js/styles/github.css' import { toast } from '../main' +function mentionPlugin(md) { + const mentionReg = /^@\[([^\]]+)\]/ + function mention(state, silent) { + const pos = state.pos + if (state.src.charCodeAt(pos) !== 0x40) return false + const match = mentionReg.exec(state.src.slice(pos)) + if (!match) return false + if (!silent) { + const tokenOpen = state.push('link_open', 'a', 1) + tokenOpen.attrs = [ + ['href', `/users/${match[1]}`], + ['target', '_blank'], + ['class', 'mention-link'] + ] + const text = state.push('text', '', 0) + text.content = `@${match[1]}` + state.push('link_close', 'a', -1) + } + state.pos += match[0].length + return true + } + md.inline.ruler.before('emphasis', 'mention', mention) +} + const md = new MarkdownIt({ html: false, linkify: true, @@ -18,6 +42,8 @@ const md = new MarkdownIt({ } }) +md.use(mentionPlugin) + export function renderMarkdown(text) { return md.render(text || '') }