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