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

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,143 @@ 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 isFigureImage
115
+ const isFigureImageReg = /^([ \t]*\!\[) *?(.*?([0-9]*)) *?(\]\(.*?\))/
116
+ let hasPrevCaption
117
+ let i
118
+ i = n - 1
119
+ while (i >= 0) {
120
+ if (/^[ \t]*$/.test(lines[i])) {
121
+ i--
122
+ continue
123
+ }
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
+ }
138
+ break
139
+ }
67
140
  }
68
- return hasMarkLabel[0]
141
+ break
69
142
  }
70
- return ''
143
+
144
+ if (isFigureImage) return
145
+
146
+ i = n + 1
147
+ while (i < lines.length) {
148
+ if (/^[\t ]*$/.test(lines[i])) {
149
+ i++
150
+ continue
151
+ }
152
+ isFigureImage = lines[i].match(new RegExp(isFigureImageReg))
153
+ if (isFigureImage) {
154
+ lines[i] = lines[i].replace(new RegExp(isFigureImageReg), '$1' + label(hasMarkLabel, counter[mark], true) + '$4')
155
+ }
156
+ break
157
+ }
158
+ return
71
159
  }
72
160
 
73
161
  let n = 0
74
162
  let lines = []
75
163
  let lineBreaks = []
76
- const hasFigureImageReg = /^( *\!\[ *)(.*?)( *\]\(.*?\))/
77
164
  const counter = {
78
165
  img: 0,
79
166
  table: 0,
80
167
  "pre-code": 0,
81
168
  "pre-samp": 0,
82
169
  blockquote: 0,
170
+ slide: 0,
83
171
  }
172
+
173
+
84
174
  lines = markdown.split(/\r\n|\n/)
85
175
  lineBreaks = markdown.match(/\r\n|\n/g);
176
+ let isBackquoteCodeBlock = false
177
+ let isTildeCodeBlock = false
178
+
179
+ if(lines.length === 0) return markdown
86
180
 
87
- if(lines.length === 0) {
88
- lines.push(markdown)
89
- }
90
181
  while (n < lines.length) {
91
- lines[n]
182
+ if (lines[n].match(/^ *```/)) {
183
+ if (isBackquoteCodeBlock) {
184
+ isBackquoteCodeBlock = false
185
+ } else {
186
+ isBackquoteCodeBlock = true
187
+ }
188
+ }
189
+ if (lines[n].match(/^ *~~~/)) {
190
+ if (isTildeCodeBlock) {
191
+ isTildeCodeBlock = false
192
+ } else {
193
+ isTildeCodeBlock = true
194
+ }
195
+ }
196
+ if (isBackquoteCodeBlock || isTildeCodeBlock) {
197
+ n++
198
+ continue
199
+ }
200
+
92
201
  for (let mark of Object.keys(markReg)) {
93
202
  const hasMarkLabel = lines[n].match(markReg[mark]);
94
- //console.log(hasMarkLabel)
95
- if (hasMarkLabel) {
203
+ //if (hasMarkLabel) console.log(hasMarkLabel)
204
+ if (hasMarkLabel && opt[mark]) {
96
205
  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
- }
206
+ //console.log('lines[n]: ' + lines[n])
207
+ lines[n] = lines[n].replace(new RegExp('^([ \t]*)' + hasMarkLabel[0]), '$1' + label(hasMarkLabel, counter[mark]))
208
+ if (mark === 'img') {
209
+ setImageAltNumber(lines, n, mark, hasMarkLabel, counter)
125
210
  }
126
211
  }
127
212
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@peaceroad/markown-figure-num-setting",
3
- "version": "0.1.0",
4
- "description": "Set figure number of p7d-markdown-it-p-captions.",
3
+ "version": "0.1.2",
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
 
@@ -60,3 +60,104 @@
60
60
 
61
61
  段落。段落。段落。
62
62
 
63
+
64
+
65
+ [Input]
66
+ 図 キャプション
67
+
68
+ ![図1](image.jpg)
69
+
70
+ 図 キャプション
71
+
72
+ ![図2](image.jpg)
73
+
74
+ [Output]
75
+ 図1 キャプション
76
+
77
+ ![図1](image.jpg)
78
+
79
+ 図2 キャプション
80
+
81
+ ![図2](image.jpg)
82
+
83
+
84
+
85
+ [Input]
86
+ ![図1](image.jpg)
87
+
88
+ 図 キャプション
89
+
90
+ ![図2](image.jpg)
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
+
115
+ 段落。段落。段落。
116
+
117
+ [Output]
118
+ 段落。段落。段落。
119
+
120
+ ```
121
+ 図 キャプション
122
+
123
+ ![図1](image.jpg)
124
+ ```
125
+
126
+ ```
127
+ Code. Caption
128
+ ```
129
+
130
+ 段落。段落。段落。
131
+
132
+
133
+ [Input]
134
+ 段落。段落。段落。
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
+
@@ -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 !== 1) { n++; continue }
37
37
  console.log('Test: ' + n + ' >>>')
38
38
  const m = ms[n].inputMarkdown
39
39
  const h = setMarkdownFigureNum(m)