diff --git a/scripts/markdownPlugin.ts b/scripts/markdownPlugin.ts
index b44f616a1b4..4cec8c38003 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';
@@ -37,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 {
@@ -80,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 = {};
@@ -128,30 +119,13 @@ function markdown2js(content: string, file: string) {
'" aria-hidden="true">';
return '' + anchor + text + '';
-
- // return '' +
- // text + '';
};
const placeholder: any = {};
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) {
@@ -172,7 +146,6 @@ function markdown2js(content: string, file: string) {
}
});
- // placeholder[index] = ``;
if (lang === 'html') {
if (~code.indexOf('文档内容有误?欢迎大家一起来编写,文档地址:${file.subpath}。`;
info.html =
'' +
content.replace(
@@ -221,33 +192,47 @@ function markdown2js(content: string, file: string) {
return 'export default ' + JSON.stringify(info, null, 2) + ';';
}
-export default function markdownPlugin(options: {} = {}): Plugin {
+/**
+ * 解析并读取 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(): 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};
}
};
}