-
Notifications
You must be signed in to change notification settings - Fork 1
/
footnotes.js
209 lines (183 loc) · 5.27 KB
/
footnotes.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
const yaml = require('js-yaml');
const visit = require('unist-util-visit');
// fs = require('fs');
const footnotes = [];
// convert "in-string" to "inString"
const strToCamel = (str) => {
return str.replace(/-(.)/g, (match, chr) => chr.toUpperCase());
};
// convert "{'in-key': val}" to "{'inKey': val}"
const keysToCamel = (obj) => {
if (obj) {
const newObj = {};
Object.keys(obj).forEach((k) => {
newObj[strToCamel(k)] = obj[k];
});
return newObj;
}
return obj;
};
// render the html-tag in the footnote
const renderRef = ({ index, label, prefs }) => {
let ref = `${index}`;
return `<span class="footnote-ref flex-shrink-0 mr-2" id="ref-${ref}"><a id="use-ref-${ref}" href="#f-ref-${ref}" class="footnote-ref-link">↑ ${label}</a></span>`;
};
// transform each reference code block
const transformerRef = ({ markdownAST, index, prefs }) => {
// holds the definitios (content) of the rerference
const footnoteDefinitions = [];
visit(markdownAST, `footnoteDefinition`, (footnoteDefinition) => {
footnoteDefinitions.push(footnoteDefinition);
});
// create a list node `<ol>` that will output our reference list
let list = {
type: 'list',
data: {
hProperties: {
className: 'footnotes',
},
},
spread: true, // add p tag around content
children: [],
};
// register the inline footnotes
visit(markdownAST, `footnote`, (node) => {
footnotes.push({
type: 'footnote',
children: node.children,
// identifier set like a group so it can never clash
identifier: `:foootnote:--${node.position.start.offset}`,
offset: node.position.start.offset,
});
});
// register the footnote references
// this is where your footnote text lives
visit(markdownAST, `footnoteReference`, (node) => {
footnotes.push({
type: 'footnoteReference',
identifier: node.identifier,
offset: node.position.start.offset,
});
});
// sort footnotes
footnotes.sort((a, b) => a.offset - b.offset);
// filter by identifier
let uniqueFootnotes = footnotes.filter(
(item, i, ar) =>
ar.findIndex((fn) => fn.identifier === item.identifier) === i
);
let counter = 0;
// for each footnote after being sorted and cleaned etc
uniqueFootnotes.forEach((footnote, footnoteIndex) => {
let content = []; // store the content we're building below
let renderLinkRef = renderRef({
index: `${footnoteIndex + 1}`,
label: `${footnoteIndex + 1}`,
prefs,
}); // render the reference number links
if (footnote.type === 'footnote') {
//eg. `[^1]: My footnote`
content.push({
type: 'paragraph',
children: [...footnote.children],
});
}
if (footnote.type === 'footnoteReference') {
// eg. `This is a sentence[^1]
// Find the footnote definition matching this reference
let def = footnoteDefinitions.find(
(item) => item.identifier === footnote.identifier
);
if (def) {
// Add the footnote definition text to the content array
content.push(...def.children);
}
}
visit(markdownAST, `footnoteReference`, (node, index, parent) => {
// Find the matching footnote definition
let def = footnoteDefinitions.find(
(item) => item.identifier === node.identifier
);
counter += 1;
if (def) {
parent.children.splice(
index,
1,
{
type: 'jsx',
value: '<>',
},
{
type: 'jsx',
value: `<${prefs.referenceComponent} id={${counter}}>`,
},
...def.children[0].children,
{
type: 'jsx',
value: `</${prefs.referenceComponent}>`,
},
{
type: 'jsx',
value: '</>',
}
);
}
});
// add the back-reference
if (content.length > 0) {
// add list item
list.children.push({
type: 'listItem',
data: {
hProperties: {
className: 'mb-3 flex',
},
},
children: [
{
type: 'html',
value: renderLinkRef,
},
...content,
],
});
}
});
markdownAST.children = [].concat(
markdownAST.children.slice(0, index),
{
type: 'jsx',
value: `<div class="ref-notes">`,
},
list,
{
type: 'jsx',
value: '</div>',
},
markdownAST.children.slice(index + 1)
);
};
const transformer = (markdownAST, pluginOptions) => {
// transform backwards all refs-code blocks
for (let i = markdownAST.children.length - 1; i >= 0; --i) {
let node = markdownAST.children[i];
if (node.type === 'code' && node.lang === 'references') {
// set prefs
let prefs = {
referenceComponent: 'div',
footnoteComponent: 'li',
...keysToCamel(pluginOptions),
};
try {
let parsePrefs = yaml.load(markdownAST.children[i].value);
prefs = { ...prefs, ...keysToCamel(parsePrefs) };
} catch (e) {
console.log("Can't parse refs-configuration", e);
}
transformerRef({ markdownAST, index: i, prefs });
}
}
};
module.exports = ({ markdownAST }, pluginOptions) => {
return transformer(markdownAST, pluginOptions);
};