# All files / src/lib/markdown-it/plugins markdown-it-footnotes.ts

95% Statements 209/220
82.25% Branches 51/62
100% Functions 14/14
98.43% Lines 189/192

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419

    22x

    51x
    51x

    51x
    4x

    51x

    26x

    26x
    1x

    26x

    26x
    26x

    26x

    22x

    22x

    25x

    25x

    25x

    25x

    26x

    22x
    31x
    31x

    31x
    31x
    31x
    31x
    31x
    31x

    31x
    31x

    648x
    648x

    648x

    647x
    36x

    34x
    163x
    163x
    34x

    34x
    33x
    33x
    25x

    25x
    25x
    25x
    25x

    25x
    25x
    25x
    25x

    25x
    25x
    25x
    25x

    25x
    25x
    25x

    25x
    52x

    52x
    28x

    28x

    24x

    28x

    25x
    25x

    25x
    25x
    25x

    25x
    24x

    25x

    25x
    25x
    25x
    25x
    25x

    25x
    25x
    25x

    25x

    251x
    251x

    251x
    210x
    34x

    1x
    1x

    1x

    1x
    1x
    1x
    1x

    1x

    1x
    1x

    1x

    1x
    1x
    1x

    250x
    250x

    250x

    209x
    58x
    25x

    25x
    132x
    132x
    132x
    25x

    25x
    25x

    25x
    25x

    25x
    25x

    25x
    24x
    24x
    24x

    1x

    25x
    25x

    25x
    25x

    25x
    25x
    25x

    326x
    326x
    326x

    326x

    23x
    249x
    25x
    25x
    25x
    25x

    224x
    25x
    25x
    25x

    199x
    199x

    23x
    22x

    22x
    22x

    22x
    25x
    25x
    25x

    25x
    1x

    1x
    1x
    1x

    1x
    1x
    1x
    1x

    1x
    1x
    1x

    24x
    24x

    25x
    25x
    25x
    22x

    3x

    25x
    25x
    26x
    26x
    26x

    25x
    22x

    25x
    25x

    22x
    22x

    31x
    31x
    31x
    31x

     /*
     Copyright (c) 2014-2015 Vitaly Puzrin, Alex Kocharin

     Permission is hereby granted, free of charge, to any person
     obtaining a copy of this software and associated documentation
     files (the "Software"), to deal in the Software without
     restriction, including without limitation the rights to use,
     copy, modify, merge, publish, distribute, sublicense, and/or sell
     copies of the Software, and to permit persons to whom the
     Software is furnished to do so, subject to the following
     conditions:

     The above copyright notice and this permission notice shall be
     included in all copies or substantial portions of the Software.

     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
     OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
     HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
     WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     OTHER DEALINGS IN THE SOFTWARE.
     */

    /*
     Markdown-it-footnotes plugin 3.0.2 source adapted to fit MarkBind's needs, to:
     - show a popover along with the footnote link
     - remove footnote backreferences
     - change footnote definition opening / closing renderers for easy identification in NodeProcessor

     Changes are delimited with a // CHANGE HERE comment
     */

    import MarkdownIt from 'markdown-it';
    import Renderer from 'markdown-it/lib/renderer';
    import Token from 'markdown-it/lib/token';
    import StateBlock from 'markdown-it/lib/rules_block/state_block';
    import StateInline from 'markdown-it/lib/rules_inline/state_inline';
    import StateCore from 'markdown-it/lib/rules_core/state_core';
    import { MARKBIND_FOOTNOTE_POPOVER_ID_PREFIX } from '../../../html/constants';

    // Process footnotes
    //

    ////////////////////////////////////////////////////////////////////////////////
    // Renderer partials

    function render_footnote_anchor_name(tokens: Token[], idx: number, _options: MarkdownIt.Options, env: any) {
      const n = Number(tokens[idx].meta.id + 1).toString();
      let prefix = '';

      if (typeof env.docId === 'string') {
        prefix = `-${env.docId}-`;
      }

      return prefix + n;
    }

    function render_footnote_caption(tokens: Token[], idx: number) {
      let n = Number(tokens[idx].meta.id + 1).toString();

      if (tokens[idx].meta.subId > 0) {
        n += `:${tokens[idx].meta.subId}`;
      }

      return `[${n}]`;
    }

    function render_footnote_ref(tokens: Token[], idx: number, options: MarkdownIt.Options, env: any, slf: Renderer) {
      const id = slf.rules.footnote_anchor_name!(tokens, idx, options, env, slf);
      const caption = slf.rules.footnote_caption!(tokens, idx, options, env, slf);

      // CHANGE here
      // Old code:
      // return '<sup class="footnote-ref"><a href="#fn' + id + '" id="fnref' + refid + '">' + caption + '</a></sup>';
      // Additions: <trigger> for footnote popover and aria-describedby label
      return `<trigger for="${MARKBIND_FOOTNOTE_POPOVER_ID_PREFIX}fn${id}">`
        + `<sup class="footnote-ref"><a aria-describedby="footnote-label" href="#fn${id}">`
        + caption
        + '</a></sup></trigger>';
    }

    function render_footnote_block_open() {
      // CHANGE HERE - change this into something easily recognisable and combinable by {@link NodeProcessor}
      // Original:
      /*return (options.xhtmlOut ? '<hr class="footnotes-sep" />\n' : '<hr class="footnotes-sep">\n') +
        '<section class="footnotes">\n' +
        '<ol class="footnotes-list">\n';*/

      return '<mb-temp-footnotes>\n';
    }

    function render_footnote_block_close() {
      // CHANGE HERE - change this into something easily recognisable and combinable by {@link NodeProcessor}
      // Original: return '</ol>\n</section>\n';
      return '</mb-temp-footnotes>\n';
    }

    function render_footnote_open(tokens: Token[], idx: number, options: MarkdownIt.Options, env: any, slf: Renderer) {
      let id = slf.rules.footnote_anchor_name!(tokens, idx, options, env, slf);

      Iif (tokens[idx].meta.subId > 0) {
        id += `:${tokens[idx].meta.subId}`;
      }

      return `<li id="fn${id}" class="footnote-item">`;
    }

    function render_footnote_close() {
      return '</li>\n';
    }

    function render_footnote_anchor() {
      // CHANGE HERE
      // Below line adds backreferences, but doesn't work well with panels, so disabled for now.
      // Old code:
      // /* ↩ with escape code to prevent display as Apple Emoji on iOS */
      // return ' <a href="#fnref' + id + '" class="footnote-backref">\u21a9\uFE0E</a>';

      return '';
    }

    export function footnotePlugin(md: MarkdownIt): void {
      const parseLinkLabel = md.helpers.parseLinkLabel;
      const isSpace = md.utils.isSpace;

      md.renderer.rules.footnote_ref          = render_footnote_ref;
      md.renderer.rules.footnote_block_open   = render_footnote_block_open;
      md.renderer.rules.footnote_block_close  = render_footnote_block_close;
      md.renderer.rules.footnote_open         = render_footnote_open;
      md.renderer.rules.footnote_close        = render_footnote_close;
      md.renderer.rules.footnote_anchor       = render_footnote_anchor;

      // helpers (only used in other rules, no tokens are attached to those)
      md.renderer.rules.footnote_caption      = render_footnote_caption;
      md.renderer.rules.footnote_anchor_name  = render_footnote_anchor_name;

      // Process footnote block definition
      function footnote_def(state: StateBlock, startLine: number, endLine: number, silent: boolean): boolean {
        let pos;
        let label;
        let token;
        let ch;
        const start = state.bMarks[startLine] + state.tShift[startLine];
        const 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++) {
          Iif (state.src.charCodeAt(pos) === 0x20) { return false; }
          if (state.src.charCodeAt(pos) === 0x5D /* ] */) {
            break;
          }
        }

        if (pos === start + 2) { return false; } // no empty footnote labels
        Iif (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);

        const oldBMark = state.bMarks[startLine];
        const oldTShift = state.tShift[startLine];
        const oldSCount = state.sCount[startLine];
        const oldParentType = state.parentType;

        const posAfterColon = pos;
        const initial = state.sCount[startLine] + pos - (state.bMarks[startLine] + state.tShift[startLine]);
        let offset = initial;

        while (pos < max) {
          ch = state.src.charCodeAt(pos);

          if (isSpace(ch)) {
            Iif (ch === 0x09) {
              offset += 4 - offset % 4;
            } else {
              offset++;
            }
          } else {
            break;
          }

          pos++;
        }

        state.tShift[startLine] = pos - posAfterColon;
        state.sCount[startLine] = offset - initial;

        state.bMarks[startLine] = posAfterColon;
        state.blkIndent += 4;
        state.parentType = 'footnote' as any; // Note find a better fix for this.

        if (state.sCount[startLine] < state.blkIndent) {
          state.sCount[startLine] += state.blkIndent;
        }

        state.md.block.tokenize(state, startLine, endLine);

        state.parentType = oldParentType;
        state.blkIndent -= 4;
        state.tShift[startLine] = oldTShift;
        state.sCount[startLine] = oldSCount;
        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: StateInline, silent: boolean): boolean {
        let labelStart;
        let labelEnd;
        let footnoteId;
        let token;
        let tokens: Token[];
        const max = state.posMax;
        const 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
        Iif (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.md.inline.parse(
            state.src.slice(labelStart, labelEnd),
            state.md,
            state.env,
            tokens = []
          );

          token      = state.push('footnote_ref', '', 0);
          token.meta = { id: footnoteId };

          state.env.footnotes.list[footnoteId] = {
            content: state.src.slice(labelStart, labelEnd),
            tokens: tokens
          };
        }

        state.pos = labelEnd + 1;
        state.posMax = max;
        return true;
      }

      // Process footnote references ([^...])
      function footnote_ref(state: StateInline, silent: boolean): boolean {
        let label;
        let pos;
        let footnoteId;
        let footnoteSubId;
        let token;
        const max = state.posMax;
        const 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; }
        Iif (state.src.charCodeAt(start + 1) !== 0x5E/* ^ */) { return false; }

        for (pos = start + 2; pos < max; pos++) {
          Iif (state.src.charCodeAt(pos) === 0x20) { return false; }
          Iif (state.src.charCodeAt(pos) === 0x0A) { return false; }
          if (state.src.charCodeAt(pos) === 0x5D /* ] */) {
            break;
          }
        }

        Iif (pos === start + 2 || pos >= max) { return false; } // no empty footnote labels
        pos++;

        label = state.src.slice(start + 2, pos - 1);
        Iif (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, label };
        }

        state.pos = pos;
        state.posMax = max;
        return true;
      }

      // Glue footnote tokens to end of token stream
      function footnote_tail(state: StateCore): void {
        let i, l, j, t, lastParagraph, list, token, tokens: Token[], current: Token[] = [], currentLabel: string = '';
        let insideRef = false;
        const refTokens: { [key: string]: Token[] } = {};

        if (!state.env.footnotes) { return; }

        state.tokens = state.tokens.filter((tok) => {
          if (tok.type === 'footnote_reference_open') {
            insideRef = true;
            current = [];
            currentLabel = tok.meta.label;
            return false;
          }
          if (tok.type === 'footnote_reference_close') {
            insideRef = false;
            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, label: list[i].label };
          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  = list[i].content;
            tokens.push(token);

            token          = new state.Token('paragraph_close', 'p', -1);
            token.block    = true;
            tokens.push(token);

          } else if (list[i].label && refTokens[`:${list[i].label}`]) {
            tokens = refTokens[`:${list[i].label}`];
          } else E{
            tokens = [];
          }

          state.tokens = state.tokens.concat(tokens);
          const lastToken = state.tokens[state.tokens.length - 1];
          if (lastToken && lastToken.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, label: list[i].label };
            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);
    }