zhangdocs
Version:
Simple document generation tool. Dependence Node.js run.
165 lines (139 loc) • 5.75 kB
JavaScript
var fs = require("fs");
var path = require('path');
var file = require("./file");
var marked = require('marked');
var color = require('colors-cli');
var stylus = require('./stylus');
var copy = require('./copy');
var toc = require('./toc');
var nav = require('./nav');
var imgPath = require('./imgPath');
var log = console.log;
var highlight = require('highlight.js');
var ejs = require('ejs');
var autoprefixer = require('autoprefixer-stylus');
var cheerio = require('cheerio');
var menu_update = require('./menu_update');
// 创建自定义渲染器来处理Mermaid图表
var renderer = new marked.Renderer();
// 重写code方法以处理Mermaid代码块
renderer.code = function (code, lang) {
if (lang === 'mermaid') {
// 为Mermaid图表生成特殊的div容器
return '<div class="mermaid">' + code + '</div>';
} else {
// 对于其他语言,使用默认的高亮处理
if (lang && highlight.getLanguage(lang)) {
return '<pre><code class="hljs ' + lang + '">' + highlight.highlight(lang, code).value + '</code></pre>';
} else {
return '<pre><code class="hljs">' + highlight.highlightAuto(code).value + '</code></pre>';
}
}
};
// 移除自定义renderer和所有公式相关正则处理,直接用marked默认渲染
marked.setOptions({
gfm: true,
tables: true,
breaks: false,
pedantic: false,
sanitize: false,
smartLists: true,
smartypants: false,
renderer: renderer
});
var template = '';
function build(commander, changeFile) {
require('./menu_update')();
// 载入配置文件
var pkg = require(path.resolve('package.json'));
// 指定模板目录
if (commander.theme && commander.args.length > 0) template = commander.args[0];
else {
// 模板目录
template = path.dirname(__dirname) + '/theme/' + pkg.theme;
}
// 皮肤目录不存在判断
if (!file.isDir(template)) return log(color.red('\nTheme directory does not exist.!\n'))
var name = pkg.name;
var zhangdocsmd = pkg.zhangdocs.md;
var todir = process.cwd() + '/';
var html = ''; //
var markedstr; // markdown 生成HTML
var _path;
var tocHTML = '';//单页面导航静态页面
var navHTML = '';
var pathArr = file.readMDSync(todir + 'md/');
if (changeFile) pathArr = [changeFile];
pathArr.forEach(function (item, idx) {
//菜单层级
var isIndex = false
var len = item.replace(process.cwd() + '/md/', '').split('/').length;
if (path.basename(item) === zhangdocsmd[0]) len = 0;
//导航菜单生成
var navHTML = nav(zhangdocsmd, item, pathArr, len);
let mdContent = file.read(item);
// 自动把 \[ ... \] 替换为 $$ ... $$
mdContent = mdContent.replace(/\\\[([\s\S]*?)\\\]/g, function (match, formula) {
return `$$${formula}$$`;
});
// 自动把 \( ... \) 替换为 $...$
mdContent = mdContent.replace(/\\\((.+?)\\\)/g, function (match, formula) {
return `$${formula}$`;
});
markedstr = marked(mdContent);
// markdown 增强 及树菜单生成
markedstr = toc(markedstr, function (_html) {
tocHTML = _html.toc
});
item = path.normalize(item)
// 获取 md 的相对目录
item = item.replace(path.normalize(process.cwd() + '/md/'), '');
// 是否为首页判断
zhangdocsmd[0] === item ? isIndex = true : isIndex = false;
// 指定HTML路径
_path = zhangdocsmd[0] === item ?
(path.normalize(process.cwd() + '/' + item)).replace(item, "index.html") :
(path.normalize(process.cwd() + '/html/' + item)).replace('.md', ".html");
// 图片引用路径处理
markedstr = imgPath(markedstr, _path);
// ejs 模板生成HTML
html = file.ejs(template + '/layout.ejs', {
title: pkg.name,//项目工程名字
index: isIndex,//是否为首页
pkg: pkg,
relative_path: file.relativePath(_path, process.cwd()),//相对路径
menu_html: navHTML,//页面之间的超链接导航
toc_html: tocHTML,// markdown 导航
markdown_html: markedstr // markdown 生成字符串
});
file.mkdirsSync(path.dirname(_path));
//写入指定目录中
file.write(_path, html);
if (!changeFile) log(color.blue('Generates') + ' "' + item.replace('.html', ".md") + '"' + color.blue(" Success ! ") + new Date());
})
// 复制复制所有静态资源到生产目录里面
copy(template + '/source/', todir + 'static', {
filter: function (_file) {
if (!/\.(styl)$/.test(_file)) {
return true;
} else {
return false;
}
}
}, function (err, _file) {
if (err) return console.error(err);
})
// 复制 gitignore 到项目的根目录中
if (!file.exists(todir + '.gitignore')) copy(template + '/gitignore', todir, function (err, _file) {
if (err) return console.error(err);
fs.renameSync(todir + 'gitignore', todir + '.gitignore')
});
// css如果是 stylus 生成 css
stylus(template + '/source/css/', function (err, css, _filename, _extname) {
if (err) return log(err);
file.write(todir + 'static/css/' + _filename + '.css', css);
});
log();
if (!changeFile) log(color.green('Successful build static pages!'));
}
module.exports = build;