From 9209ebea4cd601d9b91c51e5ca8bc7b57d2b18e4 Mon Sep 17 00:00:00 2001 From: CH-122 <1521930938@qq.com> Date: Wed, 13 Aug 2025 15:40:40 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E9=93=BE=E6=8E=A5?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=E4=BB=A5=E6=94=AF=E6=8C=81=E5=A4=96=E9=83=A8?= =?UTF-8?q?=E9=93=BE=E6=8E=A5=E5=9C=A8=E6=96=B0=E7=AA=97=E5=8F=A3=E6=89=93?= =?UTF-8?q?=E5=BC=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend_nuxt/utils/markdown.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/frontend_nuxt/utils/markdown.js b/frontend_nuxt/utils/markdown.js index 4c2b586bd..8495ce2bb 100644 --- a/frontend_nuxt/utils/markdown.js +++ b/frontend_nuxt/utils/markdown.js @@ -50,6 +50,29 @@ function tiebaEmojiPlugin(md) { }) } +// 链接在新窗口打开 +function linkPlugin(md) { + const defaultRender = md.renderer.rules.link_open || function(tokens, idx, options, env, self) { + return self.renderToken(tokens, idx, options) + } + + md.renderer.rules.link_open = function (tokens, idx, options, env, self) { + const token = tokens[idx] + const hrefIndex = token.attrIndex('href') + + if (hrefIndex >= 0) { + const href = token.attrs[hrefIndex][1] + // 如果是外部链接,添加 target="_blank" 和 rel="noopener noreferrer" + if (href.startsWith('http://') || href.startsWith('https://')) { + token.attrPush(['target', '_blank']) + token.attrPush(['rel', 'noopener noreferrer']) + } + } + + return defaultRender(tokens, idx, options, env, self) + } +} + const md = new MarkdownIt({ html: false, linkify: true, @@ -67,6 +90,7 @@ const md = new MarkdownIt({ md.use(mentionPlugin) md.use(tiebaEmojiPlugin) +md.use(linkPlugin) // 添加链接插件 export function renderMarkdown(text) { return md.render(text || '')