@peaceroad/markown-figure-num-setting 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 +73 -0
- package/index.js +144 -0
- package/package.json +20 -0
- package/test/examples.txt +62 -0
- package/test/test.js +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 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,73 @@
|
|
|
1
|
+
# markown-figure-num-setting
|
|
2
|
+
|
|
3
|
+
Set Figure number.
|
|
4
|
+
|
|
5
|
+
```
|
|
6
|
+
import setMarkdownFigureNum from '@peaceroad/setMarkdownFigureNum'
|
|
7
|
+
|
|
8
|
+
setMarkdownFigureNum(markdownCont)
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
[Input]
|
|
13
|
+
段落。段落。段落。
|
|
14
|
+
|
|
15
|
+
図 キャプション
|
|
16
|
+
|
|
17
|
+

|
|
18
|
+
|
|
19
|
+
段落。段落。段落。
|
|
20
|
+
|
|
21
|
+
図 キャプション
|
|
22
|
+
|
|
23
|
+

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

|
|
33
|
+
|
|
34
|
+
段落。段落。段落。
|
|
35
|
+
|
|
36
|
+
図2 キャプション
|
|
37
|
+
|
|
38
|
+

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

|
|
50
|
+
|
|
51
|
+
段落。段落。段落。
|
|
52
|
+
|
|
53
|
+
図2 キャプション
|
|
54
|
+
|
|
55
|
+

|
|
56
|
+
|
|
57
|
+
段落。段落。段落。
|
|
58
|
+
|
|
59
|
+
[Output]
|
|
60
|
+
段落。段落。段落。
|
|
61
|
+
|
|
62
|
+
図1 キャプション
|
|
63
|
+
|
|
64
|
+

|
|
65
|
+
|
|
66
|
+
段落。段落。段落。
|
|
67
|
+
|
|
68
|
+
図2 キャプション
|
|
69
|
+
|
|
70
|
+

|
|
71
|
+
|
|
72
|
+
段落。段落。段落。
|
|
73
|
+
```
|
package/index.js
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
const setMarkdownFigureNum = (markdown, option) => {
|
|
2
|
+
|
|
3
|
+
let opt = {
|
|
4
|
+
img: true,
|
|
5
|
+
table: true,
|
|
6
|
+
code: false,
|
|
7
|
+
samp: false,
|
|
8
|
+
blockquote: false,
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
opt["pre-code"] = opt.code
|
|
12
|
+
opt["pre-samp"] = opt.samp
|
|
13
|
+
|
|
14
|
+
const markAfterNum = '[A-Z0-9]{1,6}(?:[.-][A-Z0-9]{1,6}){0,5}';
|
|
15
|
+
const markAfterNumAfterJoint = '[.:.。: ]';
|
|
16
|
+
|
|
17
|
+
const markAfterEn = '(?:' +
|
|
18
|
+
'[ ]*' + markAfterNumAfterJoint + '(?:(?=[ ]+)|$)|' +
|
|
19
|
+
'[ ]*' + markAfterNum + markAfterNumAfterJoint + '(?:(?=[ ]+)|$)|' +
|
|
20
|
+
'[ ]*' + markAfterNum + '(?:(?=[ ]+[^a-z])|$)|' +
|
|
21
|
+
'[.]' + markAfterNum + '(?:(?=[ ]+[^a-z])|$)' +
|
|
22
|
+
')';
|
|
23
|
+
const markAfterJa = '(?:' +
|
|
24
|
+
'[ ]*(?:' + markAfterNumAfterJoint + '|(?=[ ]))|' +
|
|
25
|
+
'[ ]*' + markAfterNum + '(?:' + markAfterNumAfterJoint + '(?:(?=[ ])|$))|' +
|
|
26
|
+
'[ ]*' + markAfterNum + '(?:[:。.:: ]|(?=[ ])|$)' +
|
|
27
|
+
')';
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
const markReg = {
|
|
31
|
+
//fig(ure)?, illust, photo
|
|
32
|
+
"img": new RegExp('^(?:' +
|
|
33
|
+
'(?:[fF][iI][gG](:?[uU][rR][eE])?|[iI][lL]{2}[uU][sS][tT]|[pP][hH][oO[tT][oO])'+ markAfterEn + '|' +
|
|
34
|
+
'(?:図|イラスト|写真)' + markAfterJa +
|
|
35
|
+
')'),
|
|
36
|
+
//movie, video
|
|
37
|
+
"video": new RegExp('^(?:' +
|
|
38
|
+
'(?:[mM][oO][vV][iI][eE]|[vV][iI][dD][eE][oO])'+ markAfterEn + '|' +
|
|
39
|
+
'(?:動画|ビデオ)' + markAfterJa +
|
|
40
|
+
')'),
|
|
41
|
+
//table
|
|
42
|
+
"table": new RegExp('^(?:' +
|
|
43
|
+
'(?:[tT][aA][bB][lL][eE])'+ markAfterEn + '|' +
|
|
44
|
+
'(?:表)' + markAfterJa +
|
|
45
|
+
')'),
|
|
46
|
+
//code(block)?, program
|
|
47
|
+
"pre-code": new RegExp('^(?:' +
|
|
48
|
+
'(?:[cC][oO][dD][eE](?:[bB][lL][oO][cC][kK])?|[pP][rR][oO][gG][rR][aA][mM]|[aA][lL][gG][oO][rR][iI[tT][hH][mM])'+ markAfterEn + '|' +
|
|
49
|
+
'(?:(?:ソース)?コード|命令|プログラム|算譜|アルゴリズム|算法)' + markAfterJa +
|
|
50
|
+
')'),
|
|
51
|
+
//terminal, prompt, command
|
|
52
|
+
"pre-samp": new RegExp('^(?:' +
|
|
53
|
+
'(?:[cC][oO][nN][sS][oO][lL][eE]|[tT][eE][rR][mM][iI][nN][aA][lL]|[pP][rR][oO][mM][pP][tT]|[cC][oO][mM]{2}[aA][nN][dD])'+ markAfterEn + '|' +
|
|
54
|
+
'(?:端末|ターミナル|コマンド|(?:コマンド)?プロンプト)' + markAfterJa +
|
|
55
|
+
')'),
|
|
56
|
+
//quote, blockquote
|
|
57
|
+
"blockquote": new RegExp('^(?:' +
|
|
58
|
+
'(?:(?:[bB][lL][oO][cC][kK])?[qQ][uU][oO][tT][eE])'+ markAfterEn + '|' +
|
|
59
|
+
'(?:引用(?:元)?|出典)' + markAfterJa +
|
|
60
|
+
')')
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const label = (hasMarkLabel, hasFigureImageReg) => {
|
|
64
|
+
if (/ */.test(hasFigureImageReg[2])) {
|
|
65
|
+
if (/[^a-zA-Z]/.test(hasMarkLabel[0])) {
|
|
66
|
+
return hasMarkLabel[0].replace(new RegExp(markAfterNum + '$'), '')
|
|
67
|
+
}
|
|
68
|
+
return hasMarkLabel[0]
|
|
69
|
+
}
|
|
70
|
+
return ''
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
let n = 0
|
|
74
|
+
let lines = []
|
|
75
|
+
let lineBreaks = []
|
|
76
|
+
const hasFigureImageReg = /^( *\!\[ *)(.*?)( *\]\(.*?\))/
|
|
77
|
+
const counter = {
|
|
78
|
+
img: 0,
|
|
79
|
+
table: 0,
|
|
80
|
+
"pre-code": 0,
|
|
81
|
+
"pre-samp": 0,
|
|
82
|
+
blockquote: 0,
|
|
83
|
+
}
|
|
84
|
+
lines = markdown.split(/\r\n|\n/)
|
|
85
|
+
lineBreaks = markdown.match(/\r\n|\n/g);
|
|
86
|
+
|
|
87
|
+
if(lines.length === 0) {
|
|
88
|
+
lines.push(markdown)
|
|
89
|
+
}
|
|
90
|
+
while (n < lines.length) {
|
|
91
|
+
lines[n]
|
|
92
|
+
for (let mark of Object.keys(markReg)) {
|
|
93
|
+
const hasMarkLabel = lines[n].match(markReg[mark]);
|
|
94
|
+
//console.log(hasMarkLabel)
|
|
95
|
+
if (hasMarkLabel) {
|
|
96
|
+
counter[mark]++
|
|
97
|
+
lines[n] = lines[n].replace(new RegExp('^([ ]*)' + hasMarkLabel[0]), '$1' + label(hasMarkLabel, hasFigureImageReg) + counter[mark])
|
|
98
|
+
|
|
99
|
+
let hasNextFigureImage = false
|
|
100
|
+
//let hasPrevFigureImage = false
|
|
101
|
+
let i = n + 1
|
|
102
|
+
while (i < lines.length) {
|
|
103
|
+
if (/^\s*$/.test(lines[i])) {
|
|
104
|
+
i++
|
|
105
|
+
continue
|
|
106
|
+
}
|
|
107
|
+
if (lines[i].match(new RegExp(hasFigureImageReg))) {
|
|
108
|
+
lines[i] = lines[i].replace(new RegExp(hasFigureImageReg), '$1$2' + label(hasMarkLabel, hasFigureImageReg) + counter[mark] + '$3')
|
|
109
|
+
hasNextFigureImage = true
|
|
110
|
+
}
|
|
111
|
+
break
|
|
112
|
+
}
|
|
113
|
+
if (!hasNextFigureImage) {
|
|
114
|
+
i = n -1
|
|
115
|
+
while (i >= 0) {
|
|
116
|
+
if (/^\s$/.test(lines[i])) {
|
|
117
|
+
i--
|
|
118
|
+
continue
|
|
119
|
+
}
|
|
120
|
+
if (lines[i].match(new RegExp(hasFigureImageReg))) {
|
|
121
|
+
lines[i] = lines[i].replace(new RegExp(hasFigureImageReg), '$1$2' + label(hasMarkLabel, hasFigureImageReg) + counter[mark] + '$3')
|
|
122
|
+
}
|
|
123
|
+
break
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
n++
|
|
129
|
+
}
|
|
130
|
+
n = 0
|
|
131
|
+
markdown = ''
|
|
132
|
+
while (n < lines.length) {
|
|
133
|
+
if (n === lines.length - 1) {
|
|
134
|
+
markdown += lines[n]
|
|
135
|
+
} else {
|
|
136
|
+
markdown += lines[n] + lineBreaks[n]
|
|
137
|
+
}
|
|
138
|
+
n++
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return markdown
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export default setMarkdownFigureNum
|
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@peaceroad/markown-figure-num-setting",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Set figure number of p7d-markdown-it-p-captions.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "node test/test.js"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/peaceroad/markown-figure-num-setting.git"
|
|
13
|
+
},
|
|
14
|
+
"author": "peaceroad <peaceroad@gmail.com>",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/peaceroad/markown-figure-num-setting/issues"
|
|
18
|
+
},
|
|
19
|
+
"homepage": "https://github.com/peaceroad/markown-figure-num-setting#readme"
|
|
20
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
[Input]
|
|
2
|
+
段落。段落。段落。
|
|
3
|
+
|
|
4
|
+
図 キャプション
|
|
5
|
+
|
|
6
|
+

|
|
7
|
+
|
|
8
|
+
段落。段落。段落。
|
|
9
|
+
|
|
10
|
+
図 キャプション
|
|
11
|
+
|
|
12
|
+

|
|
13
|
+
|
|
14
|
+
段落。段落。段落。
|
|
15
|
+
|
|
16
|
+
[Output]
|
|
17
|
+
段落。段落。段落。
|
|
18
|
+
|
|
19
|
+
図1 キャプション
|
|
20
|
+
|
|
21
|
+

|
|
22
|
+
|
|
23
|
+
段落。段落。段落。
|
|
24
|
+
|
|
25
|
+
図2 キャプション
|
|
26
|
+
|
|
27
|
+

|
|
28
|
+
|
|
29
|
+
段落。段落。段落。
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
[Input]
|
|
34
|
+
段落。段落。段落。
|
|
35
|
+
|
|
36
|
+
図1 キャプション
|
|
37
|
+
|
|
38
|
+

|
|
39
|
+
|
|
40
|
+
段落。段落。段落。
|
|
41
|
+
|
|
42
|
+
図2 キャプション
|
|
43
|
+
|
|
44
|
+

|
|
45
|
+
|
|
46
|
+
段落。段落。段落。
|
|
47
|
+
|
|
48
|
+
[Output]
|
|
49
|
+
段落。段落。段落。
|
|
50
|
+
|
|
51
|
+
図1 キャプション
|
|
52
|
+
|
|
53
|
+

|
|
54
|
+
|
|
55
|
+
段落。段落。段落。
|
|
56
|
+
|
|
57
|
+
図2 キャプション
|
|
58
|
+
|
|
59
|
+

|
|
60
|
+
|
|
61
|
+
段落。段落。段落。
|
|
62
|
+
|
package/test/test.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import assert from 'assert'
|
|
2
|
+
import fs from 'fs'
|
|
3
|
+
import path from 'path'
|
|
4
|
+
import setMarkdownFigureNum 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
|
+
const example = __dirname + path.sep + 'examples.txt'
|
|
12
|
+
const exampleCont = fs.readFileSync(example, 'utf-8').trim()
|
|
13
|
+
let ms = [];
|
|
14
|
+
let ms0 = exampleCont.split(/\n*\[Input\]\n/)
|
|
15
|
+
let n = 1;
|
|
16
|
+
while(n < ms0.length) {
|
|
17
|
+
let mhs = ms0[n].split(/\n+\[Output[^\]]*?\]\n/)
|
|
18
|
+
let i = 1
|
|
19
|
+
while (i < 2) {
|
|
20
|
+
if (mhs[i] === undefined) {
|
|
21
|
+
mhs[i] = ''
|
|
22
|
+
} else {
|
|
23
|
+
mhs[i] = mhs[i].replace(/$/,'\n')
|
|
24
|
+
}
|
|
25
|
+
i++
|
|
26
|
+
}
|
|
27
|
+
ms[n] = {
|
|
28
|
+
inputMarkdown: mhs[0].trim(),
|
|
29
|
+
outputMarkdown: mhs[1].trim(),
|
|
30
|
+
};
|
|
31
|
+
n++
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
n = 1;
|
|
35
|
+
while(n < ms.length) {
|
|
36
|
+
if (n !== 1) { n++; continue }
|
|
37
|
+
console.log('Test: ' + n + ' >>>')
|
|
38
|
+
const m = ms[n].inputMarkdown
|
|
39
|
+
const h = setMarkdownFigureNum(m)
|
|
40
|
+
try {
|
|
41
|
+
assert.strictEqual(h, ms[n].outputMarkdown)
|
|
42
|
+
} catch(e) {
|
|
43
|
+
console.log('incorrect: ')
|
|
44
|
+
console.log('H: ' + h +'\n\nC: ' + ms[n].outputMarkdown)
|
|
45
|
+
}
|
|
46
|
+
n++
|
|
47
|
+
}
|