index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // Process footnotes
  2. //
  3. 'use strict';
  4. ////////////////////////////////////////////////////////////////////////////////
  5. // Renderer partials
  6. function render_footnote_anchor_name(tokens, idx, options, env/*, slf*/) {
  7. var n = Number(tokens[idx].meta.id + 1).toString();
  8. var prefix = '';
  9. if (typeof env.docId === 'string') {
  10. prefix = '-' + env.docId + '-';
  11. }
  12. return prefix + n;
  13. }
  14. function render_footnote_caption(tokens, idx/*, options, env, slf*/) {
  15. var n = Number(tokens[idx].meta.id + 1).toString();
  16. if (tokens[idx].meta.subId > 0) {
  17. n += ':' + tokens[idx].meta.subId;
  18. }
  19. return '[' + n + ']';
  20. }
  21. function render_footnote_ref(tokens, idx, options, env, slf) {
  22. var id = slf.rules.footnote_anchor_name(tokens, idx, options, env, slf);
  23. var caption = slf.rules.footnote_caption(tokens, idx, options, env, slf);
  24. var refid = id;
  25. if (tokens[idx].meta.subId > 0) {
  26. refid += ':' + tokens[idx].meta.subId;
  27. }
  28. return '<sup class="footnote-ref"><a href="#fn' + id + '" id="fnref' + refid + '">' + caption + '</a></sup>';
  29. }
  30. function render_footnote_block_open(tokens, idx, options) {
  31. return (options.xhtmlOut ? '<hr class="footnotes-sep" />\n' : '<hr class="footnotes-sep">\n') +
  32. '<section class="footnotes">\n' +
  33. '<ol class="footnotes-list">\n';
  34. }
  35. function render_footnote_block_close() {
  36. return '</ol>\n</section>\n';
  37. }
  38. function render_footnote_open(tokens, idx, options, env, slf) {
  39. var id = slf.rules.footnote_anchor_name(tokens, idx, options, env, slf);
  40. if (tokens[idx].meta.subId > 0) {
  41. id += ':' + tokens[idx].meta.subId;
  42. }
  43. return '<li id="fn' + id + '" class="footnote-item">';
  44. }
  45. function render_footnote_close() {
  46. return '</li>\n';
  47. }
  48. function render_footnote_anchor(tokens, idx, options, env, slf) {
  49. var id = slf.rules.footnote_anchor_name(tokens, idx, options, env, slf);
  50. if (tokens[idx].meta.subId > 0) {
  51. id += ':' + tokens[idx].meta.subId;
  52. }
  53. /* ↩ with escape code to prevent display as Apple Emoji on iOS */
  54. return ' <a href="#fnref' + id + '" class="footnote-backref">\u21a9\uFE0E</a>';
  55. }
  56. module.exports = function footnote_plugin(md) {
  57. var parseLinkLabel = md.helpers.parseLinkLabel,
  58. isSpace = md.utils.isSpace;
  59. md.renderer.rules.footnote_ref = render_footnote_ref;
  60. md.renderer.rules.footnote_block_open = render_footnote_block_open;
  61. md.renderer.rules.footnote_block_close = render_footnote_block_close;
  62. md.renderer.rules.footnote_open = render_footnote_open;
  63. md.renderer.rules.footnote_close = render_footnote_close;
  64. md.renderer.rules.footnote_anchor = render_footnote_anchor;
  65. // helpers (only used in other rules, no tokens are attached to those)
  66. md.renderer.rules.footnote_caption = render_footnote_caption;
  67. md.renderer.rules.footnote_anchor_name = render_footnote_anchor_name;
  68. // Process footnote block definition
  69. function footnote_def(state, startLine, endLine, silent) {
  70. var oldBMark, oldTShift, oldSCount, oldParentType, pos, label, token,
  71. initial, offset, ch, posAfterColon,
  72. start = state.bMarks[startLine] + state.tShift[startLine],
  73. max = state.eMarks[startLine];
  74. // line should be at least 5 chars - "[^x]:"
  75. if (start + 4 > max) { return false; }
  76. if (state.src.charCodeAt(start) !== 0x5B/* [ */) { return false; }
  77. if (state.src.charCodeAt(start + 1) !== 0x5E/* ^ */) { return false; }
  78. for (pos = start + 2; pos < max; pos++) {
  79. if (state.src.charCodeAt(pos) === 0x20) { return false; }
  80. if (state.src.charCodeAt(pos) === 0x5D /* ] */) {
  81. break;
  82. }
  83. }
  84. if (pos === start + 2) { return false; } // no empty footnote labels
  85. if (pos + 1 >= max || state.src.charCodeAt(++pos) !== 0x3A /* : */) { return false; }
  86. if (silent) { return true; }
  87. pos++;
  88. if (!state.env.footnotes) { state.env.footnotes = {}; }
  89. if (!state.env.footnotes.refs) { state.env.footnotes.refs = {}; }
  90. label = state.src.slice(start + 2, pos - 2);
  91. state.env.footnotes.refs[':' + label] = -1;
  92. token = new state.Token('footnote_reference_open', '', 1);
  93. token.meta = { label: label };
  94. token.level = state.level++;
  95. state.tokens.push(token);
  96. oldBMark = state.bMarks[startLine];
  97. oldTShift = state.tShift[startLine];
  98. oldSCount = state.sCount[startLine];
  99. oldParentType = state.parentType;
  100. posAfterColon = pos;
  101. initial = offset = state.sCount[startLine] + pos - (state.bMarks[startLine] + state.tShift[startLine]);
  102. while (pos < max) {
  103. ch = state.src.charCodeAt(pos);
  104. if (isSpace(ch)) {
  105. if (ch === 0x09) {
  106. offset += 4 - offset % 4;
  107. } else {
  108. offset++;
  109. }
  110. } else {
  111. break;
  112. }
  113. pos++;
  114. }
  115. state.tShift[startLine] = pos - posAfterColon;
  116. state.sCount[startLine] = offset - initial;
  117. state.bMarks[startLine] = posAfterColon;
  118. state.blkIndent += 4;
  119. state.parentType = 'footnote';
  120. if (state.sCount[startLine] < state.blkIndent) {
  121. state.sCount[startLine] += state.blkIndent;
  122. }
  123. state.md.block.tokenize(state, startLine, endLine, true);
  124. state.parentType = oldParentType;
  125. state.blkIndent -= 4;
  126. state.tShift[startLine] = oldTShift;
  127. state.sCount[startLine] = oldSCount;
  128. state.bMarks[startLine] = oldBMark;
  129. token = new state.Token('footnote_reference_close', '', -1);
  130. token.level = --state.level;
  131. state.tokens.push(token);
  132. return true;
  133. }
  134. // Process inline footnotes (^[...])
  135. function footnote_inline(state, silent) {
  136. var labelStart,
  137. labelEnd,
  138. footnoteId,
  139. token,
  140. tokens,
  141. max = state.posMax,
  142. start = state.pos;
  143. if (start + 2 >= max) { return false; }
  144. if (state.src.charCodeAt(start) !== 0x5E/* ^ */) { return false; }
  145. if (state.src.charCodeAt(start + 1) !== 0x5B/* [ */) { return false; }
  146. labelStart = start + 2;
  147. labelEnd = parseLinkLabel(state, start + 1);
  148. // parser failed to find ']', so it's not a valid note
  149. if (labelEnd < 0) { return false; }
  150. // We found the end of the link, and know for a fact it's a valid link;
  151. // so all that's left to do is to call tokenizer.
  152. //
  153. if (!silent) {
  154. if (!state.env.footnotes) { state.env.footnotes = {}; }
  155. if (!state.env.footnotes.list) { state.env.footnotes.list = []; }
  156. footnoteId = state.env.footnotes.list.length;
  157. state.md.inline.parse(
  158. state.src.slice(labelStart, labelEnd),
  159. state.md,
  160. state.env,
  161. tokens = []
  162. );
  163. token = state.push('footnote_ref', '', 0);
  164. token.meta = { id: footnoteId };
  165. state.env.footnotes.list[footnoteId] = {
  166. content: state.src.slice(labelStart, labelEnd),
  167. tokens: tokens
  168. };
  169. }
  170. state.pos = labelEnd + 1;
  171. state.posMax = max;
  172. return true;
  173. }
  174. // Process footnote references ([^...])
  175. function footnote_ref(state, silent) {
  176. var label,
  177. pos,
  178. footnoteId,
  179. footnoteSubId,
  180. token,
  181. max = state.posMax,
  182. start = state.pos;
  183. // should be at least 4 chars - "[^x]"
  184. if (start + 3 > max) { return false; }
  185. if (!state.env.footnotes || !state.env.footnotes.refs) { return false; }
  186. if (state.src.charCodeAt(start) !== 0x5B/* [ */) { return false; }
  187. if (state.src.charCodeAt(start + 1) !== 0x5E/* ^ */) { return false; }
  188. for (pos = start + 2; pos < max; pos++) {
  189. if (state.src.charCodeAt(pos) === 0x20) { return false; }
  190. if (state.src.charCodeAt(pos) === 0x0A) { return false; }
  191. if (state.src.charCodeAt(pos) === 0x5D /* ] */) {
  192. break;
  193. }
  194. }
  195. if (pos === start + 2) { return false; } // no empty footnote labels
  196. if (pos >= max) { return false; }
  197. pos++;
  198. label = state.src.slice(start + 2, pos - 1);
  199. if (typeof state.env.footnotes.refs[':' + label] === 'undefined') { return false; }
  200. if (!silent) {
  201. if (!state.env.footnotes.list) { state.env.footnotes.list = []; }
  202. if (state.env.footnotes.refs[':' + label] < 0) {
  203. footnoteId = state.env.footnotes.list.length;
  204. state.env.footnotes.list[footnoteId] = { label: label, count: 0 };
  205. state.env.footnotes.refs[':' + label] = footnoteId;
  206. } else {
  207. footnoteId = state.env.footnotes.refs[':' + label];
  208. }
  209. footnoteSubId = state.env.footnotes.list[footnoteId].count;
  210. state.env.footnotes.list[footnoteId].count++;
  211. token = state.push('footnote_ref', '', 0);
  212. token.meta = { id: footnoteId, subId: footnoteSubId, label: label };
  213. }
  214. state.pos = pos;
  215. state.posMax = max;
  216. return true;
  217. }
  218. // Glue footnote tokens to end of token stream
  219. function footnote_tail(state) {
  220. var i, l, j, t, lastParagraph, list, token, tokens, current, currentLabel,
  221. insideRef = false,
  222. refTokens = {};
  223. if (!state.env.footnotes) { return; }
  224. state.tokens = state.tokens.filter(function (tok) {
  225. if (tok.type === 'footnote_reference_open') {
  226. insideRef = true;
  227. current = [];
  228. currentLabel = tok.meta.label;
  229. return false;
  230. }
  231. if (tok.type === 'footnote_reference_close') {
  232. insideRef = false;
  233. // prepend ':' to avoid conflict with Object.prototype members
  234. refTokens[':' + currentLabel] = current;
  235. return false;
  236. }
  237. if (insideRef) { current.push(tok); }
  238. return !insideRef;
  239. });
  240. if (!state.env.footnotes.list) { return; }
  241. list = state.env.footnotes.list;
  242. token = new state.Token('footnote_block_open', '', 1);
  243. state.tokens.push(token);
  244. for (i = 0, l = list.length; i < l; i++) {
  245. token = new state.Token('footnote_open', '', 1);
  246. token.meta = { id: i, label: list[i].label };
  247. state.tokens.push(token);
  248. if (list[i].tokens) {
  249. tokens = [];
  250. token = new state.Token('paragraph_open', 'p', 1);
  251. token.block = true;
  252. tokens.push(token);
  253. token = new state.Token('inline', '', 0);
  254. token.children = list[i].tokens;
  255. token.content = list[i].content;
  256. tokens.push(token);
  257. token = new state.Token('paragraph_close', 'p', -1);
  258. token.block = true;
  259. tokens.push(token);
  260. } else if (list[i].label) {
  261. tokens = refTokens[':' + list[i].label];
  262. }
  263. if (tokens) state.tokens = state.tokens.concat(tokens);
  264. if (state.tokens[state.tokens.length - 1].type === 'paragraph_close') {
  265. lastParagraph = state.tokens.pop();
  266. } else {
  267. lastParagraph = null;
  268. }
  269. t = list[i].count > 0 ? list[i].count : 1;
  270. for (j = 0; j < t; j++) {
  271. token = new state.Token('footnote_anchor', '', 0);
  272. token.meta = { id: i, subId: j, label: list[i].label };
  273. state.tokens.push(token);
  274. }
  275. if (lastParagraph) {
  276. state.tokens.push(lastParagraph);
  277. }
  278. token = new state.Token('footnote_close', '', -1);
  279. state.tokens.push(token);
  280. }
  281. token = new state.Token('footnote_block_close', '', -1);
  282. state.tokens.push(token);
  283. }
  284. md.block.ruler.before('reference', 'footnote_def', footnote_def, { alt: [ 'paragraph', 'reference' ] });
  285. md.inline.ruler.after('image', 'footnote_inline', footnote_inline);
  286. md.inline.ruler.after('footnote_inline', 'footnote_ref', footnote_ref);
  287. md.core.ruler.after('inline', 'footnote_tail', footnote_tail);
  288. };