UNPKG

@webdoc/plugin-markdown

Version:
85 lines (80 loc) 2.25 kB
"use strict"; const env = global.Webdoc.env; const config = env["plugin/markdown"] || {}; const defaultTags = ["author", "brief", "description", "params", "returns"]; let tags = []; let excludeTags = []; function shouldProcessString(tagName, text) { let shouldProcess = true; if ((tagName === "author" || tagName === "see") && !text.includes("[")) { shouldProcess = false; } return shouldProcess; } const hljs = require("highlight.js"); const renderer = require("markdown-it")({ breaks: !!config.hardwrap, html: true, highlight: function (str, lang) { if (lang === "mermaid") { try { return "<div class=\"mermaid\">\n" + str + "\n</div>"; } catch (__) {} } else if (lang && hljs.getLanguage(lang)) { try { return "<pre class=\"hljs\"><code>" + hljs.highlight(str, { language: lang, ignoreIllegals: true }).value + "</code></pre>"; } catch (__) {} } return "<pre class=\"hljs\"><code>" + str + "</code></pre>"; } }); function process(doclet) { tags.forEach(tag => { if (!doclet[tag]) { return; } if (typeof doclet[tag] === "string" && shouldProcessString(tag, doclet[tag])) { doclet[tag] = renderer.render(doclet[tag]).replace(/\s+$/, "").replace(/&#39;/g, "'"); } else if (Array.isArray(doclet[tag])) { doclet[tag].forEach((value, index, original) => { if (typeof value === "object") { process(value); return; } const inner = {}; inner[tag] = value; process(inner); original[index] = inner[tag]; }); } }); } if (config.tags) { tags = config.tags.slice(); } if (config.excludeTags) { excludeTags = config.excludeTags.slice(); } defaultTags.forEach(tag => { if (!excludeTags.includes(tag) && !tags.includes(tag)) { tags.push(tag); } }); function convertMarkdownToHTML(doc) { process(doc); for (let i = 0; i < doc.members.length; i++) { convertMarkdownToHTML(doc.members[i]); } } function main() { global.Webdoc.Parser.installPlugin({ id: "@webdoc/plugin-markdown", onLoad() { global.Webdoc.Parser.installDoctreeMod("@webdoc/plugins-markdown", 400, convertMarkdownToHTML); } }); } main();