Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 38 additions & 53 deletions scripts/markdownPlugin.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -37,17 +39,6 @@ const renderer = new marked.Renderer();
}
});

// renderer.table = function(header, body) {
// return '<table class="table table-striped">\n'
// + '<thead>\n'
// + header
// + '</thead>\n'
// + '<tbody>\n'
// + body
// + '</tbody>\n'
// + '</table>\n';
// };

renderer.link = function (href: string, title: string, text: string) {
if ((this as any).options.sanitize) {
try {
Expand Down Expand Up @@ -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 = {};
Expand Down Expand Up @@ -128,30 +119,13 @@ function markdown2js(content: string, file: string) {
'" aria-hidden="true"><svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>';

return '<h' + level + '>' + anchor + text + '</h' + level + '>';

// return '<h' + level + '><a name="' +
// escapedText +
// '" class="anchor" href="#' +
// escapedText +
// '"><span class="header-link"></span></a>' +
// text + '</h' + level + '>';
};

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) {
Expand All @@ -172,7 +146,6 @@ function markdown2js(content: string, file: string) {
}
});

// placeholder[index] = `<iframe class="doc-iframe" width="100%" height="${setting.height || 200}px" frameBorder="0" src="/play?code=${encodeURIComponent(code)}&scope=${encodeURIComponent(setting.scope)}"></iframe>`;
if (lang === 'html') {
if (~code.indexOf('<html') || ~code.indexOf('<link')) {
return _;
Expand Down Expand Up @@ -204,8 +177,6 @@ function markdown2js(content: string, file: string) {
return placeholder[id] || '';
});

// content = global.fis ? fis.compile.partial(content, file, 'html') : content;
// + `\n\n<div class="m-t-lg b-l b-info b-3x wrapper bg-light dk">文档内容有误?欢迎大家一起来编写,文档地址:<i class="fa fa-github"></i><a href="https://github.com/baidu/amis/tree/master${file.subpath}">${file.subpath}</a>。</div>`;
info.html =
'<div class="markdown-body">' +
content.replace(
Expand All @@ -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<string> = [];

// 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};
}
};
}
Loading