@peaceroad/markown-figure-num-setting 0.1.1 → 0.1.3

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 CHANGED
@@ -69,5 +69,36 @@ setMarkdownFigureNum(markdownCont)
69
69
 
70
70
  ![図2](image.jpg)
71
71
 
72
+ 段落。段落。段落。
73
+
74
+
75
+ [Input]
76
+ 段落。段落。段落。
77
+
78
+ Figure. キャプション
79
+
80
+ ![](image.jpg)
81
+
82
+ 段落。段落。段落。
83
+
84
+ Figure. キャプション
85
+
86
+ ![](image.jpg)
87
+
88
+ 段落。段落。段落。
89
+
90
+ [Output]
91
+ 段落。段落。段落。
92
+
93
+ Figure1. キャプション
94
+
95
+ ![Figure 1](image.jpg)
96
+
97
+ 段落。段落。段落。
98
+
99
+ Figure 2. キャプション
100
+
101
+ ![Figure 2](image.jpg)
102
+
72
103
  段落。段落。段落。
73
104
  ```
package/index.js CHANGED
@@ -2,35 +2,59 @@ const setMarkdownFigureNum = (markdown, option) => {
2
2
 
3
3
  let opt = {
4
4
  img: true,
5
- table: true,
5
+ table: false,
6
6
  code: false,
7
7
  samp: false,
8
8
  blockquote: false,
9
+ slide: false,
9
10
  }
10
-
11
11
  opt["pre-code"] = opt.code
12
12
  opt["pre-samp"] = opt.samp
13
+ if (option !== undefined) {
14
+ for (let o in option) {
15
+ opt[o] = option[o];
16
+ }
17
+ }
18
+
13
19
 
14
20
  const markAfterNum = '[A-Z0-9]{1,6}(?:[.-][A-Z0-9]{1,6}){0,5}';
15
- const markAfterNumAfterJoint = '[.:.。: ]';
21
+ const joint = '[.:.。: ]';
22
+ const jointFullWidth = '[.。: ]';
23
+ const jointHalfWidth = '[.:]';
16
24
 
17
25
  const markAfterEn = '(?:' +
18
- '[  ]*' + markAfterNumAfterJoint + '(?:(?=[ ]+)|$)|' +
19
- '[  ]*' + markAfterNum + markAfterNumAfterJoint + '(?:(?=[ ]+)|$)|' +
20
- '[  ]*' + markAfterNum + '(?:(?=[  ]+[^a-z])|$)|' +
21
- '[.]' + markAfterNum + '(?:(?=[  ]+[^a-z])|$)' +
26
+ ' *(?:' +
27
+ jointHalfWidth + '(?:(?=[ ]+)|$)|' +
28
+ jointFullWidth + '|' +
29
+ '(?=[ ]+[^0-9a-zA-Z])' +
30
+ ')|' +
31
+ ' *' + '(' + markAfterNum + ')(?:' +
32
+ jointHalfWidth + '(?:(?=[ ]+)|$)|' +
33
+ jointFullWidth + '|' +
34
+ '(?=[ ]+[^a-z])|$' +
35
+ ')|' +
36
+ '[.](' + markAfterNum + ')(?:' +
37
+ joint + '|(?=[ ]+[^a-z])|$' +
38
+ ')' +
22
39
  ')';
40
+
23
41
  const markAfterJa = '(?:' +
24
- '[  ]*(?:' + markAfterNumAfterJoint + '|(?=[ ]))|' +
25
- '[  ]*' + markAfterNum + '(?:' + markAfterNumAfterJoint + '(?:(?=[ ])|$))|' +
26
- '[  ]*' + markAfterNum + '(?:[:。.:: ]|(?=[ ])|$)' +
42
+ ' *(?:' +
43
+ jointHalfWidth + '(?:(?=[ ]+)|$)|' +
44
+ jointFullWidth + '|' +
45
+ '(?=[ ]+)' +
46
+ ')|' +
47
+ ' *' + '(' + markAfterNum + ')(?:' +
48
+ jointHalfWidth + '(?:(?=[ ]+)|$)|' +
49
+ jointFullWidth + '|' +
50
+ '(?=[ ]+)|$' +
51
+ ')' +
27
52
  ')';
28
53
 
29
-
30
54
  const markReg = {
31
55
  //fig(ure)?, illust, photo
32
56
  "img": new RegExp('^(?:' +
33
- '(?:[fF][iI][gG](:?[uU][rR][eE])?|[iI][lL]{2}[uU][sS][tT]|[pP][hH][oO[tT][oO])'+ markAfterEn + '|' +
57
+ '(?:[fF][iI][gG](?:[uU][rR][eE])?|[iI][lL]{2}[uU][sS][tT]|[pP][hH][oO[tT][oO])'+ markAfterEn + '|' +
34
58
  '(?:図|イラスト|写真)' + markAfterJa +
35
59
  ')'),
36
60
  //movie, video
@@ -46,82 +70,151 @@ const setMarkdownFigureNum = (markdown, option) => {
46
70
  //code(block)?, program
47
71
  "pre-code": new RegExp('^(?:' +
48
72
  '(?:[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 +
73
+ '(?:(?:ソース)?コード|リスト|命令|プログラム|算譜|アルゴリズム|算法)' + markAfterJa +
50
74
  ')'),
51
75
  //terminal, prompt, command
52
76
  "pre-samp": new RegExp('^(?:' +
53
77
  '(?:[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
78
  '(?:端末|ターミナル|コマンド|(?:コマンド)?プロンプト)' + markAfterJa +
55
79
  ')'),
56
- //quote, blockquote
80
+ //quote, blockquote, source
57
81
  "blockquote": new RegExp('^(?:' +
58
- '(?:(?:[bB][lL][oO][cC][kK])?[qQ][uU][oO][tT][eE])'+ markAfterEn + '|' +
82
+ '(?:(?:[bB][lL][oO][cC][kK])?[qQ][uU][oO][tT][eE]|[sS][oO][uU][rR][cC][eE])'+ markAfterEn + '|' +
59
83
  '(?:引用(?:元)?|出典)' + markAfterJa +
84
+ ')'),
85
+ //slide
86
+ "slide": new RegExp('^(?:' +
87
+ '(?:[sS][lL][iI][dD][eE])'+ markAfterEn + '|' +
88
+ '(?:スライド)' + markAfterJa +
60
89
  ')')
90
+ };
91
+
92
+ const label = (hasMarkLabel, counter, isAlt) => {
93
+
94
+ let label = hasMarkLabel[0]
95
+
96
+ if (hasMarkLabel[3]) {
97
+ label = hasMarkLabel[0].replace(new RegExp(hasMarkLabel[3] + '$'), '')
98
+ }
99
+ let isLabelLastJoint = label.match(new RegExp('(' + joint +')$'))
100
+ if (isLabelLastJoint) {
101
+ if (isAlt) {
102
+ label = label.replace(new RegExp(joint +'$'), '') + ' ' + counter
103
+ } else {
104
+ label = label.replace(new RegExp(joint +'$'), '') + ' ' + counter + isLabelLastJoint[1]
105
+ }
106
+ } else {
107
+ label += counter
108
+ }
109
+ return label
61
110
  }
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 + '$'), '')
111
+
112
+
113
+ const setImageAltNumber = (lines, n, mark, hasMarkLabel, counter) => {
114
+ let hasPrevFigureImage = false
115
+ let isFigureImage
116
+ const figureImageReg = /^([ \t]*\!\[) *?(.*?([0-9]*)) *?(\]\(.*?\))/
117
+ let i
118
+ i = n - 1
119
+ //console.log('lines[n]: ' + lines[n])
120
+ //console.log('CheckPrevLine')
121
+ while (i >= 0) {
122
+ if (/^[ \t]*$/.test(lines[i])) {
123
+ i--
124
+ continue
125
+ }
126
+ isFigureImage = lines[i].match(new RegExp(figureImageReg))
127
+ if (!isFigureImage) break
128
+ //console.log(isFigureImage)
129
+
130
+ let j = i
131
+ while (j >= 0) {
132
+ if (/^[ \t]*$/.test(lines[j])) {
133
+ j--
134
+ continue
135
+ }
136
+ //console.log(isFigureImage[3], (counter.img - 1).toString())
137
+ if (isFigureImage[3] === (counter.img - 1).toString()) {
138
+ hasPrevFigureImage = false
139
+ break
140
+ }
141
+ lines[i] = lines[i].replace(new RegExp(figureImageReg), '$1' + label(hasMarkLabel, counter[mark], true) + '$4')
142
+ //console.log('ChangePrevLine: ' + lines[i])
143
+ hasPrevFigureImage = true
144
+ break
145
+ }
146
+ break
147
+ }
148
+
149
+ if (hasPrevFigureImage) return
150
+
151
+ //console.log('CheckNextLine')
152
+ i = n + 1
153
+ while (i < lines.length) {
154
+ if (/^[\t ]*$/.test(lines[i])) {
155
+ i++
156
+ continue
157
+ }
158
+ isFigureImage = lines[i].match(new RegExp(figureImageReg))
159
+ //console.log(isFigureImage)
160
+ if (isFigureImage) {
161
+ lines[i] = lines[i].replace(new RegExp(figureImageReg), '$1' + label(hasMarkLabel, counter[mark], true) + '$4')
162
+ //console.log('ChangeNextLine: ' + lines[i])
67
163
  }
68
- return hasMarkLabel[0]
164
+ break
69
165
  }
70
- return ''
166
+ return
71
167
  }
72
168
 
73
169
  let n = 0
74
170
  let lines = []
75
171
  let lineBreaks = []
76
- const hasFigureImageReg = /^( *\!\[ *)(.*?)( *\]\(.*?\))/
77
172
  const counter = {
78
173
  img: 0,
79
174
  table: 0,
80
175
  "pre-code": 0,
81
176
  "pre-samp": 0,
82
177
  blockquote: 0,
178
+ slide: 0,
83
179
  }
180
+
181
+
84
182
  lines = markdown.split(/\r\n|\n/)
85
183
  lineBreaks = markdown.match(/\r\n|\n/g);
184
+ let isBackquoteCodeBlock = false
185
+ let isTildeCodeBlock = false
186
+
187
+ if(lines.length === 0) return markdown
86
188
 
87
- if(lines.length === 0) {
88
- lines.push(markdown)
89
- }
90
189
  while (n < lines.length) {
91
- lines[n]
190
+ if (lines[n].match(/^ *```/)) {
191
+ if (isBackquoteCodeBlock) {
192
+ isBackquoteCodeBlock = false
193
+ } else {
194
+ isBackquoteCodeBlock = true
195
+ }
196
+ }
197
+ if (lines[n].match(/^ *~~~/)) {
198
+ if (isTildeCodeBlock) {
199
+ isTildeCodeBlock = false
200
+ } else {
201
+ isTildeCodeBlock = true
202
+ }
203
+ }
204
+ if (isBackquoteCodeBlock || isTildeCodeBlock) {
205
+ n++
206
+ continue
207
+ }
208
+
92
209
  for (let mark of Object.keys(markReg)) {
93
210
  const hasMarkLabel = lines[n].match(markReg[mark]);
94
- //console.log(hasMarkLabel)
95
- if (hasMarkLabel) {
211
+ //if (hasMarkLabel) console.log(hasMarkLabel)
212
+ if (hasMarkLabel && opt[mark]) {
96
213
  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' + 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' + label(hasMarkLabel, hasFigureImageReg) + counter[mark] + '$3')
122
- }
123
- break
124
- }
214
+ //console.log('lines[n]: ' + lines[n])
215
+ lines[n] = lines[n].replace(new RegExp('^([ \t]*)' + hasMarkLabel[0]), '$1' + label(hasMarkLabel, counter[mark]))
216
+ if (mark === 'img') {
217
+ setImageAltNumber(lines, n, mark, hasMarkLabel, counter)
125
218
  }
126
219
  }
127
220
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@peaceroad/markown-figure-num-setting",
3
- "version": "0.1.1",
4
- "description": "Set figure number of p7d-markdown-it-p-captions.",
3
+ "version": "0.1.3",
4
+ "description": "Set figure number for p7d-markdown-it-p-captions.",
5
5
  "type": "module",
6
6
  "main": "index.js",
7
7
  "scripts": {
package/test/examples.txt CHANGED
@@ -39,7 +39,7 @@
39
39
 
40
40
  段落。段落。段落。
41
41
 
42
- 2 キャプション
42
+ 3 キャプション
43
43
 
44
44
  ![](image.jpg)
45
45
 
@@ -61,33 +61,138 @@
61
61
  段落。段落。段落。
62
62
 
63
63
 
64
+
64
65
  [Input]
65
- 段落。段落。段落。
66
+ 図 キャプション
67
+
68
+ ![図1](image.jpg)
66
69
 
67
70
  図 キャプション
68
71
 
72
+ ![図2](image.jpg)
73
+
74
+ [Output]
75
+ 図1 キャプション
76
+
69
77
  ![図1](image.jpg)
70
78
 
71
- 段落。段落。段落。
79
+ 図2 キャプション
80
+
81
+ ![図2](image.jpg)
82
+
83
+
84
+
85
+ [Input]
86
+ ![図1](image.jpg)
72
87
 
73
88
  図 キャプション
74
89
 
75
90
  ![図2](image.jpg)
76
91
 
92
+ 図 キャプション
93
+ [Output]
94
+ ![図1](image.jpg)
95
+
96
+ 図1 キャプション
97
+
98
+ ![図2](image.jpg)
99
+
100
+ 図2 キャプション
101
+
102
+ [Input]
103
+ 段落。段落。段落。
104
+
105
+ ```
106
+ 図 キャプション
107
+
108
+ ![図1](image.jpg)
109
+ ```
110
+
111
+ ```
112
+ Code. Caption
113
+ ```
114
+
77
115
  段落。段落。段落。
78
116
 
79
117
  [Output]
80
118
  段落。段落。段落。
81
119
 
82
- 図1 キャプション
120
+ ```
121
+ 図 キャプション
83
122
 
84
123
  ![図1](image.jpg)
124
+ ```
125
+
126
+ ```
127
+ Code. Caption
128
+ ```
129
+
130
+ 段落。段落。段落。
85
131
 
132
+
133
+ [Input]
86
134
  段落。段落。段落。
87
135
 
136
+ Figure. キャプション
137
+
138
+ ![](image.jpg)
139
+
140
+ 段落。段落。段落。
141
+
142
+ Figure. キャプション
143
+
144
+ ![](image.jpg)
145
+
146
+ 段落。段落。段落。
147
+
148
+ [Output]
149
+ 段落。段落。段落。
150
+
151
+ Figure 1. キャプション
152
+
153
+ ![Figure 1](image.jpg)
154
+
155
+ 段落。段落。段落。
156
+
157
+ Figure 2. キャプション
158
+
159
+ ![Figure 2](image.jpg)
160
+
161
+ 段落。段落。段落。
162
+
163
+
164
+ [Input]
165
+ 図 キャプション
166
+
167
+ ![](image.jpg)
168
+
169
+ 図 キャプション
170
+
171
+ ![](image.jpg)
172
+
173
+ [Output]
174
+ 図1 キャプション
175
+
176
+ ![図1](image.jpg)
177
+
88
178
  図2 キャプション
89
179
 
90
180
  ![図2](image.jpg)
91
181
 
92
- 段落。段落。段落。
93
182
 
183
+ [Input]
184
+ ![](image.jpg)
185
+
186
+ 図 キャプション
187
+
188
+ ![](image.jpg)
189
+
190
+ 図 キャプション
191
+ [Output]
192
+ ![図1](image.jpg)
193
+
194
+ 図1 キャプション
195
+
196
+ ![図2](image.jpg)
197
+
198
+ 図2 キャプション
@@ -0,0 +1,28 @@
1
+ [Input]
2
+ This line is [Figure][#flag] paragraph.
3
+
4
+ Figure. A caption.
5
+
6
+ ![](cat.jpg) {#flag}
7
+
8
+ Figure. A caption.
9
+
10
+ ![](cat.jpg) {#flag2}
11
+
12
+ A paragraph. [Figure][#flag2] paragraph.
13
+
14
+ [Output]
15
+ This line is [Figure 1][] paragraph.
16
+
17
+ Figure 1. A caption.
18
+
19
+ ![Figure 1](cat.jpg) {#figure1}
20
+
21
+ Figure 2. A caption.
22
+
23
+ ![Figure 2](cat.jpg) {#figure2}
24
+
25
+ A paragraph. [Figure 2][] paragraph.
26
+
27
+ [Figure 1]: #figure1
28
+ [Figure 2]: #figure2
package/test/test.js CHANGED
@@ -33,7 +33,7 @@ while(n < ms0.length) {
33
33
 
34
34
  n = 1;
35
35
  while(n < ms.length) {
36
- //if (n !== 1) { n++; continue }
36
+ //if (n !== 8) { n++; continue }
37
37
  console.log('Test: ' + n + ' >>>')
38
38
  const m = ms[n].inputMarkdown
39
39
  const h = setMarkdownFigureNum(m)