@canopycanopycanopy/b-ber-parser-footnotes 3.0.8-next.61 → 3.0.8-next.97

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ //#region src/index.d.ts
2
+ declare function footnotePlugin(md: any, callback: (tokens: any[]) => void): void;
3
+ export = footnotePlugin;
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/index.ts"],"mappings":";iBAuIwB,cAAA,CACtB,EAAA,OACA,QAAA,GAAW,MAAA;AAAA"}
package/dist/index.js CHANGED
@@ -1,406 +1,372 @@
1
- "use strict";
2
-
3
- var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");
4
- var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
5
- _Object$defineProperty(exports, "__esModule", {
6
- value: true
7
- });
8
- exports.default = footnotePlugin;
9
- var _isArray = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/array/is-array"));
10
- var _slice = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/slice"));
11
- var _filter = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/filter"));
12
- var _concat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/concat"));
13
- var _isUndefined = _interopRequireDefault(require("lodash/isUndefined"));
14
- var _State = _interopRequireDefault(require("@canopycanopycanopy/b-ber-lib/State"));
15
- var _counter = _interopRequireDefault(require("./counter"));
16
- /* eslint-disable camelcase */
17
- /* eslint-disable no-param-reassign,no-plusplus */
18
-
19
- /*
20
- Modified version of markdown-it-footnote@3.0.1
21
- https://github.com//markdown-it/markdown-it-footnote
22
- MIT license
23
- */
24
-
25
- const counter = new _counter.default();
1
+ //#region \0rolldown/runtime.js
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
+ key = keys[i];
11
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
12
+ get: ((k) => from[k]).bind(null, key),
13
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
14
+ });
15
+ }
16
+ return to;
17
+ };
18
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
19
+ value: mod,
20
+ enumerable: true
21
+ }) : target, mod));
22
+ //#endregion
23
+ let _canopycanopycanopy_b_ber_lib = require("@canopycanopycanopy/b-ber-lib");
24
+ let lodash_isUndefined_js = require("lodash/isUndefined.js");
25
+ lodash_isUndefined_js = __toESM(lodash_isUndefined_js);
26
+ let crypto = require("crypto");
27
+ crypto = __toESM(crypto);
28
+ //#region src/counter.ts
29
+ var Counter = class {
30
+ page;
31
+ list;
32
+ item;
33
+ refs;
34
+ constructor() {
35
+ this.page = -1;
36
+ this.list = 1;
37
+ this.item = 1;
38
+ this.refs = [];
39
+ }
40
+ listCounter(grouped, page) {
41
+ if (page === 0) {
42
+ this.page = -1;
43
+ this.item = 1;
44
+ this.list = 1;
45
+ }
46
+ if (!grouped) return this.item;
47
+ if (page !== this.page) this.list = 1;
48
+ else this.list += 1;
49
+ return this.list;
50
+ }
51
+ listItemCounter(grouped, page) {
52
+ if (!grouped) {
53
+ const n = this.item;
54
+ this.item += 1;
55
+ return n;
56
+ }
57
+ if (page !== this.page) {
58
+ this.page = page;
59
+ this.item = 1;
60
+ } else this.item += 1;
61
+ return this.item;
62
+ }
63
+ setRef = (label) => {
64
+ const idRef = `-${label}-${crypto.default.randomBytes(8).toString("hex")}`;
65
+ this.refs.push(idRef);
66
+ return idRef;
67
+ };
68
+ getRef = () => this.refs.shift();
69
+ };
70
+ //#endregion
71
+ //#region src/index.ts
72
+ const counter = new Counter();
26
73
  function renderFootnoteAnchorName(tokens, idx, _options, env, _slf) {
27
- const n = Number(tokens[idx].meta.id + 1).toString();
28
- return typeof env.docId === 'string' ? `-${env.docId}-${n}` : '';
74
+ const n = Number(tokens[idx].meta.id + 1).toString();
75
+ return typeof env.docId === "string" ? `-${env.docId}-${n}` : "";
29
76
  }
30
77
  function renderFootnoteCaption(tokens, idx, _options, _env, _slf) {
31
- let n;
32
- if (!_State.default.config.group_footnotes) {
33
- n = counter.listItemCounter(_State.default.config.group_footnotes, _State.default.footnotes.length);
34
- } else {
35
- n = Number(tokens[idx].meta.id + 1);
36
- }
37
- return tokens[idx].meta.subId > 0 ? `${n}:${tokens[idx].meta.subId}` : n;
78
+ let n;
79
+ if (!_canopycanopycanopy_b_ber_lib.State.config.group_footnotes) n = counter.listItemCounter(_canopycanopycanopy_b_ber_lib.State.config.group_footnotes, _canopycanopycanopy_b_ber_lib.State.footnotes.length);
80
+ else n = Number(tokens[idx].meta.id + 1);
81
+ return tokens[idx].meta.subId > 0 ? `${n}:${tokens[idx].meta.subId}` : n;
38
82
  }
39
83
  function renderFootnoteRef(tokens, idx, options, env, slf) {
40
- const caption = slf.rules.footnote_caption(tokens, idx, options, env, slf);
41
- const ref = counter.getRef();
42
- return `<a epub:type="noteref" class="footnote-ref" href="notes.xhtml#fn${ref}" id="fnref${ref}">${caption}</a>`;
84
+ const caption = slf.rules.footnote_caption(tokens, idx, options, env, slf);
85
+ const ref = counter.getRef();
86
+ return `<a epub:type="noteref" class="footnote-ref" href="notes.xhtml#fn${ref}" id="fnref${ref}">${caption}</a>`;
43
87
  }
44
- function renderFootnoteBlockOpen(/* tokens, idx, options */
45
- ) {
46
- const start = counter.listCounter(_State.default.config.group_footnotes, _State.default.footnotes.length);
47
- return `<ol class="footnotes" start="${start}">`;
88
+ function renderFootnoteBlockOpen() {
89
+ return `<ol class="footnotes" start="${counter.listCounter(_canopycanopycanopy_b_ber_lib.State.config.group_footnotes, _canopycanopycanopy_b_ber_lib.State.footnotes.length)}">`;
48
90
  }
49
91
  function renderFootnoteBlockClose() {
50
- return '</ol>';
92
+ return "</ol>";
51
93
  }
52
94
  function renderFootnoteOpen(tokens, idx, _options, env, _self) {
53
- const ref = counter.setRef(tokens[idx].meta.label);
54
- const childIndex = idx + 2;
55
-
56
- // push the backlink into the parent paragraph
57
- if (tokens[childIndex]) {
58
- if (!(0, _isArray.default)(tokens[childIndex].children)) {
59
- tokens[childIndex].children = [];
60
- }
61
- tokens[childIndex].children.push({
62
- type: 'inline',
63
- attrs: [['hidden', 'hidden'], ['class', 'hidden-backlink']],
64
- tag: 'span',
65
- nesting: 1,
66
- block: false
67
- }, {
68
- type: 'inline',
69
- tag: 'a',
70
- attrs: [['href', `${env.reference}#fnref${ref}`]],
71
- nesting: 1
72
- }, {
73
- type: 'text',
74
- block: false,
75
- content: '\u21B5'
76
- }, {
77
- type: 'inline',
78
- tag: 'a',
79
- nesting: -1
80
- }, {
81
- type: 'inline',
82
- tag: 'span',
83
- nesting: -1
84
- });
85
- }
86
- return `<li class="footnote" epub:type="footnote" id="fn${ref}">`;
95
+ const ref = counter.setRef(tokens[idx].meta.label);
96
+ const childIndex = idx + 2;
97
+ if (tokens[childIndex]) {
98
+ if (!Array.isArray(tokens[childIndex].children)) tokens[childIndex].children = [];
99
+ tokens[childIndex].children.push({
100
+ type: "inline",
101
+ attrs: [["hidden", "hidden"], ["class", "hidden-backlink"]],
102
+ tag: "span",
103
+ nesting: 1,
104
+ block: false
105
+ }, {
106
+ type: "inline",
107
+ tag: "a",
108
+ attrs: [["href", `${env.reference}#fnref${ref}`]],
109
+ nesting: 1
110
+ }, {
111
+ type: "text",
112
+ block: false,
113
+ content: "↵"
114
+ }, {
115
+ type: "inline",
116
+ tag: "a",
117
+ nesting: -1
118
+ }, {
119
+ type: "inline",
120
+ tag: "span",
121
+ nesting: -1
122
+ });
123
+ }
124
+ return `<li class="footnote" epub:type="footnote" id="fn${ref}">`;
87
125
  }
88
126
  function renderFootnoteClose() {
89
- return '</li>';
127
+ return "</li>";
90
128
  }
91
- function renderFootnoteAnchor(/*tokens, idx, options,env, slf */
92
- ) {
93
- /* ↩ with escape code to prevent display as Apple Emoji on iOS */
94
- // return ' <a href="#fnref' + id + '">\u21a9\uFE0E</a>';
95
- return '';
129
+ function renderFootnoteAnchor() {
130
+ return "";
96
131
  }
97
132
  function footnotePlugin(md, callback) {
98
- const {
99
- parseLinkLabel
100
- } = md.helpers;
101
- const {
102
- isSpace
103
- } = md.utils;
104
- md.renderer.rules.footnote_ref = renderFootnoteRef;
105
- md.renderer.rules.footnote_block_open = renderFootnoteBlockOpen;
106
- md.renderer.rules.footnote_block_close = renderFootnoteBlockClose;
107
- md.renderer.rules.footnote_open = renderFootnoteOpen;
108
- md.renderer.rules.footnote_close = renderFootnoteClose;
109
- md.renderer.rules.footnote_anchor = renderFootnoteAnchor;
110
-
111
- // helpers (only used in other rules, no tokens are attached to those)
112
- md.renderer.rules.footnote_caption = renderFootnoteCaption;
113
- md.renderer.rules.footnoteAnchorName = renderFootnoteAnchorName;
114
-
115
- // Process footnote block definition
116
- function footnoteDef(state, startLine, endLine, silent) {
117
- var _context;
118
- const start = state.bMarks[startLine] + state.tShift[startLine];
119
- const max = state.eMarks[startLine];
120
- let pos;
121
- let token;
122
- let offset;
123
- let ch;
124
-
125
- // line should be at least 5 chars - "[^x]:"
126
- if (start + 4 > max) return false;
127
- if (state.src.charCodeAt(start) !== 0x5b /* [ */) return false;
128
- if (state.src.charCodeAt(start + 1) !== 0x5e /* ^ */) return false;
129
- for (pos = start + 2; pos < max; pos++) {
130
- if (state.src.charCodeAt(pos) === 0x20) return false;
131
- if (state.src.charCodeAt(pos) === 0x5d /* ] */) {
132
- break;
133
- }
134
- }
135
- if (pos === start + 2) return false; // no empty footnote labels
136
- if (pos + 1 >= max || state.src.charCodeAt(++pos) !== 0x3a /* : */) {
137
- return false;
138
- }
139
- if (silent) return true;
140
- pos++;
141
-
142
- // Footnote is found, start parsing footnote body here
143
-
144
- if (!state.env.footnotes) state.env.footnotes = {};
145
- if (!state.env.footnotes.refs) state.env.footnotes.refs = {};
146
- const label = (0, _slice.default)(_context = state.src).call(_context, start + 2, pos - 2);
147
- state.env.footnotes.refs[`:${label}`] = -1;
148
- token = new state.Token('footnote_reference_open', '', 1);
149
- token.meta = {
150
- label
151
- };
152
- token.level = state.level++;
153
- state.tokens.push(token);
154
- const oldBMark = state.bMarks[startLine];
155
- const oldTShift = state.tShift[startLine];
156
- const oldSCount = state.sCount[startLine];
157
- const oldParentType = state.parentType;
158
- const posAfterColon = pos;
159
-
160
- // eslint-disable-next-line no-multi-assign
161
- const initial = offset = state.sCount[startLine] + pos - (state.bMarks[startLine] + state.tShift[startLine]);
162
- while (pos < max) {
163
- ch = state.src.charCodeAt(pos);
164
- if (isSpace(ch)) {
165
- if (ch === 0x09) {
166
- offset += 4 - offset % 4;
167
- } else {
168
- offset++;
169
- }
170
- } else {
171
- break;
172
- }
173
- pos++;
174
- }
175
- state.tShift[startLine] = pos - posAfterColon;
176
- state.sCount[startLine] = offset - initial;
177
- state.bMarks[startLine] = posAfterColon;
178
- state.blkIndent += 4;
179
- state.parentType = 'footnote';
180
- if (state.sCount[startLine] < state.blkIndent) {
181
- state.sCount[startLine] += state.blkIndent;
182
- }
183
- state.md.block.tokenize(state, startLine, endLine, true);
184
- state.parentType = oldParentType;
185
- state.blkIndent -= 4;
186
- state.tShift[startLine] = oldTShift;
187
- state.sCount[startLine] = oldSCount;
188
- state.bMarks[startLine] = oldBMark;
189
- token = new state.Token('footnote_reference_close', '', -1);
190
- token.level = --state.level;
191
- state.tokens.push(token);
192
- return true;
193
- }
194
-
195
- // Process inline footnotes (^[...])
196
- function footnoteInline(state, silent) {
197
- const max = state.posMax;
198
- const start = state.pos;
199
- let footnoteId;
200
- let token;
201
- let tokens;
202
- if (start + 2 >= max) return false;
203
- if (state.src.charCodeAt(start) !== 0x5e /* ^ */) return false;
204
- if (state.src.charCodeAt(start + 1) !== 0x5b /* [ */) return false;
205
- const labelStart = start + 2;
206
- const labelEnd = parseLinkLabel(state, start + 1);
207
-
208
- // Failed to find ']', so it's not a valid note
209
- if (labelEnd < 0) return false;
210
-
211
- // Found the end of the link, and know for a fact it's a valid link; so
212
- // all that's left to do is to call tokenizer.
213
- if (!silent) {
214
- var _context2;
215
- if (!state.env.footnotes) state.env.footnotes = {};
216
- if (!state.env.footnotes.list) state.env.footnotes.list = [];
217
- footnoteId = state.env.footnotes.list.length;
218
- state.md.inline.parse((0, _slice.default)(_context2 = state.src).call(_context2, labelStart, labelEnd), state.md, state.env, tokens = []);
219
- token = state.push('footnote_ref', '', 0);
220
- token.meta = {
221
- id: footnoteId
222
- };
223
- state.env.footnotes.list[footnoteId] = {
224
- tokens
225
- };
226
- }
227
- state.pos = labelEnd + 1;
228
- state.posMax = max;
229
- return true;
230
- }
231
-
232
- // Process footnote references ([^...])
233
- function footnoteRef(state, silent) {
234
- var _context3;
235
- const max = state.posMax;
236
- const start = state.pos;
237
- let pos;
238
- let footnoteId;
239
- let footnoteSubId;
240
- let token;
241
-
242
- // should be at least 4 chars - "[^x]"
243
- if (start + 3 > max) return false;
244
- if (!state.env.footnotes || !state.env.footnotes.refs) return false;
245
- if (state.src.charCodeAt(start) !== 0x5b /* [ */) return false;
246
- if (state.src.charCodeAt(start + 1) !== 0x5e /* ^ */) return false;
247
- for (pos = start + 2; pos < max; pos++) {
248
- if (state.src.charCodeAt(pos) === 0x20) return false;
249
- if (state.src.charCodeAt(pos) === 0x0a) return false;
250
- if (state.src.charCodeAt(pos) === 0x5d /* ] */) break;
251
- }
252
- if (pos === start + 2) return false; // no empty footnote labels
253
- if (pos >= max) return false;
254
- pos++;
255
- const label = (0, _slice.default)(_context3 = state.src).call(_context3, start + 2, pos - 1);
256
- if ((0, _isUndefined.default)(state.env.footnotes.refs[`:${label}`])) return false;
257
- if (!silent) {
258
- if (!state.env.footnotes.list) state.env.footnotes.list = [];
259
- if (state.env.footnotes.refs[`:${label}`] < 0) {
260
- footnoteId = state.env.footnotes.list.length;
261
- state.env.footnotes.list[footnoteId] = {
262
- label,
263
- count: 0
264
- };
265
- state.env.footnotes.refs[`:${label}`] = footnoteId;
266
- } else {
267
- footnoteId = state.env.footnotes.refs[`:${label}`];
268
- }
269
- footnoteSubId = state.env.footnotes.list[footnoteId].count;
270
- state.env.footnotes.list[footnoteId].count++;
271
- token = state.push('footnote_ref', '', 0);
272
- token.meta = {
273
- id: footnoteId,
274
- subId: footnoteSubId,
275
- label
276
- };
277
- }
278
- state.pos = pos;
279
- state.posMax = max;
280
- return true;
281
- }
282
-
283
- // Glue footnote tokens to end of token stream
284
- function footnoteTail(state) {
285
- var _context4, _context6, _context7;
286
- const refTokens = {};
287
- let i;
288
- let l;
289
- let j;
290
- let t;
291
- let lastParagraph;
292
- let token;
293
- let tokens;
294
- let current;
295
- let currentLabel;
296
- let insideRef = false;
297
- let footnoteTokens = [];
298
- if (!state.env.footnotes) return;
299
- state.tokens = (0, _filter.default)(_context4 = state.tokens).call(_context4, tok => {
300
- if (tok.type === 'footnote_reference_open') {
301
- insideRef = true;
302
- current = [];
303
- currentLabel = tok.meta.label;
304
- return false;
305
- }
306
- if (tok.type === 'footnote_reference_close') {
307
- insideRef = false;
308
- // prepend ':' to avoid conflict with Object.prototype members
309
- refTokens[`:${currentLabel}`] = current;
310
- return false;
311
- }
312
- if (insideRef) current.push(tok);
313
- return !insideRef;
314
- });
315
- if (!state.env.footnotes.list) return;
316
- const {
317
- list
318
- } = state.env.footnotes;
319
- token = new state.Token('footnote_block_open', '', 1);
320
- state.tokens.push(token);
321
- for (i = 0, l = list.length; i < l; i++) {
322
- var _context5;
323
- token = new state.Token('footnote_open', '', 1);
324
- token.meta = {
325
- id: i,
326
- label: list[i].label
327
- };
328
- state.tokens.push(token);
329
- if (list[i].tokens) {
330
- tokens = [];
331
- token = new state.Token('paragraph_open', 'p', 1);
332
- token.block = true;
333
- tokens.push(token);
334
- token = new state.Token('inline', '', 0);
335
- token.children = list[i].tokens;
336
- token.content = '';
337
- tokens.push(token);
338
- token = new state.Token('paragraph_close', 'p', -1);
339
- token.block = true;
340
- tokens.push(token);
341
- } else if (list[i].label) {
342
- tokens = refTokens[`:${list[i].label}`];
343
- }
344
- state.tokens = (0, _concat.default)(_context5 = state.tokens).call(_context5, tokens);
345
- if (state.tokens[state.tokens.length - 1].type === 'paragraph_close') {
346
- lastParagraph = state.tokens.pop();
347
- } else {
348
- lastParagraph = null;
349
- }
350
- t = list[i].count > 0 ? list[i].count : 1;
351
- for (j = 0; j < t; j++) {
352
- token = new state.Token('footnote_anchor', '', 0);
353
- token.meta = {
354
- id: i,
355
- subId: j,
356
- label: list[i].label
357
- };
358
- state.tokens.push(token);
359
- }
360
- if (lastParagraph) {
361
- state.tokens.push(lastParagraph);
362
- }
363
- token = new state.Token('footnote_close', '', -1);
364
- state.tokens.push(token);
365
- }
366
- token = new state.Token('footnote_block_close', '', -1);
367
- state.tokens.push(token);
368
-
369
- // create return value for callback
370
- insideRef = false;
371
- footnoteTokens = (0, _filter.default)(_context6 = [...state.tokens]).call(_context6, a => {
372
- if (a.type === 'footnote_block_open') {
373
- insideRef = true;
374
- return true;
375
- }
376
- if (a.type === 'footnote_block_close') {
377
- insideRef = false;
378
- return true;
379
- }
380
- return insideRef;
381
- });
382
-
383
- // remove footnotes from `state.tokens`
384
- insideRef = false;
385
- state.tokens = (0, _filter.default)(_context7 = state.tokens).call(_context7, _ => {
386
- if (_.type === 'footnote_block_open') {
387
- insideRef = true;
388
- return false;
389
- }
390
- if (_.type === 'footnote_block_close') {
391
- insideRef = false;
392
- return false;
393
- }
394
- return !insideRef;
395
- });
396
-
397
- // return to MarkdownRenderer class
398
- callback(footnoteTokens);
399
- }
400
- md.block.ruler.before('reference', 'footnote_def', footnoteDef, {
401
- alt: ['paragraph', 'reference']
402
- });
403
- md.inline.ruler.after('image', 'footnote_inline', footnoteInline);
404
- md.inline.ruler.after('footnote_inline', 'footnote_ref', footnoteRef);
405
- md.core.ruler.after('inline', 'footnote_tail', footnoteTail);
406
- }
133
+ const { parseLinkLabel } = md.helpers;
134
+ const { isSpace } = md.utils;
135
+ md.renderer.rules.footnote_ref = renderFootnoteRef;
136
+ md.renderer.rules.footnote_block_open = renderFootnoteBlockOpen;
137
+ md.renderer.rules.footnote_block_close = renderFootnoteBlockClose;
138
+ md.renderer.rules.footnote_open = renderFootnoteOpen;
139
+ md.renderer.rules.footnote_close = renderFootnoteClose;
140
+ md.renderer.rules.footnote_anchor = renderFootnoteAnchor;
141
+ md.renderer.rules.footnote_caption = renderFootnoteCaption;
142
+ md.renderer.rules.footnoteAnchorName = renderFootnoteAnchorName;
143
+ function footnoteDef(state, startLine, endLine, silent) {
144
+ const start = state.bMarks[startLine] + state.tShift[startLine];
145
+ const max = state.eMarks[startLine];
146
+ let pos;
147
+ let token;
148
+ let offset;
149
+ let ch;
150
+ if (start + 4 > max) return false;
151
+ if (state.src.charCodeAt(start) !== 91) return false;
152
+ if (state.src.charCodeAt(start + 1) !== 94) return false;
153
+ for (pos = start + 2; pos < max; pos++) {
154
+ if (state.src.charCodeAt(pos) === 32) return false;
155
+ if (state.src.charCodeAt(pos) === 93) break;
156
+ }
157
+ if (pos === start + 2) return false;
158
+ if (pos + 1 >= max || state.src.charCodeAt(++pos) !== 58) return false;
159
+ if (silent) return true;
160
+ pos++;
161
+ if (!state.env.footnotes) state.env.footnotes = {};
162
+ if (!state.env.footnotes.refs) state.env.footnotes.refs = {};
163
+ const label = state.src.slice(start + 2, pos - 2);
164
+ state.env.footnotes.refs[`:${label}`] = -1;
165
+ token = new state.Token("footnote_reference_open", "", 1);
166
+ token.meta = { label };
167
+ token.level = state.level++;
168
+ state.tokens.push(token);
169
+ const oldBMark = state.bMarks[startLine];
170
+ const oldTShift = state.tShift[startLine];
171
+ const oldSCount = state.sCount[startLine];
172
+ const oldParentType = state.parentType;
173
+ const posAfterColon = pos;
174
+ const initial = offset = state.sCount[startLine] + pos - (state.bMarks[startLine] + state.tShift[startLine]);
175
+ while (pos < max) {
176
+ ch = state.src.charCodeAt(pos);
177
+ if (isSpace(ch)) if (ch === 9) offset += 4 - offset % 4;
178
+ else offset++;
179
+ else break;
180
+ pos++;
181
+ }
182
+ state.tShift[startLine] = pos - posAfterColon;
183
+ state.sCount[startLine] = offset - initial;
184
+ state.bMarks[startLine] = posAfterColon;
185
+ state.blkIndent += 4;
186
+ state.parentType = "footnote";
187
+ if (state.sCount[startLine] < state.blkIndent) state.sCount[startLine] += state.blkIndent;
188
+ state.md.block.tokenize(state, startLine, endLine, true);
189
+ state.parentType = oldParentType;
190
+ state.blkIndent -= 4;
191
+ state.tShift[startLine] = oldTShift;
192
+ state.sCount[startLine] = oldSCount;
193
+ state.bMarks[startLine] = oldBMark;
194
+ token = new state.Token("footnote_reference_close", "", -1);
195
+ token.level = --state.level;
196
+ state.tokens.push(token);
197
+ return true;
198
+ }
199
+ function footnoteInline(state, silent) {
200
+ const max = state.posMax;
201
+ const start = state.pos;
202
+ let footnoteId;
203
+ let token;
204
+ let tokens;
205
+ if (start + 2 >= max) return false;
206
+ if (state.src.charCodeAt(start) !== 94) return false;
207
+ if (state.src.charCodeAt(start + 1) !== 91) return false;
208
+ const labelStart = start + 2;
209
+ const labelEnd = parseLinkLabel(state, start + 1);
210
+ if (labelEnd < 0) return false;
211
+ if (!silent) {
212
+ if (!state.env.footnotes) state.env.footnotes = {};
213
+ if (!state.env.footnotes.list) state.env.footnotes.list = [];
214
+ footnoteId = state.env.footnotes.list.length;
215
+ state.md.inline.parse(state.src.slice(labelStart, labelEnd), state.md, state.env, tokens = []);
216
+ token = state.push("footnote_ref", "", 0);
217
+ token.meta = { id: footnoteId };
218
+ state.env.footnotes.list[footnoteId] = { tokens };
219
+ }
220
+ state.pos = labelEnd + 1;
221
+ state.posMax = max;
222
+ return true;
223
+ }
224
+ function footnoteRef(state, silent) {
225
+ const max = state.posMax;
226
+ const start = state.pos;
227
+ let pos;
228
+ let footnoteId;
229
+ let footnoteSubId;
230
+ let token;
231
+ if (start + 3 > max) return false;
232
+ if (!state.env.footnotes || !state.env.footnotes.refs) return false;
233
+ if (state.src.charCodeAt(start) !== 91) return false;
234
+ if (state.src.charCodeAt(start + 1) !== 94) return false;
235
+ for (pos = start + 2; pos < max; pos++) {
236
+ if (state.src.charCodeAt(pos) === 32) return false;
237
+ if (state.src.charCodeAt(pos) === 10) return false;
238
+ if (state.src.charCodeAt(pos) === 93) break;
239
+ }
240
+ if (pos === start + 2) return false;
241
+ if (pos >= max) return false;
242
+ pos++;
243
+ const label = state.src.slice(start + 2, pos - 1);
244
+ if ((0, lodash_isUndefined_js.default)(state.env.footnotes.refs[`:${label}`])) return false;
245
+ if (!silent) {
246
+ if (!state.env.footnotes.list) state.env.footnotes.list = [];
247
+ if (state.env.footnotes.refs[`:${label}`] < 0) {
248
+ footnoteId = state.env.footnotes.list.length;
249
+ state.env.footnotes.list[footnoteId] = {
250
+ label,
251
+ count: 0
252
+ };
253
+ state.env.footnotes.refs[`:${label}`] = footnoteId;
254
+ } else footnoteId = state.env.footnotes.refs[`:${label}`];
255
+ footnoteSubId = state.env.footnotes.list[footnoteId].count;
256
+ state.env.footnotes.list[footnoteId].count++;
257
+ token = state.push("footnote_ref", "", 0);
258
+ token.meta = {
259
+ id: footnoteId,
260
+ subId: footnoteSubId,
261
+ label
262
+ };
263
+ }
264
+ state.pos = pos;
265
+ state.posMax = max;
266
+ return true;
267
+ }
268
+ function footnoteTail(state) {
269
+ const refTokens = {};
270
+ let i;
271
+ let l;
272
+ let j;
273
+ let t;
274
+ let lastParagraph;
275
+ let token;
276
+ let tokens;
277
+ let current;
278
+ let currentLabel;
279
+ let insideRef = false;
280
+ let footnoteTokens = [];
281
+ if (!state.env.footnotes) return;
282
+ state.tokens = state.tokens.filter((tok) => {
283
+ if (tok.type === "footnote_reference_open") {
284
+ insideRef = true;
285
+ current = [];
286
+ currentLabel = tok.meta.label;
287
+ return false;
288
+ }
289
+ if (tok.type === "footnote_reference_close") {
290
+ insideRef = false;
291
+ refTokens[`:${currentLabel}`] = current;
292
+ return false;
293
+ }
294
+ if (insideRef) current.push(tok);
295
+ return !insideRef;
296
+ });
297
+ if (!state.env.footnotes.list) return;
298
+ const { list } = state.env.footnotes;
299
+ token = new state.Token("footnote_block_open", "", 1);
300
+ state.tokens.push(token);
301
+ for (i = 0, l = list.length; i < l; i++) {
302
+ token = new state.Token("footnote_open", "", 1);
303
+ token.meta = {
304
+ id: i,
305
+ label: list[i].label
306
+ };
307
+ state.tokens.push(token);
308
+ if (list[i].tokens) {
309
+ tokens = [];
310
+ token = new state.Token("paragraph_open", "p", 1);
311
+ token.block = true;
312
+ tokens.push(token);
313
+ token = new state.Token("inline", "", 0);
314
+ token.children = list[i].tokens;
315
+ token.content = "";
316
+ tokens.push(token);
317
+ token = new state.Token("paragraph_close", "p", -1);
318
+ token.block = true;
319
+ tokens.push(token);
320
+ } else if (list[i].label) tokens = refTokens[`:${list[i].label}`];
321
+ state.tokens = state.tokens.concat(tokens);
322
+ if (state.tokens[state.tokens.length - 1].type === "paragraph_close") lastParagraph = state.tokens.pop();
323
+ else lastParagraph = null;
324
+ t = list[i].count > 0 ? list[i].count : 1;
325
+ for (j = 0; j < t; j++) {
326
+ token = new state.Token("footnote_anchor", "", 0);
327
+ token.meta = {
328
+ id: i,
329
+ subId: j,
330
+ label: list[i].label
331
+ };
332
+ state.tokens.push(token);
333
+ }
334
+ if (lastParagraph) state.tokens.push(lastParagraph);
335
+ token = new state.Token("footnote_close", "", -1);
336
+ state.tokens.push(token);
337
+ }
338
+ token = new state.Token("footnote_block_close", "", -1);
339
+ state.tokens.push(token);
340
+ insideRef = false;
341
+ footnoteTokens = [...state.tokens].filter((a) => {
342
+ if (a.type === "footnote_block_open") {
343
+ insideRef = true;
344
+ return true;
345
+ }
346
+ if (a.type === "footnote_block_close") {
347
+ insideRef = false;
348
+ return true;
349
+ }
350
+ return insideRef;
351
+ });
352
+ insideRef = false;
353
+ state.tokens = state.tokens.filter((_) => {
354
+ if (_.type === "footnote_block_open") {
355
+ insideRef = true;
356
+ return false;
357
+ }
358
+ if (_.type === "footnote_block_close") {
359
+ insideRef = false;
360
+ return false;
361
+ }
362
+ return !insideRef;
363
+ });
364
+ callback(footnoteTokens);
365
+ }
366
+ md.block.ruler.before("reference", "footnote_def", footnoteDef, { alt: ["paragraph", "reference"] });
367
+ md.inline.ruler.after("image", "footnote_inline", footnoteInline);
368
+ md.inline.ruler.after("footnote_inline", "footnote_ref", footnoteRef);
369
+ md.core.ruler.after("inline", "footnote_tail", footnoteTail);
370
+ }
371
+ //#endregion
372
+ module.exports = footnotePlugin;
package/package.json CHANGED
@@ -1,14 +1,12 @@
1
1
  {
2
2
  "name": "@canopycanopycanopy/b-ber-parser-footnotes",
3
- "version": "3.0.8-next.61+2bbec564",
3
+ "version": "3.0.8-next.97+2e9748e6",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
6
7
  "scripts": {
7
- "prepare:dist": "BABEL_ENV=production babel --config-file ../../babel.config.js -d dist/ src/ --ignore **/__tests__/**",
8
- "clean": "rimraf dist",
9
- "prepare": "npm run clean && npm run prepare:dist",
10
- "build": "npm run prepare",
11
- "watch": "BABEL_ENV=production babel --config-file ../../babel.config.js -d dist/ src/ --watch src",
8
+ "build": "tsdown",
9
+ "typecheck": "tsc --noEmit",
12
10
  "test": "jest"
13
11
  },
14
12
  "author": "Triple Canopy <b-ber@canopycanopycanopy.com> (https://triplecanopy.github.io/)",
@@ -17,18 +15,18 @@
17
15
  "access": "public"
18
16
  },
19
17
  "devDependencies": {
20
- "@babel/cli": "^7.10.5",
21
- "@babel/core": "^7.10.5",
22
- "@babel/preset-env": "^7.10.4",
23
- "browserslist": "^4.17.4",
24
- "jest": "^26.6.3",
25
- "rimraf": "^2.7.1"
18
+ "@swc/core": "^1.15.40",
19
+ "@swc/jest": "^0.2.39",
20
+ "jest": "^29.7.0",
21
+ "rimraf": "^2.7.1",
22
+ "tsdown": "^0.22.1",
23
+ "typescript": "^6.0.3"
26
24
  },
27
25
  "dependencies": {
28
- "@canopycanopycanopy/b-ber-lib": "3.0.8-next.61+2bbec564",
29
- "@canopycanopycanopy/b-ber-logger": "3.0.8-next.61+2bbec564",
30
- "@canopycanopycanopy/b-ber-shapes-directives": "3.0.8-next.61+2bbec564",
31
- "@canopycanopycanopy/b-ber-templates": "3.0.8-next.61+2bbec564",
26
+ "@canopycanopycanopy/b-ber-lib": "3.0.8-next.97+2e9748e6",
27
+ "@canopycanopycanopy/b-ber-logger": "3.0.8-next.97+2e9748e6",
28
+ "@canopycanopycanopy/b-ber-shapes-directives": "3.0.8-next.97+2e9748e6",
29
+ "@canopycanopycanopy/b-ber-templates": "3.0.8-next.97+2e9748e6",
32
30
  "lodash": "^4.17.21"
33
31
  },
34
32
  "files": [
@@ -48,5 +46,5 @@
48
46
  "url": "https://maxwellsimmer.com"
49
47
  }
50
48
  ],
51
- "gitHead": "2bbec5643278f53becb0d2ba1f55edfe379d5587"
49
+ "gitHead": "2e9748e6f00446cd35dc8f933e049d8200fc24dd"
52
50
  }
package/dist/counter.js DELETED
@@ -1,60 +0,0 @@
1
- "use strict";
2
-
3
- var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");
4
- var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
5
- _Object$defineProperty(exports, "__esModule", {
6
- value: true
7
- });
8
- exports.default = void 0;
9
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/defineProperty"));
10
- var _crypto = _interopRequireDefault(require("crypto"));
11
- // Keep track of footnotes that have been rendered to start new ordered lists at
12
- // proper count
13
- class Counter {
14
- constructor() {
15
- (0, _defineProperty2.default)(this, "setRef", label => {
16
- const hash = _crypto.default.randomBytes(8).toString('hex');
17
- const idRef = `-${label}-${hash}`;
18
- this.refs.push(idRef);
19
- return idRef;
20
- });
21
- (0, _defineProperty2.default)(this, "getRef", () => this.refs.shift());
22
- this.page = -1;
23
- this.list = 1;
24
- this.item = 1;
25
- this.refs = [];
26
- }
27
- listCounter(grouped, page) {
28
- // Reset all counters if the footnotes list is empty. This occurs when
29
- // running `bber serve`
30
- if (page === 0) {
31
- this.page = -1;
32
- this.item = 1;
33
- this.list = 1;
34
- }
35
- if (!grouped) {
36
- return this.item;
37
- }
38
- if (page !== this.page) {
39
- this.list = 1;
40
- } else {
41
- this.list += 1;
42
- }
43
- return this.list;
44
- }
45
- listItemCounter(grouped, page) {
46
- if (!grouped) {
47
- const n = this.item;
48
- this.item += 1;
49
- return n;
50
- }
51
- if (page !== this.page) {
52
- this.page = page;
53
- this.item = 1;
54
- } else {
55
- this.item += 1;
56
- }
57
- return this.item;
58
- }
59
- }
60
- var _default = exports.default = Counter;