@peaceroad/markdown-it-table-ex 0.3.0 → 0.3.1

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.
Files changed (2) hide show
  1. package/index.js +31 -10
  2. package/package.json +3 -3
package/index.js CHANGED
@@ -34,15 +34,17 @@ const isStrongWrappedInline = (inline, allowFallback) => {
34
34
  if (!inline || typeof inline.content !== 'string') return false;
35
35
  const content = inline.content;
36
36
  if (!content.startsWith('**') || !content.endsWith('**')) return false;
37
- if (!Array.isArray(inline.children)) return !!allowFallback;
38
- if (getLeadingStrongCloseIndex(inline) !== -1) return true;
39
- return !!allowFallback;
37
+ if (allowFallback) return true;
38
+ if (!Array.isArray(inline.children)) return false;
39
+ return getLeadingStrongCloseIndex(inline) !== -1;
40
40
  };
41
41
 
42
42
  const hasLeadingStrongMarker = (inline, allowFallback) => {
43
- if (getLeadingStrongCloseIndex(inline) !== -1) return true;
44
- if (!allowFallback || !inline || typeof inline.content !== 'string') return false;
45
- return inline.content.startsWith('**');
43
+ if (!inline || typeof inline.content !== 'string') return false;
44
+ if (!inline.content.startsWith('**')) return false;
45
+ if (allowFallback) return true;
46
+ if (!Array.isArray(inline.children)) return false;
47
+ return getLeadingStrongCloseIndex(inline) !== -1;
46
48
  };
47
49
 
48
50
  const hasInlineRule = (md, name) => {
@@ -316,14 +318,20 @@ const setColgroup = (state, tableOpenIdx, opt, allowFallback) => {
316
318
  : /^\*\*[^*::]+[::]\*\*\s*(.*)$/;
317
319
 
318
320
  let thIdx = tr1 + 1;
319
- const origThs = [];
321
+ const origThInfos = [];
320
322
  let trCloseIdx = -1;
321
323
 
322
324
  while (thIdx < tokens.length) {
323
325
  const tokenType = tokens[thIdx].type;
324
326
  if (tokenType === 'th_open') {
325
327
  const inline = tokens[thIdx + 1];
326
- origThs.push(inline);
328
+ let map = null;
329
+ if (Array.isArray(tokens[thIdx].map)) {
330
+ map = tokens[thIdx].map.slice();
331
+ } else if (inline && Array.isArray(inline.map)) {
332
+ map = inline.map.slice();
333
+ }
334
+ origThInfos.push({ inline, map });
327
335
  const content = inline.content;
328
336
  const hasLeadingStrong = hasLeadingStrongMarker(inline, allowFallback);
329
337
  let match = null;
@@ -411,19 +419,23 @@ const setColgroup = (state, tableOpenIdx, opt, allowFallback) => {
411
419
 
412
420
  let thPtr = 0;
413
421
  for (let i = 0; i < groupData.spans.length; i++) {
422
+ const origInfo = origThInfos[thPtr];
423
+ const cellMap = origInfo && origInfo.map ? origInfo.map : null;
414
424
  if (groupNames[i] === null) {
415
425
  // Leftmost cell: rowspan=2
416
426
  const thOpen = new Token('th_open', 'th', 1);
417
427
  thOpen.attrSet('rowspan', '2');
418
428
  thOpen.attrSet('scope', 'col');
429
+ if (cellMap) thOpen.map = cellMap;
419
430
 
420
431
  const thInline = new Token('inline', '', 0);
421
- const origInline = origThs[thPtr];
432
+ const origInline = origInfo ? origInfo.inline : null;
422
433
 
423
434
  if (origInline) {
424
435
  removeStrongWrappers(origInline, allowFallback);
425
436
  thInline.content = origInline.content;
426
437
  thInline.children = Array.isArray(origInline.children) ? origInline.children : [];
438
+ if (cellMap) thInline.map = cellMap;
427
439
  } else {
428
440
  thInline.content = '';
429
441
  thInline.children = [];
@@ -443,10 +455,12 @@ const setColgroup = (state, tableOpenIdx, opt, allowFallback) => {
443
455
  thOpen.attrSet('colspan', groupData.spans[i].toString());
444
456
  }
445
457
  thOpen.attrSet('scope', 'col');
458
+ if (cellMap) thOpen.map = cellMap;
446
459
 
447
460
  const thInline = new Token('inline', '', 0);
448
461
  thInline.content = groupNames[i];
449
462
  thInline.children = [{ type: 'text', content: groupNames[i], level: 0 }];
463
+ if (cellMap) thInline.map = cellMap;
450
464
 
451
465
  newTr1.push(
452
466
  thOpen,
@@ -457,11 +471,14 @@ const setColgroup = (state, tableOpenIdx, opt, allowFallback) => {
457
471
 
458
472
  // Second row: each item in the group
459
473
  for (let j = 0; j < groupData.spans[i]; j++) {
474
+ const subInfo = origThInfos[thPtr];
475
+ const subMap = subInfo && subInfo.map ? subInfo.map : null;
460
476
  const th2Open = new Token('th_open', 'th', 1);
461
477
  th2Open.attrSet('scope', 'col');
478
+ if (subMap) th2Open.map = subMap;
462
479
 
463
480
  const th2Inline = new Token('inline', '', 0);
464
- const origInline = origThs[thPtr];
481
+ const origInline = subInfo ? subInfo.inline : null;
465
482
  const orig = origInline?.content || '';
466
483
  let match = null;
467
484
  if (opt.colgroupWithNoAsterisk) {
@@ -473,6 +490,7 @@ const setColgroup = (state, tableOpenIdx, opt, allowFallback) => {
473
490
  }
474
491
  th2Inline.content = match ? match[1] : orig;
475
492
  th2Inline.children = [{ type: 'text', content: th2Inline.content, level: 0 }];
493
+ if (subMap) th2Inline.map = subMap;
476
494
 
477
495
  newTr2.push(
478
496
  th2Open,
@@ -675,6 +693,9 @@ const tableEx = (state, opt) => {
675
693
  if (opt.wrapper) {
676
694
  const wrapperStartToken = new state.Token('div_open', 'div', 1);
677
695
  wrapperStartToken.attrPush(['class', 'table-wrapper']);
696
+ if (Array.isArray(tokens[idx].map)) {
697
+ wrapperStartToken.map = tokens[idx].map.slice();
698
+ }
678
699
  const linebreakToken = new state.Token('text', '', 0);
679
700
  linebreakToken.content = '\n';
680
701
  tokens.splice(idx, 0, wrapperStartToken, linebreakToken);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peaceroad/markdown-it-table-ex",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "A markdown-it plugin. Add matrix and wrapper notation for table processing.",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -16,8 +16,8 @@
16
16
  "author": "peaceroad <peaceroad@gmail.com>",
17
17
  "license": "MIT",
18
18
  "devDependencies": {
19
- "@peaceroad/markdown-it-figure-with-p-caption": "^0.14.2",
20
- "@peaceroad/markdown-it-strong-ja": "^0.5.5",
19
+ "@peaceroad/markdown-it-figure-with-p-caption": "^0.15.1",
20
+ "@peaceroad/markdown-it-strong-ja": "^0.6.1",
21
21
  "markdown-it": "^14.1.0",
22
22
  "markdown-it-multimd-table": "^4.2.3"
23
23
  }