@peaceroad/markdown-imgattr-to-pcaption 0.1.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/LICENSE +21 -0
- package/README.md +107 -0
- package/index.js +194 -0
- package/package.json +20 -0
- package/test/examples-img-title-attr.txt +87 -0
- package/test/examples-label-lang.txt +16 -0
- package/test/examples.txt +88 -0
- package/test/test.js +77 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 k_taka
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# markdown-imgattr-to-pcaption
|
|
2
|
+
|
|
3
|
+
Change img alt attribute to figure caption paragraph for p7d-markdown-it-p-captions.
|
|
4
|
+
|
|
5
|
+
```
|
|
6
|
+
import setMarkdownImgAttrToPCaption from '@peaceroad/markdown-imgattr-to-pcaption'
|
|
7
|
+
|
|
8
|
+
setMarkdownImgAttrToPCaption(markdownCont)
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
[Input]
|
|
13
|
+
段落。段落。段落。
|
|
14
|
+
|
|
15
|
+

|
|
16
|
+
|
|
17
|
+
段落。段落。段落。
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
[Output]
|
|
21
|
+
段落。段落。段落。
|
|
22
|
+
|
|
23
|
+
図 キャプション
|
|
24
|
+
|
|
25
|
+

|
|
26
|
+
|
|
27
|
+
段落。段落。段落。
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
[Input]
|
|
31
|
+
段落。段落。段落。
|
|
32
|
+
|
|
33
|
+

|
|
34
|
+
|
|
35
|
+
段落。段落。段落。
|
|
36
|
+
|
|
37
|
+
[Output]
|
|
38
|
+
段落。段落。段落。
|
|
39
|
+
|
|
40
|
+
図 キャプション
|
|
41
|
+
|
|
42
|
+

|
|
43
|
+
|
|
44
|
+
段落。段落。段落。
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
[Input]
|
|
49
|
+
段落。段落。段落。
|
|
50
|
+
|
|
51
|
+

|
|
52
|
+
|
|
53
|
+
段落。段落。段落。
|
|
54
|
+
|
|
55
|
+
[Output]
|
|
56
|
+
段落。段落。段落。
|
|
57
|
+
|
|
58
|
+
図1 キャプション
|
|
59
|
+
|
|
60
|
+

|
|
61
|
+
|
|
62
|
+
段落。段落。段落。
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Option
|
|
66
|
+
|
|
67
|
+
### imgTitleCaption: true
|
|
68
|
+
|
|
69
|
+
```
|
|
70
|
+
[Input]
|
|
71
|
+
段落。段落。段落。
|
|
72
|
+
|
|
73
|
+

|
|
74
|
+
|
|
75
|
+
段落。段落。段落。
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
[Output]
|
|
79
|
+
段落。段落。段落。
|
|
80
|
+
|
|
81
|
+
図 キャプション
|
|
82
|
+
|
|
83
|
+

|
|
84
|
+
|
|
85
|
+
段落。段落。段落。
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### labelLang: 'en'
|
|
89
|
+
|
|
90
|
+
```
|
|
91
|
+
[Input]
|
|
92
|
+
段落。段落。段落。
|
|
93
|
+
|
|
94
|
+

|
|
95
|
+
|
|
96
|
+
段落。段落。段落。
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
[Output]
|
|
100
|
+
段落。段落。段落。
|
|
101
|
+
|
|
102
|
+
Figure. キャプション
|
|
103
|
+
|
|
104
|
+

|
|
105
|
+
|
|
106
|
+
段落。段落。段落。
|
|
107
|
+
```
|
package/index.js
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
const setMarkdownImgAttrToPCaption = (markdown, option) => {
|
|
2
|
+
|
|
3
|
+
const opt = {
|
|
4
|
+
imgAltCaption : true,
|
|
5
|
+
imgTitleCaption: false,
|
|
6
|
+
labelLang: 'ja',
|
|
7
|
+
}
|
|
8
|
+
if (option !== undefined) {
|
|
9
|
+
if (option.imgTitleCaption) {
|
|
10
|
+
opt.imgTitleCaption = option.imgTitleCaption
|
|
11
|
+
}
|
|
12
|
+
if (option.labelLang) {
|
|
13
|
+
opt.labelLang = option.labelLang
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
if (opt.imgTitleCaption) opt.imgAltCaption = false
|
|
17
|
+
|
|
18
|
+
let n = 0
|
|
19
|
+
let lines = markdown.split(/\r\n|\n/)
|
|
20
|
+
let lineBreaks = markdown.match(/\r\n|\n/g);
|
|
21
|
+
let isBackquoteCodeBlock = false
|
|
22
|
+
let isTildeCodeBlock = false
|
|
23
|
+
const br = lineBreaks ? lineBreaks[0] : ''
|
|
24
|
+
|
|
25
|
+
if(lines.length === 0) return markdown
|
|
26
|
+
|
|
27
|
+
while (n < lines.length) {
|
|
28
|
+
let isPrevBreakLine = false
|
|
29
|
+
let isNextBreakLine = false
|
|
30
|
+
if (lines[n].match(/^[ \t]*```/)) {
|
|
31
|
+
if (isBackquoteCodeBlock) {
|
|
32
|
+
isBackquoteCodeBlock = false
|
|
33
|
+
} else {
|
|
34
|
+
isBackquoteCodeBlock = true
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
if (lines[n].match(/^[ \t]*~~~/)) {
|
|
38
|
+
if (isTildeCodeBlock) {
|
|
39
|
+
isTildeCodeBlock = false
|
|
40
|
+
} else {
|
|
41
|
+
isTildeCodeBlock = true
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (isBackquoteCodeBlock || isTildeCodeBlock) {
|
|
45
|
+
n++
|
|
46
|
+
continue
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (n === 0) {
|
|
50
|
+
isPrevBreakLine = true
|
|
51
|
+
} else {
|
|
52
|
+
isPrevBreakLine = /^[ \t]*$/.test(lines[n-1])
|
|
53
|
+
}
|
|
54
|
+
if (n === lines.length -1) {
|
|
55
|
+
isNextBreakLine = true
|
|
56
|
+
} else {
|
|
57
|
+
isNextBreakLine = /^[ \t]*$/.test(lines[n+1])
|
|
58
|
+
}
|
|
59
|
+
if (isPrevBreakLine && isNextBreakLine) {
|
|
60
|
+
lines[n] = modLines(n ,lines, br, opt)
|
|
61
|
+
}
|
|
62
|
+
n++
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
n = 0
|
|
66
|
+
markdown = ''
|
|
67
|
+
while (n < lines.length) {
|
|
68
|
+
if (n === lines.length - 1) {
|
|
69
|
+
markdown += lines[n]
|
|
70
|
+
break
|
|
71
|
+
}
|
|
72
|
+
markdown += lines[n] + lineBreaks[n]
|
|
73
|
+
n++
|
|
74
|
+
}
|
|
75
|
+
return markdown
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const modLines = (n, lines, br, opt) => {
|
|
79
|
+
|
|
80
|
+
const markAfterNum = '[A-Z0-9]{1,6}(?:[.-][A-Z0-9]{1,6}){0,5}';
|
|
81
|
+
const joint = '[.:.。: ]';
|
|
82
|
+
const jointFullWidth = '[.。: ]';
|
|
83
|
+
const jointHalfWidth = '[.:]';
|
|
84
|
+
|
|
85
|
+
const markAfterEn = '(?:' +
|
|
86
|
+
' *(?:' +
|
|
87
|
+
jointHalfWidth + '(?:(?=[ ]+)|$)|' +
|
|
88
|
+
jointFullWidth + '|' +
|
|
89
|
+
'(?=[ ]+[^0-9a-zA-Z])' +
|
|
90
|
+
')|' +
|
|
91
|
+
' *' + '(' + markAfterNum + ')(?:' +
|
|
92
|
+
jointHalfWidth + '(?:(?=[ ]+)|$)|' +
|
|
93
|
+
jointFullWidth + '|' +
|
|
94
|
+
'(?=[ ]+[^a-z])|$' +
|
|
95
|
+
')|' +
|
|
96
|
+
'[.](' + markAfterNum + ')(?:' +
|
|
97
|
+
joint + '|(?=[ ]+[^a-z])|$' +
|
|
98
|
+
')' +
|
|
99
|
+
')';
|
|
100
|
+
const markAfterJa = '(?:' +
|
|
101
|
+
' *(?:' +
|
|
102
|
+
jointHalfWidth + '(?:(?=[ ]+)|$)|' +
|
|
103
|
+
jointFullWidth + '|' +
|
|
104
|
+
'(?=[ ]+)' +
|
|
105
|
+
')|' +
|
|
106
|
+
' *' + '(' + markAfterNum + ')(?:' +
|
|
107
|
+
jointHalfWidth + '(?:(?=[ ]+)|$)|' +
|
|
108
|
+
jointFullWidth + '|' +
|
|
109
|
+
'(?=[ ]+)|$' +
|
|
110
|
+
')' +
|
|
111
|
+
')';
|
|
112
|
+
|
|
113
|
+
const labelEn = '(?:[fF][iI][gG](?:[uU][rR][eE])?|[iI][lL]{2}[uU][sS][tT]|[pP][hH][oO][tT][oO])';
|
|
114
|
+
const labelJa = '(?:図|イラスト|写真)';
|
|
115
|
+
|
|
116
|
+
const markReg = {
|
|
117
|
+
//fig(ure)?, illust, photo
|
|
118
|
+
"img": new RegExp('^(?:' + labelEn + markAfterEn + '|' + labelJa + markAfterJa +
|
|
119
|
+
')'),
|
|
120
|
+
}
|
|
121
|
+
const markRegWithNoJoint = {
|
|
122
|
+
"img": new RegExp('^(' + labelEn + '|' + labelJa + ')([ .]?' + markAfterNum + ')?$'),
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
let reg = /^([ \t]*?)!\[ *?(.*?) *?\]\(([^ ]*?)( +"(.*?)")?\)( *(?:{.*?})?)$/
|
|
126
|
+
|
|
127
|
+
const imgLine = lines[n].match(reg)
|
|
128
|
+
if (!imgLine) return lines[n]
|
|
129
|
+
//console.log(imgLine)
|
|
130
|
+
|
|
131
|
+
let hasLabel
|
|
132
|
+
if (opt.imgAltCaption) hasLabel = imgLine[2].match(new RegExp(markReg.img))
|
|
133
|
+
if (opt.imgTitleCaption) hasLabel = imgLine[5].match(new RegExp(markReg.img))
|
|
134
|
+
let hasLabelWithNoJoint
|
|
135
|
+
if (opt.imgAltCaption) hasLabelWithNoJoint = imgLine[2].match(new RegExp(markRegWithNoJoint.img))
|
|
136
|
+
if (opt.imgTitleCaption) hasLabelWithNoJoint = imgLine[5].match(new RegExp(markRegWithNoJoint.img))
|
|
137
|
+
|
|
138
|
+
lines[n] = imgLine[1]
|
|
139
|
+
if (hasLabel) {
|
|
140
|
+
//console.log('With label::')
|
|
141
|
+
if (opt.imgAltCaption) {
|
|
142
|
+
lines[n] += imgLine[2]
|
|
143
|
+
} else if (opt.imgTitleCaption) {
|
|
144
|
+
lines[n] += imgLine[5]
|
|
145
|
+
}
|
|
146
|
+
lines[n] += br + br + imgLine[1] + '!['
|
|
147
|
+
if (opt.imgTitleCaption) lines[n] += imgLine[2]
|
|
148
|
+
lines[n] += ']'
|
|
149
|
+
} else if (hasLabelWithNoJoint) {
|
|
150
|
+
//console.log('With label (no joint)::')
|
|
151
|
+
//console.log(hasLabelWithNoJoint)
|
|
152
|
+
if (opt.imgAltCaption) {
|
|
153
|
+
lines[n] += imgLine[2]
|
|
154
|
+
} else if (opt.imgTitleCaption) {
|
|
155
|
+
lines[n] += imgLine[5]
|
|
156
|
+
}
|
|
157
|
+
lines[n] += br + br + imgLine[1] + '![' + hasLabelWithNoJoint[0].replace(new RegExp(joint + '$')) + ']'
|
|
158
|
+
} else {
|
|
159
|
+
//console.log('No label::')
|
|
160
|
+
if (opt.labelLang === 'ja') {
|
|
161
|
+
if (opt.imgAltCaption) {
|
|
162
|
+
if (imgLine[2]) {
|
|
163
|
+
lines[n] += '図 ' + imgLine[2] + br + br + imgLine[1] + '![]'
|
|
164
|
+
} else {
|
|
165
|
+
lines[n] += '図' + br + br + imgLine[1] + '![]'
|
|
166
|
+
}
|
|
167
|
+
} else if (opt.imgTitleCaption) {
|
|
168
|
+
if (imgLine[5]) {
|
|
169
|
+
lines[n] += '図 ' + imgLine[5] + br + br + imgLine[1] + '![' + imgLine[2] + ']'
|
|
170
|
+
} else {
|
|
171
|
+
lines[n] += '図' + br + br + imgLine[1] + '![' + imgLine[2] + ']'
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
} else if (opt.labelLang === 'en') {
|
|
175
|
+
if (opt.imgAltCaption) {
|
|
176
|
+
if (imgLine[2]) {
|
|
177
|
+
lines[n] += 'Figure. ' + imgLine[2] + br + br + imgLine[1] +'![]'
|
|
178
|
+
} else {
|
|
179
|
+
lines[n] += 'Figure.' + br + br + imgLine[1] +'![]'
|
|
180
|
+
}
|
|
181
|
+
} else if (opt.imgTitleCaption) {
|
|
182
|
+
if (imgLine[5]) {
|
|
183
|
+
lines[n] += 'Figure. ' + imgLine[5] + br + br + imgLine[1] + '![' + imgLine[2] + ']'
|
|
184
|
+
} else {
|
|
185
|
+
lines[n] += 'Figure. ' + br + br + imgLine[1] + '![' + imgLine[2] + ']'
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
lines[n] += '(' + imgLine[3] + ')' + imgLine[6]
|
|
191
|
+
return lines[n]
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export default setMarkdownImgAttrToPCaption
|
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@peaceroad/markdown-imgattr-to-pcaption",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Change img alt attribute to figure caption paragraph for p7d-markdown-it-p-captions.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "node test/test.js"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/peaceroad/markdown-imgattr-to-pcaption.git"
|
|
13
|
+
},
|
|
14
|
+
"author": "peaceroad <peaceroad@gmail.com>",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/peaceroad/markdown-imgattr-to-pcaption/issues"
|
|
18
|
+
},
|
|
19
|
+
"homepage": "https://github.com/peaceroad/markdown-imgattr-to-pcaption#readme"
|
|
20
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
[Input]
|
|
2
|
+
段落。段落。段落。
|
|
3
|
+
|
|
4
|
+

|
|
5
|
+
|
|
6
|
+
段落。段落。段落。
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
[Output]
|
|
10
|
+
段落。段落。段落。
|
|
11
|
+
|
|
12
|
+
図 キャプション
|
|
13
|
+
|
|
14
|
+

|
|
15
|
+
|
|
16
|
+
段落。段落。段落。
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
[Input]
|
|
20
|
+
段落。段落。段落。
|
|
21
|
+
|
|
22
|
+

|
|
23
|
+
|
|
24
|
+
段落。段落。段落。
|
|
25
|
+
|
|
26
|
+
[Output]
|
|
27
|
+
段落。段落。段落。
|
|
28
|
+
|
|
29
|
+
図 キャプション
|
|
30
|
+
|
|
31
|
+

|
|
32
|
+
|
|
33
|
+
段落。段落。段落。
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
[Input]
|
|
38
|
+
段落。段落。段落。
|
|
39
|
+
|
|
40
|
+

|
|
41
|
+
|
|
42
|
+
段落。段落。段落。
|
|
43
|
+
|
|
44
|
+
[Output]
|
|
45
|
+
段落。段落。段落。
|
|
46
|
+
|
|
47
|
+
図1 キャプション
|
|
48
|
+
|
|
49
|
+

|
|
50
|
+
|
|
51
|
+
段落。段落。段落。
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
[Input]
|
|
56
|
+
段落。段落。段落。
|
|
57
|
+
|
|
58
|
+

|
|
59
|
+
|
|
60
|
+
段落。段落。段落。
|
|
61
|
+
|
|
62
|
+
[Output]
|
|
63
|
+
段落。段落。段落。
|
|
64
|
+
|
|
65
|
+
図1 キャプション
|
|
66
|
+
|
|
67
|
+

|
|
68
|
+
|
|
69
|
+
段落。段落。段落。
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
[Input]
|
|
74
|
+
段落。段落。段落。
|
|
75
|
+
|
|
76
|
+

|
|
77
|
+
|
|
78
|
+
段落。段落。段落。
|
|
79
|
+
|
|
80
|
+
[Output]
|
|
81
|
+
段落。段落。段落。
|
|
82
|
+
|
|
83
|
+
図1:キャプション
|
|
84
|
+
|
|
85
|
+

|
|
86
|
+
|
|
87
|
+
段落。段落。段落。
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
[Input]
|
|
2
|
+
段落。段落。段落。
|
|
3
|
+
|
|
4
|
+

|
|
5
|
+
|
|
6
|
+
段落。段落。段落。
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
[Output]
|
|
10
|
+
段落。段落。段落。
|
|
11
|
+
|
|
12
|
+
図 キャプション
|
|
13
|
+
|
|
14
|
+

|
|
15
|
+
|
|
16
|
+
段落。段落。段落。
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
[Input]
|
|
20
|
+
段落。段落。段落。
|
|
21
|
+
|
|
22
|
+

|
|
23
|
+
|
|
24
|
+
段落。段落。段落。
|
|
25
|
+
|
|
26
|
+
[Output]
|
|
27
|
+
段落。段落。段落。
|
|
28
|
+
|
|
29
|
+
図 キャプション
|
|
30
|
+
|
|
31
|
+

|
|
32
|
+
|
|
33
|
+
段落。段落。段落。
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
[Input]
|
|
38
|
+
段落。段落。段落。
|
|
39
|
+
|
|
40
|
+

|
|
41
|
+
|
|
42
|
+
段落。段落。段落。
|
|
43
|
+
|
|
44
|
+
[Output]
|
|
45
|
+
段落。段落。段落。
|
|
46
|
+
|
|
47
|
+
図1 キャプション
|
|
48
|
+
|
|
49
|
+

|
|
50
|
+
|
|
51
|
+
段落。段落。段落。
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
[Input]
|
|
56
|
+
段落。段落。段落。
|
|
57
|
+
|
|
58
|
+

|
|
59
|
+
|
|
60
|
+
段落。段落。段落。
|
|
61
|
+
|
|
62
|
+
[Output]
|
|
63
|
+
段落。段落。段落。
|
|
64
|
+
|
|
65
|
+
図1 キャプション
|
|
66
|
+
|
|
67
|
+

|
|
68
|
+
|
|
69
|
+
段落。段落。段落。
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
[Input]
|
|
73
|
+
段落。段落。段落。
|
|
74
|
+
|
|
75
|
+

|
|
76
|
+
|
|
77
|
+
段落。段落。段落。
|
|
78
|
+
|
|
79
|
+
[Output]
|
|
80
|
+
段落。段落。段落。
|
|
81
|
+
|
|
82
|
+
図1:キャプション
|
|
83
|
+
|
|
84
|
+

|
|
85
|
+
|
|
86
|
+
段落。段落。段落。
|
|
87
|
+
|
|
88
|
+
|
package/test/test.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import assert from 'assert'
|
|
2
|
+
import fs from 'fs'
|
|
3
|
+
import path from 'path'
|
|
4
|
+
import setMarkdownImgAttrToPCaption from '../index.js'
|
|
5
|
+
|
|
6
|
+
let __dirname = path.dirname(new URL(import.meta.url).pathname)
|
|
7
|
+
const isWindows = (process.platform === 'win32')
|
|
8
|
+
if (isWindows) {
|
|
9
|
+
__dirname = __dirname.replace(/^\/+/, '').replace(/\//g, '\\')
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const check = (name, ex) => {
|
|
13
|
+
const exCont = fs.readFileSync(ex, 'utf-8').trim()
|
|
14
|
+
let ms = [];
|
|
15
|
+
let ms0 = exCont.split(/\n*\[Input\]\n/)
|
|
16
|
+
let n = 1;
|
|
17
|
+
while(n < ms0.length) {
|
|
18
|
+
let mhs = ms0[n].split(/\n+\[Output[^\]]*?\]\n/)
|
|
19
|
+
let i = 1
|
|
20
|
+
while (i < 2) {
|
|
21
|
+
if (mhs[i] === undefined) {
|
|
22
|
+
mhs[i] = ''
|
|
23
|
+
} else {
|
|
24
|
+
mhs[i] = mhs[i].replace(/$/,'\n')
|
|
25
|
+
}
|
|
26
|
+
i++
|
|
27
|
+
}
|
|
28
|
+
ms[n] = {
|
|
29
|
+
inputMarkdown: mhs[0].trim(),
|
|
30
|
+
outputMarkdown: mhs[1].trim(),
|
|
31
|
+
};
|
|
32
|
+
n++
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
n = 1
|
|
36
|
+
while(n < ms.length) {
|
|
37
|
+
//if (n !== 10) { n++; continue }
|
|
38
|
+
console.log('Test: ' + n + ' >>>')
|
|
39
|
+
const m = ms[n].inputMarkdown
|
|
40
|
+
let h
|
|
41
|
+
let option = {}
|
|
42
|
+
if (name === 'default') {
|
|
43
|
+
h = setMarkdownImgAttrToPCaption(m)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
if (name === 'imgTitleAttr') {
|
|
48
|
+
h = setMarkdownImgAttrToPCaption(m, {imgTitleCaption: true})
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (name === 'labelLang') {
|
|
52
|
+
h = setMarkdownImgAttrToPCaption(m, {labelLang: 'en'})
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
try {
|
|
57
|
+
assert.strictEqual(h, ms[n].outputMarkdown)
|
|
58
|
+
} catch(e) {
|
|
59
|
+
console.log('incorrect: ')
|
|
60
|
+
//console.log(m)
|
|
61
|
+
//console.log('::convert ->')
|
|
62
|
+
console.log('H: ' + h +'\n\nC: ' + ms[n].outputMarkdown)
|
|
63
|
+
}
|
|
64
|
+
n++
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
const example = {
|
|
70
|
+
default: __dirname + path.sep + 'examples.txt',
|
|
71
|
+
imgTitleAttr: __dirname + path.sep + 'examples-img-title-attr.txt',
|
|
72
|
+
labelLang: __dirname + path.sep + 'examples-label-lang.txt',
|
|
73
|
+
}
|
|
74
|
+
for (let ex in example) {
|
|
75
|
+
console.log('[Test] ' + ex)
|
|
76
|
+
check(ex, example[ex])
|
|
77
|
+
}
|