@peaceroad/markdown-it-figure-with-p-caption 0.13.0 → 0.14.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 +293 -279
- package/index.js +712 -368
- package/package.json +4 -4
- package/imgAttrToPCaption.js +0 -82
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peaceroad/markdown-it-figure-with-p-caption",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
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",
|
|
@@ -17,17 +17,17 @@
|
|
|
17
17
|
"url": "https://github.com/peaceroad/p7d-markdown-it-figure-with-p-caption/issues"
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|
|
20
|
-
"@peaceroad/markdown-it-renderer-fence": "^0.
|
|
20
|
+
"@peaceroad/markdown-it-renderer-fence": "^0.2.0",
|
|
21
|
+
"@peaceroad/markdown-it-renderer-image": "^0.5.2",
|
|
21
22
|
"highlight.js": "^11.11.1",
|
|
22
23
|
"markdown-it": "^14.1.0",
|
|
23
24
|
"markdown-it-attrs": "^4.3.1"
|
|
24
25
|
},
|
|
25
26
|
"dependencies": {
|
|
26
|
-
"p7d-markdown-it-p-captions": "^0.
|
|
27
|
+
"p7d-markdown-it-p-captions": "^0.18.1"
|
|
27
28
|
},
|
|
28
29
|
"files": [
|
|
29
30
|
"index.js",
|
|
30
|
-
"imgAttrToPCaption.js",
|
|
31
31
|
"README.md",
|
|
32
32
|
"LICENSE"
|
|
33
33
|
]
|
package/imgAttrToPCaption.js
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import { markReg } from 'p7d-markdown-it-p-captions'
|
|
2
|
-
|
|
3
|
-
const imgReg = /^( *!\[)(.*?)\]\( *?((.*?)(?: +?"(.*?)")?) *?\)( *?\{.*?\})? *$/
|
|
4
|
-
|
|
5
|
-
const imgAttrToPCaption = (state, startLine, opt) => {
|
|
6
|
-
const imgMarkReg = markReg['img']
|
|
7
|
-
let pos = state.bMarks[startLine] + state.tShift[startLine]
|
|
8
|
-
let max = state.eMarks[startLine]
|
|
9
|
-
let inline = state.src.slice(pos, max)
|
|
10
|
-
const img = inline.match(imgReg)
|
|
11
|
-
if (!img) return
|
|
12
|
-
|
|
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 : ''
|
|
18
|
-
|
|
19
|
-
const hasMarkLabel = caption.match(imgMarkReg)
|
|
20
|
-
let modCaption = ''
|
|
21
|
-
if (hasMarkLabel) {
|
|
22
|
-
modCaption = caption
|
|
23
|
-
} else {
|
|
24
|
-
const prefix = altCap || titleCap || ''
|
|
25
|
-
if (prefix && /[a-zA-Z]/.test(prefix)) {
|
|
26
|
-
modCaption = caption === '' ? prefix + '.' : prefix + '. ' + caption
|
|
27
|
-
} else {
|
|
28
|
-
modCaption = caption === '' ? prefix + ' ' : prefix + ' ' + caption
|
|
29
|
-
}
|
|
30
|
-
if (!prefix) {
|
|
31
|
-
modCaption = 'Figure.' + (caption !== '' ? ' ' + caption : '')
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
let token = state.push('paragraph_open', 'p', 1)
|
|
35
|
-
token.map = [startLine, startLine + 1]
|
|
36
|
-
token = state.push('inline', '', 0)
|
|
37
|
-
token.content = modCaption
|
|
38
|
-
token.children = [new state.Token('text', modCaption, 0)]
|
|
39
|
-
if (!opt.setFigureNumber) {
|
|
40
|
-
if(caption === '') token.attrs = [['class', 'nocaption']]
|
|
41
|
-
}
|
|
42
|
-
token = state.push('paragraph_close', 'p', -1)
|
|
43
|
-
return true
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const setAltToLabel = (state, n) => {
|
|
47
|
-
if (n < 2) return false
|
|
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]
|
|
51
|
-
if (state.tokens[n-2].children) {
|
|
52
|
-
state.tokens[n+1].content = state.tokens[n+1].content.replace(/^!\[.*?\]/, '![' + prevTokenChild.content + ']')
|
|
53
|
-
if (!imageToken.children[0]) {
|
|
54
|
-
const textToken = new state.Token('text', '', 0)
|
|
55
|
-
imageToken.children.push(textToken)
|
|
56
|
-
}
|
|
57
|
-
imageToken.children[0].content = ''
|
|
58
|
-
}
|
|
59
|
-
imageToken.content = ''
|
|
60
|
-
return true
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const setTitleToLabel = (state, n) => {
|
|
64
|
-
if (n < 2) return false
|
|
65
|
-
const imageToken = state.tokens[n+1].children[0]
|
|
66
|
-
if (imageToken.type !== 'image') return false
|
|
67
|
-
if (!state.tokens[n-2].children[0]) return false
|
|
68
|
-
imageToken.attrSet('alt', imageToken.content)
|
|
69
|
-
if (!imageToken.children[0]) {
|
|
70
|
-
const textToken = new state.Token('text', '', 0)
|
|
71
|
-
imageToken.children.push(textToken)
|
|
72
|
-
}
|
|
73
|
-
for (let i = 0; i < imageToken.attrs.length; i++) {
|
|
74
|
-
if (imageToken.attrs[i][0] === 'title') {
|
|
75
|
-
imageToken.attrs.splice(i, 1)
|
|
76
|
-
break
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
return true
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export { imgAttrToPCaption, setAltToLabel, setTitleToLabel }
|