forked from jbt/markdown-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkdown-it-footnote.js
303 lines (246 loc) · 10.5 KB
/
markdown-it-footnote.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*! markdown-it-footnote 1.0.0 https://github.com//markdown-it/markdown-it-footnote @license MIT */(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.markdownitFootnote = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
// Process footnotes
//
'use strict';
////////////////////////////////////////////////////////////////////////////////
// Renderer partials
function _footnote_ref(tokens, idx) {
var n = Number(tokens[idx].meta.id + 1).toString();
var id = 'fnref' + n;
if (tokens[idx].meta.subId > 0) {
id += ':' + tokens[idx].meta.subId;
}
return '<sup class="footnote-ref"><a href="#fn' + n + '" id="' + id + '">[' + n + ']</a></sup>';
}
function _footnote_block_open(tokens, idx, options) {
return (options.xhtmlOut ? '<hr class="footnotes-sep" />\n' : '<hr class="footnotes-sep">\n') +
'<section class="footnotes">\n' +
'<ol class="footnotes-list">\n';
}
function _footnote_block_close() {
return '</ol>\n</section>\n';
}
function _footnote_open(tokens, idx) {
var id = Number(tokens[idx].meta.id + 1).toString();
return '<li id="fn' + id + '" class="footnote-item">';
}
function _footnote_close() {
return '</li>\n';
}
function _footnote_anchor(tokens, idx) {
var n = Number(tokens[idx].meta.id + 1).toString();
var id = 'fnref' + n;
if (tokens[idx].meta.subId > 0) {
id += ':' + tokens[idx].meta.subId;
}
return ' <a href="#' + id + '" class="footnote-backref">\u21a9</a>'; /* ↩ */
}
////////////////////////////////////////////////////////////////////////////////
module.exports = function sub_plugin(md) {
var parseLinkLabel = md.helpers.parseLinkLabel;
md.renderer.rules.footnote_ref = _footnote_ref;
md.renderer.rules.footnote_block_open = _footnote_block_open;
md.renderer.rules.footnote_block_close = _footnote_block_close;
md.renderer.rules.footnote_open = _footnote_open;
md.renderer.rules.footnote_close = _footnote_close;
md.renderer.rules.footnote_anchor = _footnote_anchor;
// Process footnote block definition
function footnote_def(state, startLine, endLine, silent) {
var oldBMark, oldTShift, oldParentType, pos, label, token,
start = state.bMarks[startLine] + state.tShift[startLine],
max = state.eMarks[startLine];
// line should be at least 5 chars - "[^x]:"
if (start + 4 > max) { return false; }
if (state.src.charCodeAt(start) !== 0x5B/* [ */) { return false; }
if (state.src.charCodeAt(start + 1) !== 0x5E/* ^ */) { return false; }
for (pos = start + 2; pos < max; pos++) {
if (state.src.charCodeAt(pos) === 0x20) { return false; }
if (state.src.charCodeAt(pos) === 0x5D /* ] */) {
break;
}
}
if (pos === start + 2) { return false; } // no empty footnote labels
if (pos + 1 >= max || state.src.charCodeAt(++pos) !== 0x3A /* : */) { return false; }
if (silent) { return true; }
pos++;
if (!state.env.footnotes) { state.env.footnotes = {}; }
if (!state.env.footnotes.refs) { state.env.footnotes.refs = {}; }
label = state.src.slice(start + 2, pos - 2);
state.env.footnotes.refs[':' + label] = -1;
token = new state.Token('footnote_reference_open', '', 1);
token.meta = { label: label };
token.level = state.level++;
state.tokens.push(token);
oldBMark = state.bMarks[startLine];
oldTShift = state.tShift[startLine];
oldParentType = state.parentType;
state.tShift[startLine] = state.skipSpaces(pos) - pos;
state.bMarks[startLine] = pos;
state.blkIndent += 4;
state.parentType = 'footnote';
if (state.tShift[startLine] < state.blkIndent) {
state.tShift[startLine] += state.blkIndent;
state.bMarks[startLine] -= state.blkIndent;
}
state.md.block.tokenize(state, startLine, endLine, true);
state.parentType = oldParentType;
state.blkIndent -= 4;
state.tShift[startLine] = oldTShift;
state.bMarks[startLine] = oldBMark;
token = new state.Token('footnote_reference_close', '', -1);
token.level = --state.level;
state.tokens.push(token);
return true;
}
// Process inline footnotes (^[...])
function footnote_inline(state, silent) {
var labelStart,
labelEnd,
footnoteId,
oldLength,
token,
max = state.posMax,
start = state.pos;
if (start + 2 >= max) { return false; }
if (state.src.charCodeAt(start) !== 0x5E/* ^ */) { return false; }
if (state.src.charCodeAt(start + 1) !== 0x5B/* [ */) { return false; }
labelStart = start + 2;
labelEnd = parseLinkLabel(state, start + 1);
// parser failed to find ']', so it's not a valid note
if (labelEnd < 0) { return false; }
// We found the end of the link, and know for a fact it's a valid link;
// so all that's left to do is to call tokenizer.
//
if (!silent) {
if (!state.env.footnotes) { state.env.footnotes = {}; }
if (!state.env.footnotes.list) { state.env.footnotes.list = []; }
footnoteId = state.env.footnotes.list.length;
state.pos = labelStart;
state.posMax = labelEnd;
token = state.push('footnote_ref', '', 0);
token.meta = { id: footnoteId };
oldLength = state.tokens.length;
state.md.inline.tokenize(state);
state.env.footnotes.list[footnoteId] = { tokens: state.tokens.splice(oldLength) };
}
state.pos = labelEnd + 1;
state.posMax = max;
return true;
}
// Process footnote references ([^...])
function footnote_ref(state, silent) {
var label,
pos,
footnoteId,
footnoteSubId,
token,
max = state.posMax,
start = state.pos;
// should be at least 4 chars - "[^x]"
if (start + 3 > max) { return false; }
if (!state.env.footnotes || !state.env.footnotes.refs) { return false; }
if (state.src.charCodeAt(start) !== 0x5B/* [ */) { return false; }
if (state.src.charCodeAt(start + 1) !== 0x5E/* ^ */) { return false; }
for (pos = start + 2; pos < max; pos++) {
if (state.src.charCodeAt(pos) === 0x20) { return false; }
if (state.src.charCodeAt(pos) === 0x0A) { return false; }
if (state.src.charCodeAt(pos) === 0x5D /* ] */) {
break;
}
}
if (pos === start + 2) { return false; } // no empty footnote labels
if (pos >= max) { return false; }
pos++;
label = state.src.slice(start + 2, pos - 1);
if (typeof state.env.footnotes.refs[':' + label] === 'undefined') { return false; }
if (!silent) {
if (!state.env.footnotes.list) { state.env.footnotes.list = []; }
if (state.env.footnotes.refs[':' + label] < 0) {
footnoteId = state.env.footnotes.list.length;
state.env.footnotes.list[footnoteId] = { label: label, count: 0 };
state.env.footnotes.refs[':' + label] = footnoteId;
} else {
footnoteId = state.env.footnotes.refs[':' + label];
}
footnoteSubId = state.env.footnotes.list[footnoteId].count;
state.env.footnotes.list[footnoteId].count++;
token = state.push('footnote_ref', '', 0);
token.meta = { id: footnoteId, subId: footnoteSubId };
}
state.pos = pos;
state.posMax = max;
return true;
}
// Glue footnote tokens to end of token stream
function footnote_tail(state) {
var i, l, j, t, lastParagraph, list, token, tokens, current, currentLabel,
insideRef = false,
refTokens = {};
if (!state.env.footnotes) { return; }
state.tokens = state.tokens.filter(function(tok) {
if (tok.type === 'footnote_reference_open') {
insideRef = true;
current = [];
currentLabel = tok.meta.label;
return false;
}
if (tok.type === 'footnote_reference_close') {
insideRef = false;
// prepend ':' to avoid conflict with Object.prototype members
refTokens[':' + currentLabel] = current;
return false;
}
if (insideRef) { current.push(tok); }
return !insideRef;
});
if (!state.env.footnotes.list) { return; }
list = state.env.footnotes.list;
token = new state.Token('footnote_block_open', '', 1);
state.tokens.push(token);
for (i = 0, l = list.length; i < l; i++) {
token = new state.Token('footnote_open', '', 1);
token.meta = { id: i };
state.tokens.push(token);
if (list[i].tokens) {
tokens = [];
token = new state.Token('paragraph_open', 'p', 1);
token.block = true;
tokens.push(token);
token = new state.Token('inline', '', 0);
token.children = list[i].tokens;
token.content = '';
tokens.push(token);
token = new state.Token('paragraph_close', 'p', -1);
token.block = true;
tokens.push(token);
} else if (list[i].label) {
tokens = refTokens[':' + list[i].label];
}
state.tokens = state.tokens.concat(tokens);
if (state.tokens[state.tokens.length - 1].type === 'paragraph_close') {
lastParagraph = state.tokens.pop();
} else {
lastParagraph = null;
}
t = list[i].count > 0 ? list[i].count : 1;
for (j = 0; j < t; j++) {
token = new state.Token('footnote_anchor', '', 0);
token.meta = { id: i, subId: j };
state.tokens.push(token);
}
if (lastParagraph) {
state.tokens.push(lastParagraph);
}
token = new state.Token('footnote_close', '', -1);
state.tokens.push(token);
}
token = new state.Token('footnote_block_close', '', -1);
state.tokens.push(token);
}
md.block.ruler.before('reference', 'footnote_def', footnote_def, { alt: [ 'paragraph', 'reference' ] });
md.inline.ruler.after('image', 'footnote_inline', footnote_inline);
md.inline.ruler.after('footnote_inline', 'footnote_ref', footnote_ref);
md.core.ruler.after('inline', 'footnote_tail', footnote_tail);
};
},{}]},{},[1])(1)
});