-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
35 lines (30 loc) · 976 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const visit = require("unist-util-visit");
const plantumlEncoder = require("plantuml-encoder");
const DEFAULT_OPTIONS = {
baseUrl: "https://www.plantuml.com/plantuml/png"
};
/**
* Plugin for remark-js
*
* See details about plugin API:
* https://github.com/unifiedjs/unified#plugin
*
* You can specify the endpoint of PlantUML with the option 'baseUrl'
*
* @param {Object} pluginOptions Remark plugin options.
*/
function remarkSimplePlantumlPlugin(pluginOptions) {
const options = { ...DEFAULT_OPTIONS, ...pluginOptions };
return function transformer(syntaxTree) {
visit(syntaxTree, "code", node => {
let { lang, value, meta } = node;
if (!lang || !value || lang !== "plantuml") return;
node.type = "image";
node.url = `${options.baseUrl.replace(/\/$/, "")}/${plantumlEncoder.encode(value)}`;
node.alt = meta;
node.meta = undefined;
});
return syntaxTree;
};
}
module.exports = remarkSimplePlantumlPlugin;