-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
288 lines (260 loc) · 9.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
const markAfterNum = '[A-Z0-9]{1,6}(?:[.-][A-Z0-9]{1,6}){0,5}';
const joint = '[.:.。: ]';
const jointFullWidth = '[.。: ]';
const jointHalfWidth = '[.:]';
const markAfterEn = '(?:' +
' *(?:' +
jointHalfWidth + '(?:(?=[ ]+)|$)|' +
jointFullWidth + '|' +
'(?=[ ]+[^0-9a-zA-Z])' +
')|' +
' *(' + markAfterNum + ')(?:' +
jointHalfWidth + '(?:(?=[ ]+)|$)|' +
jointFullWidth + '|' +
'(?=[ ]+[^a-z])|$' +
')|' +
'[.](' + markAfterNum + ')(?:' +
joint + '|(?=[ ]+[^a-z])|$' +
')' +
')';
const markAfterJa = '(?:' +
' *(?:' +
jointHalfWidth + '(?:(?=[ ]+)|$)|' +
jointFullWidth + '|' +
'(?=[ ]+)' +
')|' +
' *(' + markAfterNum + ')(?:' +
jointHalfWidth + '(?:(?=[ ]+)|$)|' +
jointFullWidth + '|' +
'(?=[ ]+)|$' +
')' +
')';
const markReg = {
//fig(ure)?, illust, photo
"img": new RegExp('^(?:' +
'(?:[fF][iI][gG](?:[uU][rR][eE])?|[iI][lL]{2}[uU][sS][tT]|[pP][hH][oO][tT][oO])'+ markAfterEn + '|' +
'(?:図|イラスト|写真)' + markAfterJa +
')'),
//movie, video
"video": new RegExp('^(?:' +
'(?:[mM][oO][vV][iI][eE]|[vV][iI][dD][eE][oO])'+ markAfterEn + '|' +
'(?:動画|ビデオ)' + markAfterJa +
')'),
//table
"table": new RegExp('^(?:' +
'(?:[tT][aA][bB][lL][eE])'+ markAfterEn + '|' +
'(?:表)' + markAfterJa +
')'),
//code(block)?, program
"pre-code": new RegExp('^(?:' +
'(?:[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 + '|' +
'(?:(?:ソース)?コード|リスト|命令|プログラム|算譜|アルゴリズム|算法)' + markAfterJa +
')'),
//terminal, prompt, command
"pre-samp": new RegExp('^(?:' +
'(?:[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 + '|' +
'(?:端末|リスト|ターミナル|コマンド|(?:コマンド)?プロンプト)' + markAfterJa +
')'),
//quote, blockquote, source
"blockquote": new RegExp('^(?:' +
'(?:(?:[bB][lL][oO][cC][kK])?[qQ][uU][oO][tT][eE]|[sS][oO][uU][rR][cC][eE])'+ markAfterEn + '|' +
'(?:引用(?:元)?|出典)' + markAfterJa +
')'),
//slide
"slide": new RegExp('^(?:' +
'(?:[sS][lL][iI][dD][eE])'+ markAfterEn + '|' +
'(?:スライド)' + markAfterJa +
')')
};
/* Notice: the label only caption such as "Figure." and "図。" can be converted, but double-byte space caption i.e. `図 ` only cannot be converted. (This happens because the full-width spaces at the end of the paragraph have been trimmed.) */
const setCaptionParagraph = (n, state, caption, sp, opt) => {
const token = state.tokens[n];
const nextToken = state.tokens[n+1];
const isParagraphStartTag = token.type === 'paragraph_open';
if (!isParagraphStartTag) return caption
if (n > 1) {
const isList = state.tokens[n-1].type === 'list_item_open';
if (isList) return caption
}
let actualLabel = '';
let actualNum = '';
let actualLabelJoint = '';
for (let mark of Object.keys(markReg)) {
if (caption.name) {
if (/^pre/.test(mark)) {
if (caption.name !== mark) {
continue
}
}
}
const hasMarkLabel = nextToken.content.match(markReg[mark]);
if (hasMarkLabel) {
let i = 1;
while (i < 6) {
if (hasMarkLabel[i] !== undefined) {
actualNum = hasMarkLabel[i];
break;
}
i++;
}
//console.log('mark: ' + mark + ', caption.name: ' + caption.name + ', sp.isIframeTypeBlockquote: ' + sp.isIframeTypeBlockquote, ', sp.isVideoIframe: ' + sp.isVideoIframe)
if (sp.isIframeTypeBlockquote) {
if (mark !== 'blockquote' && caption.name !== 'blockquote') return caption
} else if (sp.isVideoIframe) {
if (mark !== 'video' && caption.name !== 'iframe') return caption
} else if (caption.name) {
if(caption.name !== 'iframe' && caption.name !== mark) return caption
}
token.attrJoin('class', opt.classPrefix + '-' + mark);
actualLabel = hasMarkLabel[0];
actualLabelJoint = actualLabel.match(new RegExp('(' + joint + '|)$'));
if(actualLabelJoint) {
actualLabelJoint = actualLabelJoint[1];
}
actualLabel = actualLabel.replace(/ *$/, '');
let convertJointSpaceFullWith = false;
if (opt.jointSpaceUseHalfWidth && actualLabelJoint === ' ') {
actualLabelJoint = ''
convertJointSpaceFullWith = true;
}
/*
console.log('=============================');
console.log('actualLabel: ' + actualLabel + ', actualNum: ' + actualNum + ', actualLabelJoint: ' + actualLabelJoint);
*/
addLabel(state, nextToken, mark, actualLabel, actualNum, actualLabelJoint, convertJointSpaceFullWith, opt);
return caption
}
}
return caption
}
const actualLabelContent = (actualLabel, actualLabelJoint, convertJointSpaceFullWith, opt) => {
actualLabel = actualLabel.replace(new RegExp('\\\\' + actualLabelJoint + '$'), '');
if (convertJointSpaceFullWith) {
actualLabel = actualLabel.replace(/ $/, '');
}
return actualLabel;
}
const markFilename = (state, nextToken, mark, opt) => {
let filename = nextToken.children[0].content.match(/^([ ]*?)"(\S.*?)"(?:[ ]+|$)/);
nextToken.children[0].content = nextToken.children[0].content.replace(/^[ ]*?"\S.*?"([ ]+|$)/, '$1');
const beforeFilenameToken = new state.Token('text', '', 0)
beforeFilenameToken.content = filename[1];
const filenameTokenOpen = new state.Token('strong_open', 'strong', 1);
filenameTokenOpen.attrSet('class', opt.classPrefix + '-' + mark + '-filename');
const filenameTokenContent = new state.Token('text', '', 0);
filenameTokenContent.content = filename[2];
const filenameTokenClose = new state.Token('strong_close', 'strong', -1);
nextToken.children.splice(0, 0, beforeFilenameToken, filenameTokenOpen, filenameTokenContent, filenameTokenClose);
return;
}
const addLabel = (state, nextToken, mark, actualLabel, actualNum, actualLabelJoint, convertJointSpaceFullWith, opt) => {
let labelTag = 'span';
if (opt.bLabel) labelTag = 'b';
if (opt.strongLabel) labelTag = 'strong';
const labelToken = {
first: new state.Token('text', '', 0),
open: new state.Token(labelTag + '_open', labelTag, 1),
content: new state.Token('text', '', 0),
close: new state.Token(labelTag + '_close', labelTag, -1),
};
labelToken.open.attrSet('class', opt.classPrefix + '-' + mark + '-label');
if (opt.hasNumClass && actualNum) {
labelToken.open.attrJoin('class', 'label-has-num');
}
labelToken.content.content = actualLabelContent(actualLabel, actualLabelJoint, convertJointSpaceFullWith, opt);
nextToken.children[0].content = nextToken.children[0].content.replace(actualLabel, '');
if (convertJointSpaceFullWith) {
nextToken.children[0].content = ' ' + nextToken.children[0].content;
}
if (opt.strongFilename) {
if (nextToken.children.length > 4) {
if(nextToken.children[1].type === 'strong_open'
&& nextToken.children[3].type === 'strong_close'
&& /^(?:[ ]|$)/.test(nextToken.children[4].content)) {
nextToken.children[1].attrJoin('class', opt.classPrefix + '-' + mark + '-filename');
}
}
}
if (opt.dquoteFilename) {
if (nextToken.children[0].content.match(/^[ ]*?"\S.*?"(?:[ ]+|$)/)) {
markFilename(state, nextToken, mark, opt);
}
}
if (actualNum) {
modifyLabel(state, nextToken, mark, labelToken, actualLabelJoint, opt);
} else {
if (opt.removeUnnumberedLabel) {
if (opt.removeUnnumberedLabelExceptMarks.length > 0) {
let isExceptMark = false
for (let exceptMark of opt.removeUnnumberedLabelExceptMarks) {
if (exceptMark === mark) {
isExceptMark = true
break
}
}
if (isExceptMark) {
modifyLabel(state, nextToken, mark, labelToken, actualLabelJoint, opt);
} else {
nextToken.children[0].content = nextToken.children[0].content.replace(new RegExp('^ *'), '');
}
} else {
nextToken.children[0].content = nextToken.children[0].content.replace(new RegExp('^ *'), '');
}
} else {
modifyLabel(state, nextToken, mark, labelToken, actualLabelJoint, opt);
}
}
return true;
}
const modifyLabel = (state, nextToken, mark, labelToken, actualLabelJoint, opt) => {
nextToken.children.splice(0, 0, labelToken.first, labelToken.open, labelToken.content, labelToken.close);
if (!actualLabelJoint) { return; }
nextToken.children[2].content = nextToken.children[2].content.replace(new RegExp(actualLabelJoint + ' *$'), '');
const labelJointToken = {
open: new state.Token('span_open', 'span', 1),
content: new state.Token('text', '', 0),
close: new state.Token('span_close', 'span', -1),
};
labelJointToken.open.attrSet('class', opt.classPrefix + '-' + mark + '-label-joint');
labelJointToken.content.content = actualLabelJoint;
nextToken.children.splice(3, 0, labelJointToken.open, labelJointToken.content, labelJointToken.close);
return;
}
const mditPCaption = (md, option) => {
const opt = {
classPrefix: 'caption',
dquoteFilename: false,
strongFilename: false,
hasNumClass: false,
bLabel: false,
strongLabel: false,
jointSpaceUseHalfWidth: false,
removeUnnumberedLabel: false,
removeUnnumberedLabelExceptMarks: [],
};
if (option !== undefined) {
for (let o in option) {
opt[o] = option[o]
}
}
const caption = {
mark: '',
name: '',
nameSuffix: '',
isPrev: false,
isNext: false,
}
const sp = {
isIframeTypeBlockquote: false,
isVideoIframe: false,
}
md.core.ruler.after('inline', 'p-caption', (state) => {
let n = 0;
while (n < state.tokens.length - 1) {
setCaptionParagraph(n, state, caption, sp, opt)
n++
}
})
}
export default mditPCaption
export { setCaptionParagraph, markAfterNum, joint, jointFullWidth, jointHalfWidth, markAfterEn, markAfterJa, markReg }