| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- 'use strict';
- var whitespace = require('is-whitespace-character');
- var normalize = require('../util/normalize');
- module.exports = footnoteDefinition;
- footnoteDefinition.notInList = true;
- footnoteDefinition.notInBlock = true;
- var C_BACKSLASH = '\\';
- var C_NEWLINE = '\n';
- var C_TAB = '\t';
- var C_SPACE = ' ';
- var C_BRACKET_OPEN = '[';
- var C_BRACKET_CLOSE = ']';
- var C_CARET = '^';
- var C_COLON = ':';
- var EXPRESSION_INITIAL_TAB = /^( {4}|\t)?/gm;
- function footnoteDefinition(eat, value, silent) {
- var self = this;
- var offsets = self.offset;
- var index;
- var length;
- var subvalue;
- var now;
- var currentLine;
- var content;
- var queue;
- var subqueue;
- var character;
- var identifier;
- var add;
- var exit;
- if (!self.options.footnotes) {
- return;
- }
- index = 0;
- length = value.length;
- subvalue = '';
- now = eat.now();
- currentLine = now.line;
- while (index < length) {
- character = value.charAt(index);
- if (!whitespace(character)) {
- break;
- }
- subvalue += character;
- index++;
- }
- if (
- value.charAt(index) !== C_BRACKET_OPEN ||
- value.charAt(index + 1) !== C_CARET
- ) {
- return;
- }
- subvalue += C_BRACKET_OPEN + C_CARET;
- index = subvalue.length;
- queue = '';
- while (index < length) {
- character = value.charAt(index);
- if (character === C_BRACKET_CLOSE) {
- break;
- } else if (character === C_BACKSLASH) {
- queue += character;
- index++;
- character = value.charAt(index);
- }
- queue += character;
- index++;
- }
- if (
- !queue ||
- value.charAt(index) !== C_BRACKET_CLOSE ||
- value.charAt(index + 1) !== C_COLON
- ) {
- return;
- }
- if (silent) {
- return true;
- }
- identifier = normalize(queue);
- subvalue += queue + C_BRACKET_CLOSE + C_COLON;
- index = subvalue.length;
- while (index < length) {
- character = value.charAt(index);
- if (character !== C_TAB && character !== C_SPACE) {
- break;
- }
- subvalue += character;
- index++;
- }
- now.column += subvalue.length;
- now.offset += subvalue.length;
- queue = '';
- content = '';
- subqueue = '';
- while (index < length) {
- character = value.charAt(index);
- if (character === C_NEWLINE) {
- subqueue = character;
- index++;
- while (index < length) {
- character = value.charAt(index);
- if (character !== C_NEWLINE) {
- break;
- }
- subqueue += character;
- index++;
- }
- queue += subqueue;
- subqueue = '';
- while (index < length) {
- character = value.charAt(index);
- if (character !== C_SPACE) {
- break;
- }
- subqueue += character;
- index++;
- }
- if (subqueue.length === 0) {
- break;
- }
- queue += subqueue;
- }
- if (queue) {
- content += queue;
- queue = '';
- }
- content += character;
- index++;
- }
- subvalue += content;
- content = content.replace(EXPRESSION_INITIAL_TAB, function (line) {
- offsets[currentLine] = (offsets[currentLine] || 0) + line.length;
- currentLine++;
- return '';
- });
- add = eat(subvalue);
- exit = self.enterBlock();
- content = self.tokenizeBlock(content, now);
- exit();
- return add({
- type: 'footnoteDefinition',
- identifier: identifier,
- children: content
- });
- }
|