@peaceroad/markdown-it-figure-with-p-caption 0.11.0 → 0.13.0
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/README.md +10 -4
- package/imgAttrToPCaption.js +31 -53
- package/index.js +312 -195
- package/package.json +12 -6
- package/.vscode/settings.json +0 -5
- package/test/examples-all-iframe-type-figure-class-name.txt +0 -192
- package/test/examples-console.txt +0 -125
- package/test/examples-has-num-class.txt +0 -31
- package/test/examples-iframe-type-blockquote-without-caption.txt +0 -92
- package/test/examples-iframe-without-caption.txt +0 -64
- package/test/examples-img-alt-caption-number.en.txt +0 -51
- package/test/examples-img-alt-caption.en.txt +0 -60
- package/test/examples-img-alt-caption.ja.txt +0 -84
- package/test/examples-img-title-caption-number.en.txt +0 -60
- package/test/examples-img-title-caption.en.txt +0 -60
- package/test/examples-img-title-caption.ja.txt +0 -30
- package/test/examples-multiple-images.txt +0 -140
- package/test/examples-no-option.txt +0 -770
- package/test/examples-one-image-without-caption.txt +0 -59
- package/test/examples-set-figure-number.en.txt +0 -21
- package/test/examples-video-without-caption.txt +0 -52
- package/test/test.js +0 -208
package/README.md
CHANGED
|
@@ -2,11 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
This is a markdown-it plugin.
|
|
4
4
|
|
|
5
|
-
For
|
|
5
|
+
For paragraphs containing only images, tables, code blocks, blockquotes, or iframes, this plugin converts them into figure elements with figcaption elements when a caption paragraph is written immediately before or after.
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
The conversion process:
|
|
8
|
+
1. Detect supported elements: image paragraphs, tables, code/samp blocks, blockquotes, videos, and iframes
|
|
9
|
+
2. Check for caption paragraphs immediately before or after the element
|
|
10
|
+
3. Convert both elements into a figure with figcaption structure
|
|
10
11
|
|
|
11
12
|
The figcaption behavior of this plugin depends on [p7d-markdown-it-p-captions](https://www.npmjs.com/package/p7d-markdown-it-p-captions).
|
|
12
13
|
|
|
@@ -36,6 +37,11 @@ Also, It is recommended to set the width and height attributes of the images at
|
|
|
36
37
|
|
|
37
38
|
It could be applied to table, codeblock(pre > code, pre > samp), video as well.
|
|
38
39
|
|
|
40
|
+
These elements are also supported within the following structure. [0.13.0+]
|
|
41
|
+
|
|
42
|
+
- Blockquote
|
|
43
|
+
- loose list (with blank lines between items), not tight list (no blank lines)
|
|
44
|
+
- Description list block (`<dl>` markup, markdown-it-deflist)
|
|
39
45
|
|
|
40
46
|
## Example
|
|
41
47
|
|
package/imgAttrToPCaption.js
CHANGED
|
@@ -1,49 +1,36 @@
|
|
|
1
1
|
import { markReg } from 'p7d-markdown-it-p-captions'
|
|
2
2
|
|
|
3
|
-
const imgReg = /^( *!\[)(.*?)\]\( *?((.*?)(?:
|
|
3
|
+
const imgReg = /^( *!\[)(.*?)\]\( *?((.*?)(?: +?"(.*?)")?) *?\)( *?\{.*?\})? *$/
|
|
4
4
|
|
|
5
5
|
const imgAttrToPCaption = (state, startLine, opt) => {
|
|
6
|
+
const imgMarkReg = markReg['img']
|
|
6
7
|
let pos = state.bMarks[startLine] + state.tShift[startLine]
|
|
7
8
|
let max = state.eMarks[startLine]
|
|
8
|
-
//console.log('init inline: ' + state.src.slice(pos, max))
|
|
9
9
|
let inline = state.src.slice(pos, max)
|
|
10
10
|
const img = inline.match(imgReg)
|
|
11
11
|
if (!img) return
|
|
12
12
|
|
|
13
|
-
let alt = img[2]
|
|
14
|
-
let title = img[5]
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
if (opt.imgAltCaption) caption = alt
|
|
19
|
-
if (opt.imgTitleCaption) caption = title
|
|
20
|
-
|
|
21
|
-
const hasMarkLabel = caption.match(markReg['img'])
|
|
13
|
+
let alt = img[2] ?? ''
|
|
14
|
+
let title = img[5] ?? ''
|
|
15
|
+
const caption = opt.imgTitleCaption ? title : (opt.imgAltCaption ? alt : '')
|
|
16
|
+
const altCap = typeof opt.imgAltCaption === 'string' ? opt.imgAltCaption : ''
|
|
17
|
+
const titleCap = typeof opt.imgTitleCaption === 'string' ? opt.imgTitleCaption : ''
|
|
22
18
|
|
|
19
|
+
const hasMarkLabel = caption.match(imgMarkReg)
|
|
23
20
|
let modCaption = ''
|
|
24
21
|
if (hasMarkLabel) {
|
|
25
22
|
modCaption = caption
|
|
26
23
|
} else {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
modCaption = (opt.imgAltCaption ? opt.imgAltCaption : opt.imgTitleCaption) + '.'
|
|
31
|
-
} else {
|
|
32
|
-
modCaption = (opt.imgAltCaption ? opt.imgAltCaption : opt.imgTitleCaption) + '. ' + caption
|
|
33
|
-
}
|
|
34
|
-
} else {
|
|
35
|
-
if (caption === '') {
|
|
36
|
-
modCaption = (opt.imgAltCaption ? opt.imgAltCaption : opt.imgTitleCaption) + ' '
|
|
37
|
-
} else {
|
|
38
|
-
modCaption = (opt.imgAltCaption ? opt.imgAltCaption : opt.imgTitleCaption) + ' ' + caption
|
|
39
|
-
}
|
|
40
|
-
}
|
|
24
|
+
const prefix = altCap || titleCap || ''
|
|
25
|
+
if (prefix && /[a-zA-Z]/.test(prefix)) {
|
|
26
|
+
modCaption = caption === '' ? prefix + '.' : prefix + '. ' + caption
|
|
41
27
|
} else {
|
|
42
|
-
modCaption = '
|
|
43
|
-
|
|
28
|
+
modCaption = caption === '' ? prefix + ' ' : prefix + ' ' + caption
|
|
29
|
+
}
|
|
30
|
+
if (!prefix) {
|
|
31
|
+
modCaption = 'Figure.' + (caption !== '' ? ' ' + caption : '')
|
|
44
32
|
}
|
|
45
33
|
}
|
|
46
|
-
//console.log('modCaption: ' + modCaption)
|
|
47
34
|
let token = state.push('paragraph_open', 'p', 1)
|
|
48
35
|
token.map = [startLine, startLine + 1]
|
|
49
36
|
token = state.push('inline', '', 0)
|
|
@@ -58,46 +45,37 @@ const imgAttrToPCaption = (state, startLine, opt) => {
|
|
|
58
45
|
|
|
59
46
|
const setAltToLabel = (state, n) => {
|
|
60
47
|
if (n < 2) return false
|
|
61
|
-
|
|
48
|
+
const imageToken = state.tokens[n+1].children[0]
|
|
49
|
+
if (imageToken.type !== 'image' || !state.tokens[n-2].children) return false
|
|
50
|
+
const prevTokenChild = state.tokens[n-2].children[0]
|
|
62
51
|
if (state.tokens[n-2].children) {
|
|
63
|
-
state.tokens[n+1].content = state.tokens[n+1].content.replace(/^!\[.*?\]/, '![' +
|
|
64
|
-
if (!
|
|
52
|
+
state.tokens[n+1].content = state.tokens[n+1].content.replace(/^!\[.*?\]/, '![' + prevTokenChild.content + ']')
|
|
53
|
+
if (!imageToken.children[0]) {
|
|
65
54
|
const textToken = new state.Token('text', '', 0)
|
|
66
|
-
|
|
55
|
+
imageToken.children.push(textToken)
|
|
67
56
|
}
|
|
68
|
-
|
|
69
|
-
//state.tokens[n+1].children[0].children[0].content = state.tokens[n-2].children[2].content
|
|
70
|
-
// Set img alt to empty value:
|
|
71
|
-
state.tokens[n+1].children[0].children[0].content = ''
|
|
57
|
+
imageToken.children[0].content = ''
|
|
72
58
|
}
|
|
73
|
-
|
|
74
|
-
//state.tokens[n+1].children[0].content = state.tokens[n-2].children[2].content
|
|
75
|
-
// Set img alt to empty value:
|
|
76
|
-
state.tokens[n+1].children[0].content = ''
|
|
77
|
-
//console.log(state.tokens[n+1].children[0])
|
|
59
|
+
imageToken.content = ''
|
|
78
60
|
return true
|
|
79
61
|
}
|
|
80
62
|
|
|
81
63
|
const setTitleToLabel = (state, n) => {
|
|
82
64
|
if (n < 2) return false
|
|
83
|
-
|
|
65
|
+
const imageToken = state.tokens[n+1].children[0]
|
|
66
|
+
if (imageToken.type !== 'image') return false
|
|
84
67
|
if (!state.tokens[n-2].children[0]) return false
|
|
85
|
-
|
|
86
|
-
if (!
|
|
68
|
+
imageToken.attrSet('alt', imageToken.content)
|
|
69
|
+
if (!imageToken.children[0]) {
|
|
87
70
|
const textToken = new state.Token('text', '', 0)
|
|
88
|
-
|
|
71
|
+
imageToken.children.push(textToken)
|
|
89
72
|
}
|
|
90
|
-
let i = 0
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
state.tokens[n+1].children[0].attrs.splice(i, i + 1)
|
|
73
|
+
for (let i = 0; i < imageToken.attrs.length; i++) {
|
|
74
|
+
if (imageToken.attrs[i][0] === 'title') {
|
|
75
|
+
imageToken.attrs.splice(i, 1)
|
|
94
76
|
break
|
|
95
|
-
} else {
|
|
96
|
-
state.tokens[n+1].children[0].attrJoin('title', '')
|
|
97
77
|
}
|
|
98
|
-
i++
|
|
99
78
|
}
|
|
100
|
-
//console.log(state.tokens[n+1].children[0])
|
|
101
79
|
return true
|
|
102
80
|
}
|
|
103
81
|
|