From e56f1f3ca602909c79dd0ddc4b755924ba849321 Mon Sep 17 00:00:00 2001 From: ranqirong Date: Thu, 8 Jan 2026 20:05:01 +0800 Subject: [PATCH 1/2] =?UTF-8?q?build:=20=20markdownPlugin=E4=B8=AD?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=9B=BF=E6=8D=A2!!!include!!!=E8=AF=AD?= =?UTF-8?q?=E6=B3=95,=E4=BB=A5=E4=BE=BFvite=E6=9E=84=E5=BB=BA=E6=97=B6?= =?UTF-8?q?=E5=B1=95=E7=A4=BAexpression=E8=A1=A8=E8=BE=BE=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/markdownPlugin.ts | 46 +++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/scripts/markdownPlugin.ts b/scripts/markdownPlugin.ts index b44f616a1b4..c6629ef1fed 100644 --- a/scripts/markdownPlugin.ts +++ b/scripts/markdownPlugin.ts @@ -1,3 +1,5 @@ +import path from 'path'; +import fs from 'fs'; import type {Plugin} from 'vite'; import * as marked from 'marked'; import * as prism from 'prismjs'; @@ -141,17 +143,7 @@ function markdown2js(content: string, file: string) { let index = 1; content = content - .replace(/\!\!\!include\s*\(([^\)]+?)\)\!\!\!/g, (_, val) => { - // todo - // const result = null; - - // if (result) { - // // 暂时不支持嵌套 include - // return result.file.getContent(); - // } - - return _; - }) + .replace(/\!\!\!include\s*\(([^\)]+?)\)\!\!\!/g, resolveInclude) .replace( /```(schema|html)(?::(.*?))?[\n|\r\n]([\s\S]*?)```/g, function (_, lang, attr, code) { @@ -221,6 +213,38 @@ function markdown2js(content: string, file: string) { return 'export default ' + JSON.stringify(info, null, 2) + ';'; } +/** + * 解析并读取 include 文件内容 + * + * @param subString 原始匹配字符串 + * @param includePath include 路径参数 + * @returns 读取到的文件内容或原始字符串 + */ +function resolveInclude(subString: string, includePath: string) { + let filepath = ''; + // 查找路径优先级: + // 1. packages 目录 + // 2. 项目根目录 + const roots = [ + path.resolve(__dirname, '../packages'), + path.resolve(__dirname, '..') + ]; + + for (const root of roots) { + const current = path.resolve(root, includePath); + if (fs.existsSync(current)) { + filepath = current; + break; + } + } + + if (filepath) { + return fs.readFileSync(filepath, 'utf8'); + } + + return subString; +} + export default function markdownPlugin(options: {} = {}): Plugin { return { name: 'markdown-as-js', From c67363a4e3b986287bf3ab67b015720d72fae93f Mon Sep 17 00:00:00 2001 From: ranqirong Date: Thu, 8 Jan 2026 20:28:41 +0800 Subject: [PATCH 2/2] =?UTF-8?q?chore:=20=E5=88=A0=E9=99=A4=E6=9C=AA?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=9A=84=E4=BB=A3=E7=A0=81=E5=92=8C=E5=8F=98?= =?UTF-8?q?=E9=87=8F,=E6=8F=90=E5=8D=87=E4=BB=A3=E7=A0=81=E5=8F=AF?= =?UTF-8?q?=E8=AF=BB=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/markdownPlugin.ts | 45 +++------------------------------------ 1 file changed, 3 insertions(+), 42 deletions(-) diff --git a/scripts/markdownPlugin.ts b/scripts/markdownPlugin.ts index c6629ef1fed..4cec8c38003 100644 --- a/scripts/markdownPlugin.ts +++ b/scripts/markdownPlugin.ts @@ -39,17 +39,6 @@ const renderer = new marked.Renderer(); } }); -// renderer.table = function(header, body) { -// return '\n' -// + '\n' -// + header -// + '\n' -// + '\n' -// + body -// + '\n' -// + '
\n'; -// }; - renderer.link = function (href: string, title: string, text: string) { if ((this as any).options.sanitize) { try { @@ -82,7 +71,7 @@ renderer.link = function (href: string, title: string, text: string) { return out; }; -function markdown2js(content: string, file: string) { +function markdown2js(content: string) { var raw = content; var m = rYml.exec(content); var info: any = {}; @@ -130,13 +119,6 @@ function markdown2js(content: string, file: string) { '" aria-hidden="true">'; return '' + anchor + text + ''; - - // return '' + - // text + ''; }; const placeholder: any = {}; @@ -164,7 +146,6 @@ function markdown2js(content: string, file: string) { } }); - // placeholder[index] = ``; if (lang === 'html') { if (~code.indexOf('文档内容有误?欢迎大家一起来编写,文档地址:${file.subpath}。`; info.html = '
' + content.replace( @@ -245,33 +224,15 @@ function resolveInclude(subString: string, includePath: string) { return subString; } -export default function markdownPlugin(options: {} = {}): Plugin { +export default function markdownPlugin(): Plugin { return { name: 'markdown-as-js', enforce: 'pre', apply: 'serve', transform(code: string, id: string) { - // if (id.endsWith('.scss') && /\/_[^\/]+\.scss$/.test(id)) { - // const markdowns: Array = []; - - // code.replace( - // /\/\*\!markdown\n([\s\S]+?)\*\//g, - // (_: string, md: string) => { - // markdowns.push(md.trim()); - // return _; - // } - // ); - - // if (markdowns.length) { - // return {code: markdown2js(markdowns.join('\n'), id) as string}; - // } - - // return null; - // } - if (!id.endsWith('.md')) return null; - return {code: markdown2js(code, id) as string}; + return {code: markdown2js(code) as string}; } }; }