@canopycanopycanopy/b-ber-parser-footnotes 1.2.16 → 2.0.2

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.
package/dist/counter.js CHANGED
@@ -1,26 +1,19 @@
1
1
  "use strict";
2
2
 
3
3
  var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");
4
-
5
4
  var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
6
-
7
5
  _Object$defineProperty(exports, "__esModule", {
8
6
  value: true
9
7
  });
10
-
11
8
  exports.default = void 0;
12
-
13
9
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/defineProperty"));
14
-
15
10
  var _crypto = _interopRequireDefault(require("crypto"));
16
-
17
11
  // Keep track of footnotes that have been rendered to start new ordered lists at
18
12
  // proper count
19
13
  class Counter {
20
14
  constructor() {
21
15
  (0, _defineProperty2.default)(this, "setRef", label => {
22
16
  const hash = _crypto.default.randomBytes(8).toString('hex');
23
-
24
17
  const idRef = `-${label}-${hash}`;
25
18
  this.refs.push(idRef);
26
19
  return idRef;
@@ -31,7 +24,6 @@ class Counter {
31
24
  this.item = 1;
32
25
  this.refs = [];
33
26
  }
34
-
35
27
  listCounter(grouped, page) {
36
28
  // Reset all counters if the footnotes list is empty. This occurs when
37
29
  // running `bber serve`
@@ -40,38 +32,30 @@ class Counter {
40
32
  this.item = 1;
41
33
  this.list = 1;
42
34
  }
43
-
44
35
  if (!grouped) {
45
36
  return this.item;
46
37
  }
47
-
48
38
  if (page !== this.page) {
49
39
  this.list = 1;
50
40
  } else {
51
41
  this.list += 1;
52
42
  }
53
-
54
43
  return this.list;
55
44
  }
56
-
57
45
  listItemCounter(grouped, page) {
58
46
  if (!grouped) {
59
47
  const n = this.item;
60
48
  this.item += 1;
61
49
  return n;
62
50
  }
63
-
64
51
  if (page !== this.page) {
65
52
  this.page = page;
66
53
  this.item = 1;
67
54
  } else {
68
55
  this.item += 1;
69
56
  }
70
-
71
57
  return this.item;
72
58
  }
73
-
74
59
  }
75
-
76
60
  var _default = Counter;
77
61
  exports.default = _default;
package/dist/index.js CHANGED
@@ -1,23 +1,14 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
4
-
5
4
  var _isArray = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/array/is-array"));
6
-
7
5
  var _slice = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/slice"));
8
-
9
6
  var _filter = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/filter"));
10
-
11
7
  var _concat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/concat"));
12
-
13
8
  var _lodash = _interopRequireDefault(require("lodash.isundefined"));
14
-
15
9
  var _State = _interopRequireDefault(require("@canopycanopycanopy/b-ber-lib/State"));
16
-
17
10
  var _counter = _interopRequireDefault(require("./counter"));
18
-
19
11
  /* eslint-disable camelcase */
20
-
21
12
  /* eslint-disable no-param-reassign,no-plusplus */
22
13
 
23
14
  /*
@@ -25,51 +16,43 @@ Modified version of markdown-it-footnote@3.0.1
25
16
  https://github.com//markdown-it/markdown-it-footnote
26
17
  MIT license
27
18
  */
28
- const counter = new _counter.default();
29
19
 
20
+ const counter = new _counter.default();
30
21
  function renderFootnoteAnchorName(tokens, idx, _options, env, _slf) {
31
22
  const n = Number(tokens[idx].meta.id + 1).toString();
32
23
  return typeof env.docId === 'string' ? `-${env.docId}-${n}` : '';
33
24
  }
34
-
35
25
  function renderFootnoteCaption(tokens, idx, _options, _env, _slf) {
36
26
  let n;
37
-
38
27
  if (!_State.default.config.group_footnotes) {
39
28
  n = counter.listItemCounter(_State.default.config.group_footnotes, _State.default.footnotes.length);
40
29
  } else {
41
30
  n = Number(tokens[idx].meta.id + 1);
42
31
  }
43
-
44
32
  return tokens[idx].meta.subId > 0 ? `${n}:${tokens[idx].meta.subId}` : n;
45
33
  }
46
-
47
34
  function renderFootnoteRef(tokens, idx, options, env, slf) {
48
35
  const caption = slf.rules.footnote_caption(tokens, idx, options, env, slf);
49
36
  const ref = counter.getRef();
50
37
  return `<a epub:type="noteref" class="footnote-ref" href="notes.xhtml#fn${ref}" id="fnref${ref}">${caption}</a>`;
51
38
  }
52
-
53
- function renderFootnoteBlockOpen()
54
- /* tokens, idx, options */
55
- {
39
+ function /* tokens, idx, options */
40
+ renderFootnoteBlockOpen() {
56
41
  const start = counter.listCounter(_State.default.config.group_footnotes, _State.default.footnotes.length);
57
42
  return `<ol class="footnotes" start="${start}">`;
58
43
  }
59
-
60
44
  function renderFootnoteBlockClose() {
61
45
  return '</ol>';
62
46
  }
63
-
64
47
  function renderFootnoteOpen(tokens, idx, _options, env, _self) {
65
48
  const ref = counter.setRef(tokens[idx].meta.label);
66
- const childIndex = idx + 2; // push the backlink into the parent paragraph
49
+ const childIndex = idx + 2;
67
50
 
51
+ // push the backlink into the parent paragraph
68
52
  if (tokens[childIndex]) {
69
53
  if (!(0, _isArray.default)(tokens[childIndex].children)) {
70
54
  tokens[childIndex].children = [];
71
55
  }
72
-
73
56
  tokens[childIndex].children.push({
74
57
  type: 'inline',
75
58
  attrs: [['hidden', 'hidden'], ['class', 'hidden-backlink']],
@@ -95,22 +78,17 @@ function renderFootnoteOpen(tokens, idx, _options, env, _self) {
95
78
  nesting: -1
96
79
  });
97
80
  }
98
-
99
81
  return `<li class="footnote" epub:type="footnote" id="fn${ref}">`;
100
82
  }
101
-
102
83
  function renderFootnoteClose() {
103
84
  return '</li>';
104
85
  }
105
-
106
- function renderFootnoteAnchor()
107
- /*tokens, idx, options,env, slf */
108
- {
86
+ function /*tokens, idx, options,env, slf */
87
+ renderFootnoteAnchor() {
109
88
  /* ↩ with escape code to prevent display as Apple Emoji on iOS */
110
89
  // return ' <a href="#fnref' + id + '">\u21a9\uFE0E</a>';
111
90
  return '';
112
91
  }
113
-
114
92
  module.exports = function footnotePlugin(md, callback) {
115
93
  const {
116
94
  parseLinkLabel
@@ -123,49 +101,40 @@ module.exports = function footnotePlugin(md, callback) {
123
101
  md.renderer.rules.footnote_block_close = renderFootnoteBlockClose;
124
102
  md.renderer.rules.footnote_open = renderFootnoteOpen;
125
103
  md.renderer.rules.footnote_close = renderFootnoteClose;
126
- md.renderer.rules.footnote_anchor = renderFootnoteAnchor; // helpers (only used in other rules, no tokens are attached to those)
104
+ md.renderer.rules.footnote_anchor = renderFootnoteAnchor;
127
105
 
106
+ // helpers (only used in other rules, no tokens are attached to those)
128
107
  md.renderer.rules.footnote_caption = renderFootnoteCaption;
129
- md.renderer.rules.footnoteAnchorName = renderFootnoteAnchorName; // Process footnote block definition
108
+ md.renderer.rules.footnoteAnchorName = renderFootnoteAnchorName;
130
109
 
110
+ // Process footnote block definition
131
111
  function footnoteDef(state, startLine, endLine, silent) {
132
112
  var _context;
133
-
134
113
  const start = state.bMarks[startLine] + state.tShift[startLine];
135
114
  const max = state.eMarks[startLine];
136
115
  let pos;
137
116
  let token;
138
117
  let offset;
139
- let ch; // line should be at least 5 chars - "[^x]:"
118
+ let ch;
140
119
 
120
+ // line should be at least 5 chars - "[^x]:"
141
121
  if (start + 4 > max) return false;
142
- if (state.src.charCodeAt(start) !== 0x5b
143
- /* [ */
144
- ) return false;
145
- if (state.src.charCodeAt(start + 1) !== 0x5e
146
- /* ^ */
147
- ) return false;
148
-
122
+ if (state.src.charCodeAt(start) !== 0x5b /* [ */) return false;
123
+ if (state.src.charCodeAt(start + 1) !== 0x5e /* ^ */) return false;
149
124
  for (pos = start + 2; pos < max; pos++) {
150
125
  if (state.src.charCodeAt(pos) === 0x20) return false;
151
-
152
- if (state.src.charCodeAt(pos) === 0x5d
153
- /* ] */
154
- ) {
155
- break;
156
- }
126
+ if (state.src.charCodeAt(pos) === 0x5d /* ] */) {
127
+ break;
128
+ }
157
129
  }
158
-
159
130
  if (pos === start + 2) return false; // no empty footnote labels
160
-
161
- if (pos + 1 >= max || state.src.charCodeAt(++pos) !== 0x3a
162
- /* : */
163
- ) {
164
- return false;
165
- }
166
-
131
+ if (pos + 1 >= max || state.src.charCodeAt(++pos) !== 0x3a /* : */) {
132
+ return false;
133
+ }
167
134
  if (silent) return true;
168
- pos++; // Footnote is found, start parsing footnote body here
135
+ pos++;
136
+
137
+ // Footnote is found, start parsing footnote body here
169
138
 
170
139
  if (!state.env.footnotes) state.env.footnotes = {};
171
140
  if (!state.env.footnotes.refs) state.env.footnotes.refs = {};
@@ -181,13 +150,12 @@ module.exports = function footnotePlugin(md, callback) {
181
150
  const oldTShift = state.tShift[startLine];
182
151
  const oldSCount = state.sCount[startLine];
183
152
  const oldParentType = state.parentType;
184
- const posAfterColon = pos; // eslint-disable-next-line no-multi-assign
153
+ const posAfterColon = pos;
185
154
 
155
+ // eslint-disable-next-line no-multi-assign
186
156
  const initial = offset = state.sCount[startLine] + pos - (state.bMarks[startLine] + state.tShift[startLine]);
187
-
188
157
  while (pos < max) {
189
158
  ch = state.src.charCodeAt(pos);
190
-
191
159
  if (isSpace(ch)) {
192
160
  if (ch === 0x09) {
193
161
  offset += 4 - offset % 4;
@@ -197,20 +165,16 @@ module.exports = function footnotePlugin(md, callback) {
197
165
  } else {
198
166
  break;
199
167
  }
200
-
201
168
  pos++;
202
169
  }
203
-
204
170
  state.tShift[startLine] = pos - posAfterColon;
205
171
  state.sCount[startLine] = offset - initial;
206
172
  state.bMarks[startLine] = posAfterColon;
207
173
  state.blkIndent += 4;
208
174
  state.parentType = 'footnote';
209
-
210
175
  if (state.sCount[startLine] < state.blkIndent) {
211
176
  state.sCount[startLine] += state.blkIndent;
212
177
  }
213
-
214
178
  state.md.block.tokenize(state, startLine, endLine, true);
215
179
  state.parentType = oldParentType;
216
180
  state.blkIndent -= 4;
@@ -221,9 +185,9 @@ module.exports = function footnotePlugin(md, callback) {
221
185
  token.level = --state.level;
222
186
  state.tokens.push(token);
223
187
  return true;
224
- } // Process inline footnotes (^[...])
225
-
188
+ }
226
189
 
190
+ // Process inline footnotes (^[...])
227
191
  function footnoteInline(state, silent) {
228
192
  const max = state.posMax;
229
193
  const start = state.pos;
@@ -231,21 +195,18 @@ module.exports = function footnotePlugin(md, callback) {
231
195
  let token;
232
196
  let tokens;
233
197
  if (start + 2 >= max) return false;
234
- if (state.src.charCodeAt(start) !== 0x5e
235
- /* ^ */
236
- ) return false;
237
- if (state.src.charCodeAt(start + 1) !== 0x5b
238
- /* [ */
239
- ) return false;
198
+ if (state.src.charCodeAt(start) !== 0x5e /* ^ */) return false;
199
+ if (state.src.charCodeAt(start + 1) !== 0x5b /* [ */) return false;
240
200
  const labelStart = start + 2;
241
- const labelEnd = parseLinkLabel(state, start + 1); // Failed to find ']', so it's not a valid note
201
+ const labelEnd = parseLinkLabel(state, start + 1);
242
202
 
243
- if (labelEnd < 0) return false; // Found the end of the link, and know for a fact it's a valid link; so
244
- // all that's left to do is to call tokenizer.
203
+ // Failed to find ']', so it's not a valid note
204
+ if (labelEnd < 0) return false;
245
205
 
206
+ // Found the end of the link, and know for a fact it's a valid link; so
207
+ // all that's left to do is to call tokenizer.
246
208
  if (!silent) {
247
209
  var _context2;
248
-
249
210
  if (!state.env.footnotes) state.env.footnotes = {};
250
211
  if (!state.env.footnotes.list) state.env.footnotes.list = [];
251
212
  footnoteId = state.env.footnotes.list.length;
@@ -258,50 +219,38 @@ module.exports = function footnotePlugin(md, callback) {
258
219
  tokens
259
220
  };
260
221
  }
261
-
262
222
  state.pos = labelEnd + 1;
263
223
  state.posMax = max;
264
224
  return true;
265
- } // Process footnote references ([^...])
266
-
225
+ }
267
226
 
227
+ // Process footnote references ([^...])
268
228
  function footnoteRef(state, silent) {
269
229
  var _context3;
270
-
271
230
  const max = state.posMax;
272
231
  const start = state.pos;
273
232
  let pos;
274
233
  let footnoteId;
275
234
  let footnoteSubId;
276
- let token; // should be at least 4 chars - "[^x]"
235
+ let token;
277
236
 
237
+ // should be at least 4 chars - "[^x]"
278
238
  if (start + 3 > max) return false;
279
239
  if (!state.env.footnotes || !state.env.footnotes.refs) return false;
280
- if (state.src.charCodeAt(start) !== 0x5b
281
- /* [ */
282
- ) return false;
283
- if (state.src.charCodeAt(start + 1) !== 0x5e
284
- /* ^ */
285
- ) return false;
286
-
240
+ if (state.src.charCodeAt(start) !== 0x5b /* [ */) return false;
241
+ if (state.src.charCodeAt(start + 1) !== 0x5e /* ^ */) return false;
287
242
  for (pos = start + 2; pos < max; pos++) {
288
243
  if (state.src.charCodeAt(pos) === 0x20) return false;
289
244
  if (state.src.charCodeAt(pos) === 0x0a) return false;
290
- if (state.src.charCodeAt(pos) === 0x5d
291
- /* ] */
292
- ) break;
245
+ if (state.src.charCodeAt(pos) === 0x5d /* ] */) break;
293
246
  }
294
-
295
247
  if (pos === start + 2) return false; // no empty footnote labels
296
-
297
248
  if (pos >= max) return false;
298
249
  pos++;
299
250
  const label = (0, _slice.default)(_context3 = state.src).call(_context3, start + 2, pos - 1);
300
251
  if ((0, _lodash.default)(state.env.footnotes.refs[`:${label}`])) return false;
301
-
302
252
  if (!silent) {
303
253
  if (!state.env.footnotes.list) state.env.footnotes.list = [];
304
-
305
254
  if (state.env.footnotes.refs[`:${label}`] < 0) {
306
255
  footnoteId = state.env.footnotes.list.length;
307
256
  state.env.footnotes.list[footnoteId] = {
@@ -312,7 +261,6 @@ module.exports = function footnotePlugin(md, callback) {
312
261
  } else {
313
262
  footnoteId = state.env.footnotes.refs[`:${label}`];
314
263
  }
315
-
316
264
  footnoteSubId = state.env.footnotes.list[footnoteId].count;
317
265
  state.env.footnotes.list[footnoteId].count++;
318
266
  token = state.push('footnote_ref', '', 0);
@@ -322,16 +270,14 @@ module.exports = function footnotePlugin(md, callback) {
322
270
  label
323
271
  };
324
272
  }
325
-
326
273
  state.pos = pos;
327
274
  state.posMax = max;
328
275
  return true;
329
- } // Glue footnote tokens to end of token stream
330
-
276
+ }
331
277
 
278
+ // Glue footnote tokens to end of token stream
332
279
  function footnoteTail(state) {
333
280
  var _context4, _context6, _context7;
334
-
335
281
  const refTokens = {};
336
282
  let i;
337
283
  let l;
@@ -352,14 +298,12 @@ module.exports = function footnotePlugin(md, callback) {
352
298
  currentLabel = tok.meta.label;
353
299
  return false;
354
300
  }
355
-
356
301
  if (tok.type === 'footnote_reference_close') {
357
- insideRef = false; // prepend ':' to avoid conflict with Object.prototype members
358
-
302
+ insideRef = false;
303
+ // prepend ':' to avoid conflict with Object.prototype members
359
304
  refTokens[`:${currentLabel}`] = current;
360
305
  return false;
361
306
  }
362
-
363
307
  if (insideRef) current.push(tok);
364
308
  return !insideRef;
365
309
  });
@@ -369,17 +313,14 @@ module.exports = function footnotePlugin(md, callback) {
369
313
  } = state.env.footnotes;
370
314
  token = new state.Token('footnote_block_open', '', 1);
371
315
  state.tokens.push(token);
372
-
373
316
  for (i = 0, l = list.length; i < l; i++) {
374
317
  var _context5;
375
-
376
318
  token = new state.Token('footnote_open', '', 1);
377
319
  token.meta = {
378
320
  id: i,
379
321
  label: list[i].label
380
322
  };
381
323
  state.tokens.push(token);
382
-
383
324
  if (list[i].tokens) {
384
325
  tokens = [];
385
326
  token = new state.Token('paragraph_open', 'p', 1);
@@ -395,17 +336,13 @@ module.exports = function footnotePlugin(md, callback) {
395
336
  } else if (list[i].label) {
396
337
  tokens = refTokens[`:${list[i].label}`];
397
338
  }
398
-
399
339
  state.tokens = (0, _concat.default)(_context5 = state.tokens).call(_context5, tokens);
400
-
401
340
  if (state.tokens[state.tokens.length - 1].type === 'paragraph_close') {
402
341
  lastParagraph = state.tokens.pop();
403
342
  } else {
404
343
  lastParagraph = null;
405
344
  }
406
-
407
345
  t = list[i].count > 0 ? list[i].count : 1;
408
-
409
346
  for (j = 0; j < t; j++) {
410
347
  token = new state.Token('footnote_anchor', '', 0);
411
348
  token.meta = {
@@ -415,51 +352,46 @@ module.exports = function footnotePlugin(md, callback) {
415
352
  };
416
353
  state.tokens.push(token);
417
354
  }
418
-
419
355
  if (lastParagraph) {
420
356
  state.tokens.push(lastParagraph);
421
357
  }
422
-
423
358
  token = new state.Token('footnote_close', '', -1);
424
359
  state.tokens.push(token);
425
360
  }
426
-
427
361
  token = new state.Token('footnote_block_close', '', -1);
428
- state.tokens.push(token); // create return value for callback
362
+ state.tokens.push(token);
429
363
 
364
+ // create return value for callback
430
365
  insideRef = false;
431
366
  footnoteTokens = (0, _filter.default)(_context6 = [...state.tokens]).call(_context6, a => {
432
367
  if (a.type === 'footnote_block_open') {
433
368
  insideRef = true;
434
369
  return true;
435
370
  }
436
-
437
371
  if (a.type === 'footnote_block_close') {
438
372
  insideRef = false;
439
373
  return true;
440
374
  }
441
-
442
375
  return insideRef;
443
- }); // remove footnotes from `state.tokens`
376
+ });
444
377
 
378
+ // remove footnotes from `state.tokens`
445
379
  insideRef = false;
446
380
  state.tokens = (0, _filter.default)(_context7 = state.tokens).call(_context7, _ => {
447
381
  if (_.type === 'footnote_block_open') {
448
382
  insideRef = true;
449
383
  return false;
450
384
  }
451
-
452
385
  if (_.type === 'footnote_block_close') {
453
386
  insideRef = false;
454
387
  return false;
455
388
  }
456
-
457
389
  return !insideRef;
458
- }); // return to MarkdownRenderer class
390
+ });
459
391
 
392
+ // return to MarkdownRenderer class
460
393
  callback(footnoteTokens);
461
394
  }
462
-
463
395
  md.block.ruler.before('reference', 'footnote_def', footnoteDef, {
464
396
  alt: ['paragraph', 'reference']
465
397
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@canopycanopycanopy/b-ber-parser-footnotes",
3
- "version": "1.2.16",
3
+ "version": "2.0.2",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -20,14 +20,15 @@
20
20
  "@babel/cli": "^7.10.5",
21
21
  "@babel/core": "^7.10.5",
22
22
  "@babel/preset-env": "^7.10.4",
23
+ "browserslist": "^4.17.4",
23
24
  "jest": "^26.6.3",
24
- "rimraf": "^2.6.3"
25
+ "rimraf": "^2.7.1"
25
26
  },
26
27
  "dependencies": {
27
- "@canopycanopycanopy/b-ber-lib": "1.2.16",
28
- "@canopycanopycanopy/b-ber-logger": "1.2.16",
29
- "@canopycanopycanopy/b-ber-shapes-directives": "1.2.16",
30
- "@canopycanopycanopy/b-ber-templates": "1.2.16",
28
+ "@canopycanopycanopy/b-ber-lib": "2.0.2",
29
+ "@canopycanopycanopy/b-ber-logger": "2.0.2",
30
+ "@canopycanopycanopy/b-ber-shapes-directives": "2.0.2",
31
+ "@canopycanopycanopy/b-ber-templates": "2.0.2",
31
32
  "lodash": "^4.17.21",
32
33
  "lodash.isundefined": "^3.0.1",
33
34
  "tar": "^6.1.11"
@@ -49,5 +50,5 @@
49
50
  "url": "https://maxwellsimmer.com"
50
51
  }
51
52
  ],
52
- "gitHead": "b4d9c563beb349a32c19e0f65447f7c7dbc8ea52"
53
+ "gitHead": "9b13185f21f602f8a2bf3a4cf503b1fd644d6432"
53
54
  }