1. vscode插件:Markdown Preview Enhanced
2. 在vscode内打开一个markdown文档
3. F1调出命令行,输入:Markdown Preview Enhanced: Extend Parser
4. 弹出的js文件内容替换成下面的代码:
function formatHead(markdown) { var seqNo = []; var mdArr = markdown.split(" "); if (mdArr && mdArr.length > 0) { mdArr.forEach((element, i) => { var mat = element.match(/^(#+)s+(S.*)/); if (mat && mat.length > 2) { // 找到标题 var headseq = mat[1].length; for (var j = 1; j < headseq; j++) { if (seqNo.length < j) { seqNo.push(1); } } if (seqNo.length < headseq) { seqNo.push(1); } else { seqNo[headseq - 1] = seqNo[headseq - 1] + 1; if (seqNo.length > headseq) { seqNo.splice(headseq, seqNo.length - headseq); } } mdArr[i] = mat[1] + " " + seqNo.join(".") + " " + mat[2]; } }); } return mdArr.join(" "); } module.exports = { onWillParseMarkdown: function(markdown) { return new Promise((resolve, reject)=> { markdown=formatHead(markdown); return resolve(markdown); }) }, onDidParseMarkdown: function(html) { return new Promise((resolve, reject)=> { return resolve(html) }) } }
5. markdown文档头部添加如下代码
--- export_on_save: puppeteer: true # export PDF on save ---
至此,大功告成。书写markdown文档后按保存按钮,会自动在同目录下生成一个同名的pdf文件