-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (34 loc) · 1.34 KB
/
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
36
37
38
39
40
41
42
43
44
45
const visit = require("unist-util-visit");
const defaults = require("./defaults");
const isLocalLink = require("./is-local-link");
const linkInnerText = require("./link-inner-text");
const mergeObjects = require("./merge-objects");
const tagAnchorWithClassName = (node, options) => {
const { url, title } = node;
const innerText = linkInnerText(node);
const titleAttribute = !!title ? title : innerText;
const shouldAddTarget = !isLocalLink(url, options.localLinkMatch);
const startElement = `<a class="${options.className}" href="${url}" title="${titleAttribute}"`;
let targetAndRel = shouldAddTarget ? ` target="${options.target}"` : "";
if (!options.allowFollowLinks) {
targetAndRel += ` rel="${options.rel}"`;
}
const endElement = `>${innerText}</a>`;
const anchorElement = startElement + targetAndRel + endElement;
return anchorElement;
};
module.exports = ({ markdownAST }, pluginOptions) => {
const linkNodes = [];
visit(markdownAST, ["link", "linkReference"], (linkNode) => {
linkNodes.push(linkNode);
});
const options = mergeObjects(defaults, pluginOptions);
for (var index = 0; index < linkNodes.length; index++) {
const node = linkNodes[index];
const html = tagAnchorWithClassName(node, options);
node.value = html;
node.type = "html";
node.children = undefined;
}
return markdownAST;
};