@peaceroad/markown-figure-num-setting 0.1.2 → 0.1.4

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
@@ -102,3 +102,26 @@ Figure 2. キャプション
102
102
 
103
103
  段落。段落。段落。
104
104
  ```
105
+
106
+
107
+ ## Option
108
+
109
+ ### noSetAlt
110
+
111
+ [Input]
112
+ 図 キャプション
113
+
114
+ ![ALT-A](image.jpg)
115
+
116
+ 図 キャプション
117
+
118
+ ![ALT-B](image.jpg)
119
+
120
+ [Output]
121
+ 図1 キャプション
122
+
123
+ ![ALT-A](image.jpg)
124
+
125
+ 図2 キャプション
126
+
127
+ ![ALT-B](image.jpg)
package/index.js CHANGED
@@ -7,6 +7,8 @@ const setMarkdownFigureNum = (markdown, option) => {
7
7
  samp: false,
8
8
  blockquote: false,
9
9
  slide: false,
10
+ noSetAlt: false,
11
+ setImgAlt: false,
10
12
  }
11
13
  opt["pre-code"] = opt.code
12
14
  opt["pre-samp"] = opt.samp
@@ -35,7 +37,8 @@ const setMarkdownFigureNum = (markdown, option) => {
35
37
  ')|' +
36
38
  '[.](' + markAfterNum + ')(?:' +
37
39
  joint + '|(?=[ ]+[^a-z])|$' +
38
- ')' +
40
+ ')|' +
41
+ '[  ]*$' +
39
42
  ')';
40
43
 
41
44
  const markAfterJa = '(?:' +
@@ -48,13 +51,14 @@ const setMarkdownFigureNum = (markdown, option) => {
48
51
  jointHalfWidth + '(?:(?=[ ]+)|$)|' +
49
52
  jointFullWidth + '|' +
50
53
  '(?=[ ]+)|$' +
51
- ')' +
54
+ ')|' +
55
+ '[  ]*$' +
52
56
  ')';
53
57
 
54
58
  const markReg = {
55
59
  //fig(ure)?, illust, photo
56
60
  "img": new RegExp('^(?:' +
57
- '(?:[fF][iI][gG](?:[uU][rR][eE])?|[iI][lL]{2}[uU][sS][tT]|[pP][hH][oO[tT][oO])'+ markAfterEn + '|' +
61
+ '(?:[fF][iI][gG](?:[uU][rR][eE])?|[iI][lL]{2}[uU][sS][tT]|[pP][hH][oO][tT][oO])'+ markAfterEn + '|' +
58
62
  '(?:図|イラスト|写真)' + markAfterJa +
59
63
  ')'),
60
64
  //movie, video
@@ -69,7 +73,7 @@ const setMarkdownFigureNum = (markdown, option) => {
69
73
  ')'),
70
74
  //code(block)?, program
71
75
  "pre-code": new RegExp('^(?:' +
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 + '|' +
76
+ '(?:[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 + '|' +
73
77
  '(?:(?:ソース)?コード|リスト|命令|プログラム|算譜|アルゴリズム|算法)' + markAfterJa +
74
78
  ')'),
75
79
  //terminal, prompt, command
@@ -92,16 +96,22 @@ const setMarkdownFigureNum = (markdown, option) => {
92
96
  const label = (hasMarkLabel, counter, isAlt) => {
93
97
 
94
98
  let label = hasMarkLabel[0]
99
+ let LabelIsEn = /[a-zA-Z]/.test(label)
100
+ let spaceBeforeCounter = ''
101
+ if (LabelIsEn) {
102
+ spaceBeforeCounter = ' '
103
+ }
95
104
 
96
105
  if (hasMarkLabel[3]) {
97
106
  label = hasMarkLabel[0].replace(new RegExp(hasMarkLabel[3] + '$'), '')
98
107
  }
99
108
  let isLabelLastJoint = label.match(new RegExp('(' + joint +')$'))
109
+
100
110
  if (isLabelLastJoint) {
101
111
  if (isAlt) {
102
- label = label.replace(new RegExp(joint +'$'), '') + ' ' + counter
112
+ label = label.replace(new RegExp(joint +'$'), '') + spaceBeforeCounter + counter
103
113
  } else {
104
- label = label.replace(new RegExp(joint +'$'), '') + ' ' + counter + isLabelLastJoint[1]
114
+ label = label.replace(new RegExp(joint +'$'), '') + spaceBeforeCounter + counter + isLabelLastJoint[1]
105
115
  }
106
116
  } else {
107
117
  label += counter
@@ -111,47 +121,54 @@ const setMarkdownFigureNum = (markdown, option) => {
111
121
 
112
122
 
113
123
  const setImageAltNumber = (lines, n, mark, hasMarkLabel, counter) => {
124
+ let hasPrevFigureImage = false
114
125
  let isFigureImage
115
- const isFigureImageReg = /^([ \t]*\!\[) *?(.*?([0-9]*)) *?(\]\(.*?\))/
116
- let hasPrevCaption
126
+ const figureImageReg = /^([ \t]*\!\[) *?(.*?([0-9]*)) *?(\]\(.*?\))/
117
127
  let i
118
128
  i = n - 1
129
+ //console.log('lines[n]: ' + lines[n])
130
+ //console.log('CheckPrevLine')
119
131
  while (i >= 0) {
120
132
  if (/^[ \t]*$/.test(lines[i])) {
121
133
  i--
122
134
  continue
123
135
  }
124
- isFigureImage = lines[i].match(new RegExp(isFigureImageReg))
125
- if (isFigureImage) {
126
- let j = i - 1
127
- while (j >= 0) {
128
- if (/^[ \t]*$/.test(lines[j])) {
129
- j--
130
- continue
131
- }
132
- if (lines[j].match(new RegExp(isFigureImageReg[2]))) {
133
- hasPrevCaption = true
134
- }
135
- if (!hasPrevCaption) {
136
- lines[i] = lines[i].replace(new RegExp(isFigureImageReg), '$1' + label(hasMarkLabel, counter[mark], true) + '$4')
137
- }
136
+ isFigureImage = lines[i].match(new RegExp(figureImageReg))
137
+ if (!isFigureImage) break
138
+
139
+ let j = i
140
+ while (j >= 0) {
141
+ if (/^[ \t]*$/.test(lines[j])) {
142
+ j--
143
+ continue
144
+ }
145
+ //console.log(isFigureImage[3], (counter.img - 1).toString())
146
+ if (isFigureImage[3] === (counter.img - 1).toString()) {
147
+ hasPrevFigureImage = false
138
148
  break
139
149
  }
150
+ lines[i] = lines[i].replace(new RegExp(figureImageReg), '$1' + label(hasMarkLabel, counter[mark], true) + '$4')
151
+ //console.log('ChangePrevLine: ' + lines[i])
152
+ hasPrevFigureImage = true
153
+ break
140
154
  }
141
155
  break
142
156
  }
143
157
 
144
- if (isFigureImage) return
158
+ if (hasPrevFigureImage) return
145
159
 
160
+ //console.log('CheckNextLine')
146
161
  i = n + 1
147
162
  while (i < lines.length) {
148
163
  if (/^[\t ]*$/.test(lines[i])) {
149
164
  i++
150
165
  continue
151
166
  }
152
- isFigureImage = lines[i].match(new RegExp(isFigureImageReg))
167
+ isFigureImage = lines[i].match(new RegExp(figureImageReg))
168
+ //console.log(isFigureImage)
153
169
  if (isFigureImage) {
154
- lines[i] = lines[i].replace(new RegExp(isFigureImageReg), '$1' + label(hasMarkLabel, counter[mark], true) + '$4')
170
+ lines[i] = lines[i].replace(new RegExp(figureImageReg), '$1' + label(hasMarkLabel, counter[mark], true) + '$4')
171
+ //console.log('ChangeNextLine: ' + lines[i])
155
172
  }
156
173
  break
157
174
  }
@@ -199,13 +216,13 @@ const setMarkdownFigureNum = (markdown, option) => {
199
216
  }
200
217
 
201
218
  for (let mark of Object.keys(markReg)) {
202
- const hasMarkLabel = lines[n].match(markReg[mark]);
219
+ const hasMarkLabel = lines[n].match(markReg[mark])
203
220
  //if (hasMarkLabel) console.log(hasMarkLabel)
204
221
  if (hasMarkLabel && opt[mark]) {
205
222
  counter[mark]++
206
223
  //console.log('lines[n]: ' + lines[n])
207
224
  lines[n] = lines[n].replace(new RegExp('^([ \t]*)' + hasMarkLabel[0]), '$1' + label(hasMarkLabel, counter[mark]))
208
- if (mark === 'img') {
225
+ if (mark === 'img' && !opt.noSetAlt) {
209
226
  setImageAltNumber(lines, n, mark, hasMarkLabel, counter)
210
227
  }
211
228
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peaceroad/markown-figure-num-setting",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "Set figure number for p7d-markdown-it-p-captions.",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -0,0 +1,36 @@
1
+ [Input]
2
+ 図 キャプション
3
+
4
+ ![ALT-A](image.jpg)
5
+
6
+ 図 キャプション
7
+
8
+ ![ALT-B](image.jpg)
9
+
10
+ [Output]
11
+ 図1 キャプション
12
+
13
+ ![ALT-A](image.jpg)
14
+
15
+ 図2 キャプション
16
+
17
+ ![ALT-B](image.jpg)
18
+
19
+
20
+ [Input]
21
+
22
+
23
+ ![ALT-A](image.jpg)
24
+
25
+
26
+
27
+ ![ALT-B](image.jpg)
28
+
29
+ [Output]
30
+ 図1
31
+
32
+ ![ALT-A](image.jpg)
33
+
34
+ 図2
35
+
36
+ ![ALT-B](image.jpg)
package/test/examples.txt CHANGED
@@ -161,3 +161,170 @@ Figure 2. キャプション
161
161
  段落。段落。段落。
162
162
 
163
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
+
178
+ 図2 キャプション
179
+
180
+ ![図2](image.jpg)
181
+
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 キャプション
199
+
200
+
201
+
202
+ [Input]
203
+
204
+
205
+ ![](image.jpg)
206
+
207
+
208
+
209
+ ![](image.jpg)
210
+ [Output]
211
+ 図1
212
+
213
+ ![図1](image.jpg)
214
+
215
+ 図2
216
+
217
+ ![図2](image.jpg)
218
+
219
+
220
+ [Input]
221
+ 図 
222
+
223
+ ![](image.jpg)
224
+
225
+ 図 
226
+
227
+ ![](image.jpg)
228
+ [Output]
229
+ 図1 
230
+
231
+ ![図1](image.jpg)
232
+
233
+ 図2 
234
+
235
+ ![図2](image.jpg)
236
+
237
+ [Input]
238
+
239
+
240
+ ![](image.jpg)
241
+
242
+
243
+
244
+ ![](image.jpg)
245
+ [Output]
246
+ 図1
247
+
248
+ ![図1](image.jpg)
249
+
250
+ 図2
251
+
252
+ ![図2](image.jpg)
253
+
254
+
255
+ [Input]
256
+ 段落。段落。段落。段落。段落。段落。段落。段落。段落。
257
+
258
+ 図1
259
+
260
+ ![図1](Src)
261
+
262
+ 段落。段落。段落。段落。段落。段落。段落。段落。段落。
263
+
264
+ 図2
265
+
266
+ ![図2](Src)
267
+
268
+ 段落。段落。段落。段落。段落。段落。段落。段落。段落。
269
+
270
+
271
+
272
+ ![図](Src)
273
+
274
+ 段落。段落。段落。段落。段落。段落。段落。段落。段落。
275
+
276
+ 図2
277
+
278
+ ![図2](Src)
279
+
280
+ 段落。段落。段落。段落。段落。段落。段落。段落。段落。
281
+
282
+
283
+
284
+ ![図](Src)
285
+
286
+ 段落。段落。段落。段落。段落。段落。段落。段落。段落。
287
+
288
+ 図2
289
+
290
+ ![図2](Src)
291
+
292
+ 段落。段落。段落。段落。段落。段落。段落。段落。段落。
293
+ [Output]
294
+ 段落。段落。段落。段落。段落。段落。段落。段落。段落。
295
+
296
+ 図1
297
+
298
+ ![図1](Src)
299
+
300
+ 段落。段落。段落。段落。段落。段落。段落。段落。段落。
301
+
302
+ 図2
303
+
304
+ ![図2](Src)
305
+
306
+ 段落。段落。段落。段落。段落。段落。段落。段落。段落。
307
+
308
+ 図3
309
+
310
+ ![図3](Src)
311
+
312
+ 段落。段落。段落。段落。段落。段落。段落。段落。段落。
313
+
314
+ 図4
315
+
316
+ ![図4](Src)
317
+
318
+ 段落。段落。段落。段落。段落。段落。段落。段落。段落。
319
+
320
+ 図5
321
+
322
+ ![図5](Src)
323
+
324
+ 段落。段落。段落。段落。段落。段落。段落。段落。段落。
325
+
326
+ 図6
327
+
328
+ ![図6](Src)
329
+
330
+ 段落。段落。段落。段落。段落。段落。段落。段落。段落。
package/test/test.js CHANGED
@@ -2,46 +2,70 @@ import assert from 'assert'
2
2
  import fs from 'fs'
3
3
  import path from 'path'
4
4
  import setMarkdownFigureNum from '../index.js'
5
+ import setMarkdownFigureNumWithNoSetAlt from '../index.js'
5
6
 
6
7
  let __dirname = path.dirname(new URL(import.meta.url).pathname)
7
8
  const isWindows = (process.platform === 'win32')
8
9
  if (isWindows) {
9
10
  __dirname = __dirname.replace(/^\/+/, '').replace(/\//g, '\\')
10
11
  }
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')
12
+
13
+ const check = (name, ex) => {
14
+ const exCont = fs.readFileSync(ex, 'utf-8').trim()
15
+ let ms = [];
16
+ let ms0 = exCont.split(/\n*\[Input\]\n/)
17
+ let n = 1;
18
+ while(n < ms0.length) {
19
+ let mhs = ms0[n].split(/\n+\[Output[^\]]*?\]\n/)
20
+ let i = 1
21
+ while (i < 2) {
22
+ if (mhs[i] === undefined) {
23
+ mhs[i] = ''
24
+ } else {
25
+ mhs[i] = mhs[i].replace(/$/,'\n')
26
+ }
27
+ i++
24
28
  }
25
- i++
29
+ ms[n] = {
30
+ inputMarkdown: mhs[0].trim(),
31
+ outputMarkdown: mhs[1].trim(),
32
+ };
33
+ n++
26
34
  }
27
- ms[n] = {
28
- inputMarkdown: mhs[0].trim(),
29
- outputMarkdown: mhs[1].trim(),
30
- };
31
- n++
32
- }
33
35
 
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)
36
+ n = 1
37
+ while(n < ms.length) {
38
+ //if (n !== 10) { n++; continue }
39
+ console.log('Test: ' + n + ' >>>')
40
+ const m = ms[n].inputMarkdown
41
+ let h
42
+ let option = {}
43
+ if (name === 'default') {
44
+ h = setMarkdownFigureNum(m)
45
+ }
46
+ if (name === 'noSetAlt') {
47
+ option.noSetAlt = true
48
+ h = setMarkdownFigureNumWithNoSetAlt(m, option)
49
+ }
50
+
51
+ try {
52
+ assert.strictEqual(h, ms[n].outputMarkdown)
53
+ } catch(e) {
54
+ console.log('incorrect: ')
55
+ //console.log(m)
56
+ //console.log('::convert ->')
57
+ console.log('H: ' + h +'\n\nC: ' + ms[n].outputMarkdown)
58
+ }
59
+ n++
45
60
  }
46
- n++
47
- }
61
+ }
62
+
63
+
64
+ const example = {
65
+ default: __dirname + path.sep + 'examples.txt',
66
+ noSetAlt: __dirname + path.sep + 'examples-no-set-alt.txt',
67
+ }
68
+ for (let ex in example) {
69
+ check(ex, example[ex])
70
+ }
71
+
@@ -1,28 +0,0 @@
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