@peaceroad/markdown-it-figure-with-p-caption 0.6.0 → 0.6.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.
- package/index.js +62 -38
- package/package.json +1 -1
- package/test/examples-img-alt-caption.txt +22 -9
- package/test/examples-img-title-caption.txt +0 -2
package/index.js
CHANGED
|
@@ -57,6 +57,19 @@ const mditFigureWithPCaption = (md, option) => {
|
|
|
57
57
|
const captionStartToken = state.tokens[n-3];
|
|
58
58
|
const captionInlineToken = state.tokens[n-2];
|
|
59
59
|
const captionEndToken = state.tokens[n-1];
|
|
60
|
+
let isNoCaption = false
|
|
61
|
+
if (captionInlineToken.attrs) {
|
|
62
|
+
for (let attr of captionInlineToken.attrs) {
|
|
63
|
+
if (attr[0] === 'class' && attr[1] === 'nocaption') {
|
|
64
|
+
isNoCaption = true
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (isNoCaption) {
|
|
69
|
+
state.tokens.splice(n-3, 3)
|
|
70
|
+
return
|
|
71
|
+
}
|
|
72
|
+
|
|
60
73
|
captionStartToken.attrs.forEach(attr => {
|
|
61
74
|
if (attr[0] === 'class') {
|
|
62
75
|
attr[1] = attr[1].replace(new RegExp(' *?f-' + caption.name), '').trim();
|
|
@@ -433,41 +446,44 @@ const mditFigureWithPCaption = (md, option) => {
|
|
|
433
446
|
|
|
434
447
|
const setAltToLabel = (state, n, en, tagName, caption, opt) => {
|
|
435
448
|
if (n < 2) return false
|
|
436
|
-
if (state.tokens[n+1].children[0].type
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
}
|
|
443
|
-
state.tokens[n+1].children[0].content = state.tokens[n-2].children[2].content
|
|
444
|
-
state.tokens[n+1].children[0].children[0].content = state.tokens[n-2].children[2].content
|
|
449
|
+
if (state.tokens[n+1].children[0].type !== 'image' || !state.tokens[n-2].children) return false
|
|
450
|
+
if (state.tokens[n-2].children[2]) {
|
|
451
|
+
state.tokens[n+1].content = state.tokens[n+1].content.replace(/^!\[.*?\]/, '![' + state.tokens[n-2].children[2].content + ']')
|
|
452
|
+
if (!state.tokens[n+1].children[0].children[0]) {
|
|
453
|
+
const textToken = new state.Token('text', '', 0)
|
|
454
|
+
state.tokens[n+1].children[0].children.push(textToken)
|
|
445
455
|
}
|
|
456
|
+
// Set figure label:
|
|
457
|
+
//state.tokens[n+1].children[0].children[0].content = state.tokens[n-2].children[2].content
|
|
458
|
+
// Set img alt to empty value:
|
|
459
|
+
state.tokens[n+1].children[0].children[0].content = ''
|
|
446
460
|
}
|
|
461
|
+
// Set figure label:
|
|
462
|
+
//state.tokens[n+1].children[0].content = state.tokens[n-2].children[2].content
|
|
463
|
+
// Set img alt to empty value:
|
|
464
|
+
state.tokens[n+1].children[0].content = ''
|
|
447
465
|
//console.log(state.tokens[n+1].children[0])
|
|
448
466
|
return true
|
|
449
467
|
}
|
|
450
468
|
|
|
451
469
|
const setTitleToLabel = (state, n, en, tagName, caption, opt) => {
|
|
452
470
|
if (n < 2) return false
|
|
453
|
-
if (state.tokens[n+1].children[0].type
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
}
|
|
468
|
-
i++
|
|
469
|
-
}
|
|
471
|
+
if (state.tokens[n+1].children[0].type !== 'image') return false
|
|
472
|
+
if (!state.tokens[n-2].children[0]) return false
|
|
473
|
+
state.tokens[n+1].children[0].attrSet('alt', state.tokens[n+1].children[0].content)
|
|
474
|
+
if (!state.tokens[n+1].children[0].children[0]) {
|
|
475
|
+
const textToken = new state.Token('text', '', 0)
|
|
476
|
+
state.tokens[n+1].children[0].children.push(textToken)
|
|
477
|
+
}
|
|
478
|
+
let i = 0
|
|
479
|
+
while (0 < state.tokens[n+1].children[0].attrs.length) {
|
|
480
|
+
if (state.tokens[n+1].children[0].attrs[i][0] === 'title') {
|
|
481
|
+
state.tokens[n+1].children[0].attrs.splice(i, i + 1)
|
|
482
|
+
break
|
|
483
|
+
} else {
|
|
484
|
+
state.tokens[n+1].children[0].attrJoin('title', '')
|
|
470
485
|
}
|
|
486
|
+
i++
|
|
471
487
|
}
|
|
472
488
|
//console.log(state.tokens[n+1].children[0])
|
|
473
489
|
return true
|
|
@@ -481,6 +497,7 @@ const mditFigureWithPCaption = (md, option) => {
|
|
|
481
497
|
if (opt.imgAltCaption && typeof opt.imgAltCaption === 'string') label = opt.imgAltCaption
|
|
482
498
|
if (opt.imgTitleCaption && typeof opt.imgTitleCaption === 'string') label = opt.imgTitleCaption
|
|
483
499
|
let caption = ''
|
|
500
|
+
let imgAttrUsedCaption = ''
|
|
484
501
|
|
|
485
502
|
const img = inline.match(/^( *!\[)(.*?)\]\( *?((.*?)(?: +?\"(.*?)\")?) *?\)( *?\{.*?\})? *$/)
|
|
486
503
|
if (!img) return
|
|
@@ -489,41 +506,48 @@ const mditFigureWithPCaption = (md, option) => {
|
|
|
489
506
|
if (opt.imgAltCaption) {
|
|
490
507
|
caption = img[2]
|
|
491
508
|
hasLabel = img[2].match(new RegExp('^' + opt.imgAltCaption))
|
|
509
|
+
imgAttrUsedCaption = 'alt'
|
|
492
510
|
}
|
|
493
511
|
if (opt.imgTitleCaption) {
|
|
494
512
|
if (!img[5]) img[5] = ''
|
|
495
513
|
caption = img[5]
|
|
496
514
|
hasLabel = img[5].match(new RegExp('^' + opt.imgTitleCaption))
|
|
515
|
+
imgAttrUsedCaption = 'title'
|
|
497
516
|
}
|
|
498
517
|
let token
|
|
499
518
|
token = state.push('paragraph_open', 'p', 1)
|
|
500
519
|
token.map = [startLine, startLine + 1]
|
|
501
520
|
token = state.push('inline', '', 0)
|
|
502
|
-
|
|
503
521
|
if (hasLabel) {
|
|
504
522
|
token.content = caption
|
|
505
523
|
} else {
|
|
506
|
-
if (label) {
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
} else {
|
|
512
|
-
|
|
524
|
+
if (!label) {
|
|
525
|
+
if (imgAttrUsedCaption === 'alt') {
|
|
526
|
+
label = opt.imgAltCaption
|
|
527
|
+
} else if (imgAttrUsedCaption === 'title') {
|
|
528
|
+
label = opt.imgTitleCaption
|
|
529
|
+
} else if (imgAttrUsedCaption) {
|
|
530
|
+
label = 'Figure'
|
|
513
531
|
}
|
|
514
|
-
|
|
532
|
+
}
|
|
533
|
+
token.content = label
|
|
534
|
+
if (/[a-zA-Z]/.test(label)) {
|
|
535
|
+
token.content += '.'
|
|
536
|
+
if (caption) token.content += ' '
|
|
515
537
|
} else {
|
|
516
|
-
token.content
|
|
538
|
+
token.content += ' '
|
|
517
539
|
}
|
|
540
|
+
token.content += caption
|
|
518
541
|
}
|
|
519
|
-
//console.log('token.content: ' + token.content)
|
|
520
542
|
token.map = [startLine, startLine + 1]
|
|
521
543
|
token.children = []
|
|
544
|
+
if (caption.length === 0) {
|
|
545
|
+
token.attrs = [['class', 'nocaption']]
|
|
546
|
+
}
|
|
522
547
|
token = state.push('paragraph_close', 'p', -1)
|
|
523
548
|
return
|
|
524
549
|
}
|
|
525
550
|
|
|
526
|
-
|
|
527
551
|
if (opt.imgAltCaption || opt.imgTitleCaption) {
|
|
528
552
|
md.block.ruler.before('paragraph', 'img_attr_caption', imgAttrToPCaption)
|
|
529
553
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peaceroad/markdown-it-figure-with-p-caption",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"description": "A markdown-it plugin. For a paragraph with only one image, a table or code block or blockquote, and by writing a caption paragraph immediately before or after, they are converted into the figure element with the figcaption element.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
[HTML]
|
|
5
5
|
<figure class="f-img">
|
|
6
6
|
<figcaption><span class="f-img-label">Figure<span class="f-img-label-joint">.</span></span> A caption.</figcaption>
|
|
7
|
-
<img src="cat.jpg" alt="
|
|
7
|
+
<img src="cat.jpg" alt="">
|
|
8
8
|
</figure>
|
|
9
9
|
|
|
10
10
|
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
[HTML]
|
|
15
15
|
<figure class="f-img">
|
|
16
16
|
<figcaption><span class="f-img-label">Figure<span class="f-img-label-joint">.</span></span> A caption.</figcaption>
|
|
17
|
-
<img src="cat.jpg" alt="
|
|
17
|
+
<img src="cat.jpg" alt="">
|
|
18
18
|
</figure>
|
|
19
19
|
|
|
20
20
|
|
|
@@ -23,8 +23,7 @@
|
|
|
23
23
|
|
|
24
24
|
[HTML]
|
|
25
25
|
<figure class="f-img">
|
|
26
|
-
<
|
|
27
|
-
<img src="cat.jpg" alt="Figure">
|
|
26
|
+
<img src="cat.jpg" alt="">
|
|
28
27
|
</figure>
|
|
29
28
|
|
|
30
29
|
|
|
@@ -36,8 +35,7 @@ Figure. A caption.
|
|
|
36
35
|
[HTML]
|
|
37
36
|
<p class="f-img"><span class="f-img-label">Figure<span class="f-img-label-joint">.</span></span> A caption.</p>
|
|
38
37
|
<figure class="f-img">
|
|
39
|
-
<
|
|
40
|
-
<img src="cat.jpg" alt="Figure">
|
|
38
|
+
<img src="cat.jpg" alt="">
|
|
41
39
|
</figure>
|
|
42
40
|
|
|
43
41
|
|
|
@@ -47,7 +45,7 @@ Figure. A caption.
|
|
|
47
45
|
[HTML]
|
|
48
46
|
<figure class="f-img">
|
|
49
47
|
<figcaption><span class="f-img-label">Figure 12<span class="f-img-label-joint">.</span></span> A caption.</figcaption>
|
|
50
|
-
<img src="cat.jpg" alt="
|
|
48
|
+
<img src="cat.jpg" alt="">
|
|
51
49
|
</figure>
|
|
52
50
|
|
|
53
51
|
|
|
@@ -57,7 +55,7 @@ Figure. A caption.
|
|
|
57
55
|
[HTML]
|
|
58
56
|
<figure class="f-img">
|
|
59
57
|
<figcaption><span class="f-img-label">図1<span class="f-img-label-joint"> </span></span>キャプション</figcaption>
|
|
60
|
-
<img src="cat.jpg" alt="
|
|
58
|
+
<img src="cat.jpg" alt="">
|
|
61
59
|
</figure>
|
|
62
60
|
|
|
63
61
|
|
|
@@ -70,7 +68,22 @@ Figure. A caption.
|
|
|
70
68
|
<p class="f-img"><span class="f-img-label">図</span> キャプション</p>
|
|
71
69
|
<figure class="f-img">
|
|
72
70
|
<figcaption><span class="f-img-label">図1<span class="f-img-label-joint"> </span></span>キャプション</figcaption>
|
|
73
|
-
<img src="cat.jpg" alt="
|
|
71
|
+
<img src="cat.jpg" alt="">
|
|
74
72
|
</figure>
|
|
75
73
|
|
|
76
74
|
|
|
75
|
+
[Markdown]
|
|
76
|
+

|
|
77
|
+
|
|
78
|
+

|
|
79
|
+
|
|
80
|
+
[HTML]
|
|
81
|
+
<figure class="f-img">
|
|
82
|
+
<figcaption><span class="f-img-label">図<span class="f-img-label-joint"> </span></span>A caption.</figcaption>
|
|
83
|
+
<img src="cat.jpg" alt="">
|
|
84
|
+
</figure>
|
|
85
|
+
<figure class="f-img">
|
|
86
|
+
<figcaption><span class="f-img-label">図</span> A caption.</figcaption>
|
|
87
|
+
<img src="cat.jpg" alt="">
|
|
88
|
+
</figure>
|
|
89
|
+
|
|
@@ -23,7 +23,6 @@
|
|
|
23
23
|
|
|
24
24
|
[HTML]
|
|
25
25
|
<figure class="f-img">
|
|
26
|
-
<figcaption><span class="f-img-label">Figure<span class="f-img-label-joint">.</span></span></figcaption>
|
|
27
26
|
<img src="cat.jpg" alt="A alt text.">
|
|
28
27
|
</figure>
|
|
29
28
|
|
|
@@ -45,7 +44,6 @@ Figure. A caption.
|
|
|
45
44
|
[HTML]
|
|
46
45
|
<p class="f-img"><span class="f-img-label">Figure<span class="f-img-label-joint">.</span></span> A caption.</p>
|
|
47
46
|
<figure class="f-img">
|
|
48
|
-
<figcaption><span class="f-img-label">Figure<span class="f-img-label-joint">.</span></span></figcaption>
|
|
49
47
|
<img src="cat.jpg" alt="">
|
|
50
48
|
</figure>
|
|
51
49
|
|