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

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
@@ -1,113 +1,37 @@
1
- const setMarkdownFigureNum = (markdown, option) => {
1
+ import { markReg as markRegEx, joint as jointStr } from "p7d-markdown-it-p-captions"
2
2
 
3
- let opt = {
4
- img: true,
5
- table: false,
6
- code: false,
7
- samp: false,
8
- blockquote: false,
9
- slide: false,
10
- }
11
- opt["pre-code"] = opt.code
12
- opt["pre-samp"] = opt.samp
13
- if (option !== undefined) {
14
- for (let o in option) {
15
- opt[o] = option[o];
16
- }
17
- }
18
3
 
4
+ const markReg = markRegEx
5
+ const joint = jointStr
6
+ //console.log(markReg)
7
+ //console.log(joint)
19
8
 
20
- const markAfterNum = '[A-Z0-9]{1,6}(?:[.-][A-Z0-9]{1,6}){0,5}';
21
- const joint = '[.:.。: ]';
22
- const jointFullWidth = '[.。: ]';
23
- const jointHalfWidth = '[.:]';
24
-
25
- const markAfterEn = '(?:' +
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
- ')' +
39
- ')';
40
-
41
- const markAfterJa = '(?:' +
42
- ' *(?:' +
43
- jointHalfWidth + '(?:(?=[ ]+)|$)|' +
44
- jointFullWidth + '|' +
45
- '(?=[ ]+)' +
46
- ')|' +
47
- ' *' + '(' + markAfterNum + ')(?:' +
48
- jointHalfWidth + '(?:(?=[ ]+)|$)|' +
49
- jointFullWidth + '|' +
50
- '(?=[ ]+)|$' +
51
- ')' +
52
- ')';
53
-
54
- const markReg = {
55
- //fig(ure)?, illust, photo
56
- "img": new RegExp('^(?:' +
57
- '(?:[fF][iI][gG](?:[uU][rR][eE])?|[iI][lL]{2}[uU][sS][tT]|[pP][hH][oO[tT][oO])'+ markAfterEn + '|' +
58
- '(?:図|イラスト|写真)' + markAfterJa +
59
- ')'),
60
- //movie, video
61
- "video": new RegExp('^(?:' +
62
- '(?:[mM][oO][vV][iI][eE]|[vV][iI][dD][eE][oO])'+ markAfterEn + '|' +
63
- '(?:動画|ビデオ)' + markAfterJa +
64
- ')'),
65
- //table
66
- "table": new RegExp('^(?:' +
67
- '(?:[tT][aA][bB][lL][eE])'+ markAfterEn + '|' +
68
- '(?:表)' + markAfterJa +
69
- ')'),
70
- //code(block)?, program
71
- "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 + '|' +
73
- '(?:(?:ソース)?コード|リスト|命令|プログラム|算譜|アルゴリズム|算法)' + markAfterJa +
74
- ')'),
75
- //terminal, prompt, command
76
- "pre-samp": new RegExp('^(?:' +
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 + '|' +
78
- '(?:端末|ターミナル|コマンド|(?:コマンド)?プロンプト)' + markAfterJa +
79
- ')'),
80
- //quote, blockquote, source
81
- "blockquote": new RegExp('^(?:' +
82
- '(?:(?:[bB][lL][oO][cC][kK])?[qQ][uU][oO][tT][eE]|[sS][oO][uU][rR][cC][eE])'+ markAfterEn + '|' +
83
- '(?:引用(?:元)?|出典)' + markAfterJa +
84
- ')'),
85
- //slide
86
- "slide": new RegExp('^(?:' +
87
- '(?:[sS][lL][iI][dD][eE])'+ markAfterEn + '|' +
88
- '(?:スライド)' + markAfterJa +
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
- }
9
+ const label = (hasMarkLabel, counter, isAlt) => {
10
+ let label = hasMarkLabel[0]
11
+ //console.log('label: ' + label + ' counter: ' + counter)
12
+ let LabelIsEn = /^[a-zA-Z]/.test(label)
13
+ const spaceBeforeCounter = LabelIsEn ? ' ' : ''
14
+
15
+ //console.log(hasMarkLabel)
16
+ if (hasMarkLabel[3]) {
17
+ label = hasMarkLabel[0].replace(new RegExp(hasMarkLabel[3] + '$'), '')
18
+ } else if (hasMarkLabel[4]) {
19
+ label = hasMarkLabel[4]
20
+ }
21
+ let hasLabelLastJoint = hasMarkLabel[0].match(new RegExp('(' + joint +')$'))
22
+
23
+ if (hasLabelLastJoint) {
24
+ if (isAlt) {
25
+ label = label.replace(new RegExp(joint +'$'), '') + spaceBeforeCounter + counter
106
26
  } else {
107
- label += counter
27
+ label = label.replace(new RegExp(joint +'$'), '') + spaceBeforeCounter + counter + hasLabelLastJoint[1]
108
28
  }
109
- return label
29
+ } else {
30
+ label += counter
110
31
  }
32
+ //console.log('label: ' + label)
33
+ return label
34
+ }
111
35
 
112
36
 
113
37
  const setImageAltNumber = (lines, n, mark, hasMarkLabel, counter) => {
@@ -125,7 +49,6 @@ const setMarkdownFigureNum = (markdown, option) => {
125
49
  }
126
50
  isFigureImage = lines[i].match(new RegExp(figureImageReg))
127
51
  if (!isFigureImage) break
128
- //console.log(isFigureImage)
129
52
 
130
53
  let j = i
131
54
  while (j >= 0) {
@@ -166,6 +89,19 @@ const setMarkdownFigureNum = (markdown, option) => {
166
89
  return
167
90
  }
168
91
 
92
+ const setMarkdownFigureNum = (markdown, option) => {
93
+ let opt = {
94
+ img: true,
95
+ table: false,
96
+ 'pre-code': false,
97
+ 'pre-samp': false,
98
+ blockquote: false,
99
+ slide: false,
100
+ noSetAlt: false,
101
+ setImgAlt: false,
102
+ }
103
+ if (option) Object.assign(opt, option)
104
+
169
105
  let n = 0
170
106
  let lines = []
171
107
  let lineBreaks = []
@@ -178,11 +114,11 @@ const setMarkdownFigureNum = (markdown, option) => {
178
114
  slide: 0,
179
115
  }
180
116
 
181
-
182
117
  lines = markdown.split(/\r\n|\n/)
183
118
  lineBreaks = markdown.match(/\r\n|\n/g);
184
119
  let isBackquoteCodeBlock = false
185
120
  let isTildeCodeBlock = false
121
+ let isMathBlock = false
186
122
 
187
123
  if(lines.length === 0) return markdown
188
124
 
@@ -201,19 +137,26 @@ const setMarkdownFigureNum = (markdown, option) => {
201
137
  isTildeCodeBlock = true
202
138
  }
203
139
  }
204
- if (isBackquoteCodeBlock || isTildeCodeBlock) {
140
+ if (lines[n].match(/^ *\$\$/)) {
141
+ if (isMathBlock) {
142
+ isMathBlock = false
143
+ } else {
144
+ isMathBlock = true
145
+ }
146
+ }
147
+ if (isBackquoteCodeBlock || isTildeCodeBlock || isMathBlock) {
205
148
  n++
206
149
  continue
207
150
  }
208
151
 
152
+ //console.log('====== n: ' + n + ' lines[n]: ' + lines[n])
209
153
  for (let mark of Object.keys(markReg)) {
210
- const hasMarkLabel = lines[n].match(markReg[mark]);
211
- //if (hasMarkLabel) console.log(hasMarkLabel)
154
+ const hasMarkLabel = lines[n].match(markReg[mark])
212
155
  if (hasMarkLabel && opt[mark]) {
156
+ //console.log(hasMarkLabel)
213
157
  counter[mark]++
214
- //console.log('lines[n]: ' + lines[n])
215
158
  lines[n] = lines[n].replace(new RegExp('^([ \t]*)' + hasMarkLabel[0]), '$1' + label(hasMarkLabel, counter[mark]))
216
- if (mark === 'img') {
159
+ if (mark === 'img' && !opt.noSetAlt) {
217
160
  setImageAltNumber(lines, n, mark, hasMarkLabel, counter)
218
161
  }
219
162
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peaceroad/markown-figure-num-setting",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "Set figure number for p7d-markdown-it-p-captions.",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -16,5 +16,8 @@
16
16
  "bugs": {
17
17
  "url": "https://github.com/peaceroad/markown-figure-num-setting/issues"
18
18
  },
19
- "homepage": "https://github.com/peaceroad/markown-figure-num-setting#readme"
19
+ "homepage": "https://github.com/peaceroad/markown-figure-num-setting#readme",
20
+ "dependencies": {
21
+ "p7d-markdown-it-p-captions": "^0.17.0"
22
+ }
20
23
  }
@@ -0,0 +1,55 @@
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
+ 図3 キャプション
22
+
23
+ ![ALT-A](image.jpg)
24
+
25
+ 図6 キャプション
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)
37
+
38
+
39
+ [Input]
40
+ 図 キャプション
41
+
42
+ ![](image.jpg)
43
+
44
+ 図 キャプション
45
+
46
+ ![](image.jpg)
47
+
48
+ [Output]
49
+ 図1 キャプション
50
+
51
+ ![](image.jpg)
52
+
53
+ 図2 キャプション
54
+
55
+ ![](image.jpg)
package/test/examples.txt CHANGED
@@ -195,4 +195,154 @@ Figure 2. キャプション
195
195
 
196
196
  ![図2](image.jpg)
197
197
 
198
- 図2 キャプション
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
+
230
+
231
+ ![](image.jpg)
232
+
233
+
234
+
235
+ ![](image.jpg)
236
+
237
+
238
+ [Input]
239
+ 図 
240
+
241
+ ![](image.jpg)
242
+
243
+ 図 
244
+
245
+ ![](image.jpg)
246
+ [Output]
247
+ 図1 
248
+
249
+ ![図1](image.jpg)
250
+
251
+ 図2 
252
+
253
+ ![図2](image.jpg)
254
+
255
+ [Input]
256
+
257
+
258
+ ![](image.jpg)
259
+
260
+
261
+
262
+ ![](image.jpg)
263
+ [Output]
264
+ 図1
265
+
266
+ ![図1](image.jpg)
267
+
268
+ 図2
269
+
270
+ ![図2](image.jpg)
271
+
272
+
273
+ [Input]
274
+ 段落。段落。段落。段落。段落。段落。段落。段落。段落。
275
+
276
+ 図1 キャプション
277
+
278
+ ![図1](Src)
279
+
280
+ 段落。段落。段落。段落。段落。段落。段落。段落。段落。
281
+
282
+ 図2 キャプション
283
+
284
+ ![図2](Src)
285
+
286
+ 段落。段落。段落。段落。段落。段落。段落。段落。段落。
287
+
288
+ 図 キャプション
289
+
290
+ ![図](Src)
291
+
292
+ 段落。段落。段落。段落。段落。段落。段落。段落。段落。
293
+
294
+ 図2 キャプション
295
+
296
+ ![図2](Src)
297
+
298
+ 段落。段落。段落。段落。段落。段落。段落。段落。段落。
299
+
300
+ 図 キャプション
301
+
302
+ ![図](Src)
303
+
304
+ 段落。段落。段落。段落。段落。段落。段落。段落。段落。
305
+
306
+ 図2 キャプション
307
+
308
+ ![図2](Src)
309
+
310
+ 段落。段落。段落。段落。段落。段落。段落。段落。段落。
311
+ [Output]
312
+ 段落。段落。段落。段落。段落。段落。段落。段落。段落。
313
+
314
+ 図1 キャプション
315
+
316
+ ![図1](Src)
317
+
318
+ 段落。段落。段落。段落。段落。段落。段落。段落。段落。
319
+
320
+ 図2 キャプション
321
+
322
+ ![図2](Src)
323
+
324
+ 段落。段落。段落。段落。段落。段落。段落。段落。段落。
325
+
326
+ 図3 キャプション
327
+
328
+ ![図3](Src)
329
+
330
+ 段落。段落。段落。段落。段落。段落。段落。段落。段落。
331
+
332
+ 図4 キャプション
333
+
334
+ ![図4](Src)
335
+
336
+ 段落。段落。段落。段落。段落。段落。段落。段落。段落。
337
+
338
+ 図5 キャプション
339
+
340
+ ![図5](Src)
341
+
342
+ 段落。段落。段落。段落。段落。段落。段落。段落。段落。
343
+
344
+ 図6 キャプション
345
+
346
+ ![図6](Src)
347
+
348
+ 段落。段落。段落。段落。段落。段落。段落。段落。段落。
package/test/test.js CHANGED
@@ -2,46 +2,75 @@ 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
+ let pass = true
14
+
15
+ const check = (name, ex) => {
16
+ const exCont = fs.readFileSync(ex, 'utf-8').trim()
17
+ let ms = [];
18
+ let ms0 = exCont.split(/\n*\[Input\]\n/)
19
+ let n = 1;
20
+ while(n < ms0.length) {
21
+ let mhs = ms0[n].split(/\n+\[Output[^\]]*?\]\n/)
22
+ let i = 1
23
+ while (i < 2) {
24
+ if (mhs[i] === undefined) {
25
+ mhs[i] = ''
26
+ } else {
27
+ mhs[i] = mhs[i].replace(/$/,'\n')
28
+ }
29
+ i++
24
30
  }
25
- i++
31
+ ms[n] = {
32
+ inputMarkdown: mhs[0].trim(),
33
+ outputMarkdown: mhs[1].trim(),
34
+ };
35
+ n++
26
36
  }
27
- ms[n] = {
28
- inputMarkdown: mhs[0].trim(),
29
- outputMarkdown: mhs[1].trim(),
30
- };
31
- n++
32
- }
33
37
 
34
- n = 1;
35
- while(n < ms.length) {
36
- //if (n !== 8) { 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)
38
+ n = 1
39
+ while(n < ms.length) {
40
+ //if (n !== 11) {n++; continue }
41
+ console.log('Test: ' + n + ' >>>')
42
+ const m = ms[n].inputMarkdown
43
+ let h
44
+ let option = {}
45
+ if (name === 'default') {
46
+ h = setMarkdownFigureNum(m)
47
+ }
48
+ if (name === 'noSetAlt') {
49
+ option.noSetAlt = true
50
+ h = setMarkdownFigureNumWithNoSetAlt(m, option)
51
+ }
52
+
53
+ try {
54
+ assert.strictEqual(h, ms[n].outputMarkdown)
55
+ } catch(e) {
56
+ pass = false
57
+ console.log('incorrect: ')
58
+ console.log(m)
59
+ console.log('::convert ->')
60
+ console.log('H: ' + h +'\n\nC: ' + ms[n].outputMarkdown)
61
+ }
62
+ n++
45
63
  }
46
- n++
47
- }
64
+ }
65
+
66
+
67
+ const example = {
68
+ default: __dirname + path.sep + 'examples.txt',
69
+ noSetAlt: __dirname + path.sep + 'examples-no-set-alt.txt',
70
+ }
71
+ for (let ex in example) {
72
+ console.log('[' + ex + '] >>> ' + example[ex])
73
+ check(ex, example[ex])
74
+ }
75
+
76
+ if (pass) console.log('\nAll tests passed.')
@@ -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