-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
147 lines (131 loc) · 4.25 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const hastToHTML = require(`hast-util-to-html`);
const toHAST = require(`mdast-util-to-hast`);
const visit = require("unist-util-visit");
const recursivelyFormatMarkdown = (
node,
footnoteBackRefPreviousElementDisplay,
footnoteBackRefInnerTextStartPosition
) => {
//Unfortunately for users of gatsby-external-link, it doesn't matter the order that
//the markdown is processed, that plugin only works on links still embedded within Markdown
//Because links in the footnotes would otherwise be transformed to text, I have to re-add
//the "target" and "rel" attributes. If you wanted to make these values conditional based on configuration,
//I would welcome a PR.
const hast = toHAST(node);
if (node.type === "link") {
return `<a href=${node.url} target="_blank" rel="noopener noreferrer" ${
node.title ? `title=${node.title}` : ""
}>${node.children[0].value}</a>`;
} else if (hast) {
if (hast.children && hast.children.length > 0) {
hast.children = hast.children.slice(0, hast.children.length - 1);
}
//the base object returned by toHAST (for a footnote) will be a <p> tag
//to change the style, we have to manipulate the return value
const newHast = {
type: `element`,
tagName: `p`,
properties: footnoteBackRefPreviousElementDisplay
? {
class: "footnote-paragraph",
style: `display:${footnoteBackRefPreviousElementDisplay}; ${
footnoteBackRefInnerTextStartPosition === "front"
? "margin-left: 5px;"
: ""
}`
}
: {},
children: hast.children
};
return hastToHTML(newHast, {
allowDangerousHTML: true
});
}
return (
node.value ||
(node.children &&
node.children
.map(child =>
recursivelyFormatMarkdown(
child,
footnoteBackRefPreviousElementDisplay,
footnoteBackRefInnerTextStartPosition
)
)
.join("")) ||
""
);
};
module.exports = (
{ markdownAST },
{
footnoteBackRefPreviousElementDisplay,
footnoteBackRefDisplay,
footnoteBackRefInnerText,
footnoteBackRefInnerTextStartPosition,
footnoteBackRefAnchorStyle,
useFootnoteMarkerText = false,
useCustomDivider
}
) => {
const footnoteBackrefs = [];
visit(markdownAST, `footnoteDefinition`, backrefNode => {
if (useFootnoteMarkerText) {
backrefNode.id = backrefNode.label;
}
footnoteBackrefs.push(backrefNode);
});
for (var index = 0; index < footnoteBackrefs.length; index++) {
const node = footnoteBackrefs[index];
//the content of the footnote itself
let innerText = recursivelyFormatMarkdown(
node,
footnoteBackRefPreviousElementDisplay,
footnoteBackRefInnerTextStartPosition
);
const pTag = innerText;
const anchorTag = `
<a href="#fnref-${
node.identifier
}" class="footnote-backref" style="display:${footnoteBackRefDisplay};${
footnoteBackRefAnchorStyle ? footnoteBackRefAnchorStyle : ""
}">
${footnoteBackRefInnerText ? footnoteBackRefInnerText : "↩"}
</a>
`;
const listItem = `
<li class="footnote-list-item" id="fn-${node.identifier}" ${
useFootnoteMarkerText ? `style="display:inline"` : ""
}>
${
useFootnoteMarkerText
? `<span class="footnote-marker-text" style="display: inline">${node.label}.</span>`
: ""
}
${
footnoteBackRefInnerTextStartPosition &&
footnoteBackRefInnerTextStartPosition === "front"
? anchorTag + pTag
: pTag + anchorTag
}
</li>
${
useFootnoteMarkerText && index < footnoteBackrefs.length - 1
? `<br/> <br/>`
: ""
}
`;
const openingTag = `
<div class="footnotes">
${useCustomDivider !== undefined ? useCustomDivider : `<hr/>`}
<ol ${useFootnoteMarkerText ? `style="list-style: none;"` : ``}>
`;
const closingOl = `</ol></div>`;
let html = index === 0 ? openingTag + listItem : listItem;
html = index === footnoteBackrefs.length - 1 ? html + closingOl : html;
node.type = "html";
node.children = undefined;
node.value = html;
}
return markdownAST;
};