-
Notifications
You must be signed in to change notification settings - Fork 0
/
regex.ts
273 lines (254 loc) · 14.6 KB
/
regex.ts
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
/* eslint-disable indent*/
/* eslint-disable @typescript-eslint/no-namespace */
import { esc } from 'escape-mkdn';
import { CONST } from './const';
export namespace RGX {
// <------------------------------------------------------------------------>
// markdown/wikilink special/reserved characters
// <------------------------------------------------------------------------>
export const MARKER = {
// markdown (originally from kramdown)
// atx header: https://github.com/gettalong/kramdown/blob/master/lib/kramdown/parser/kramdown/header.rbL29
ATX_HEADER : /^#{1,6}[\t ]*([^ \t].*)\n/im,
// setext header: https://github.com/gettalong/kramdown/blob/master/lib/kramdown/parser/kramdown/header.rbL17
SETEXT_HEADER : /^ {0,3}([^ \t].*)\n[-=][-=]*[ \t\r\f\v]*\n/im,
// list item: https://github.com/gettalong/kramdown/blob/master/lib/kramdown/parser/kramdown/list.rbL49
BULLET : /[^\n\r\S]{0,4}([+*-]) /i,
// markdown-style block-reference
// BLOCK : /".* \^" + BLOCK_ID$/i,
// wikilink (by order of syntactic appearance)
EMBED : new RegExp(esc(CONST.MARKER.EMBED)),
NOT_EMBED : new RegExp('(?<!' + esc(CONST.MARKER.EMBED) + ')', 'i'),
PREFIX : new RegExp('(?:' + CONST.MARKER.PREFIX + ' ?)'),
TYPE : new RegExp('(?: *' + CONST.MARKER.TYPE + ' ?)'),
OPEN : new RegExp(esc(CONST.MARKER.OPEN)),
HEADER : new RegExp(CONST.MARKER.HEADER),
// BLOCK : new RegExp(esc(CONST.MARKER.BLOCK)),
LABEL : new RegExp(esc(CONST.MARKER.LABEL)),
CLOSE : new RegExp(esc(CONST.MARKER.CLOSE)),
} as const;
// <------------------------------------------------------------------------>
// valid characters for wikilink text (wikitext)
// <------------------------------------------------------------------------>
export const VALID_CHARS = {
TYPE : /[^\n\r!:^|[\]]+/i,
FILENAME : /[^\n\r!:^|[\]]+/i,
HEADER : /[^\n\r!^|[\]]+/i,
// BLOCK_ID : /[^\n\r!:^|[\]\/]+/i,
LABEL : /.+?(?=\]{2})/i,
} as const;
// <------------------------------------------------------------------------>
// capture groups for wikilink text (wikitext)
// <------------------------------------------------------------------------>
export const CAP_GRP = {
TYPE : new RegExp('(' + VALID_CHARS.TYPE.source + ')', 'i'),
FILENAME : new RegExp('(' + VALID_CHARS.FILENAME.source + ')', 'i'),
HEADER : new RegExp('(' + VALID_CHARS.HEADER.source + ')', 'i'),
// BLOCK_ID : new RegExp('(' + VALID_CHARS.BLOCK_ID.source + ')', 'i'),
LABEL : new RegExp('(' + VALID_CHARS.LABEL.source + ')', 'i'),
} as const;
// <------------------------------------------------------------------------>
// used to build wikiattr regexes; will not return complete wikilinks
// <------------------------------------------------------------------------>
// this is a local-only variable -- see WIKILINK.BASE for exported version
const _CAP_GRP_BASE = new RegExp(
MARKER.OPEN.source
+ CAP_GRP.FILENAME.source
+ MARKER.CLOSE.source
, 'i');
const _BASE = new RegExp(
MARKER.OPEN.source
+ VALID_CHARS.FILENAME.source
+ MARKER.CLOSE.source
, 'i');
export const ATTR_UTIL = {
// list-comma is responsible for catching the single case too
LIST_COMMA : new RegExp(
_CAP_GRP_BASE.source + ' ?'
+ '(?:'
+ ', *' + _CAP_GRP_BASE.source + ' *'
+ ')*'
, 'i'),
PREFIX : new RegExp(
'^'
+ MARKER.PREFIX.source + '?'
+ CAP_GRP.TYPE.source
+ MARKER.TYPE.source
, 'im'),
} as const;
// <------------------------------------------------------------------------>
// for full parsing (w/ line-by-line utilities)
// <------------------------------------------------------------------------>
// for line-by-line attr processing
export const ATTR_LINE = {
// todo: TYPE -> SINGLE_OR_TYPE
TYPE : new RegExp(
ATTR_UTIL.PREFIX.source
+ '(?:' + ATTR_UTIL.LIST_COMMA.source + ')?'
+ '$'
, 'im'),
LIST_ITEM : new RegExp(
'^' + '(?: *)' + MARKER.BULLET.source
+ '(' + _CAP_GRP_BASE.source + ')'
, 'im'),
} as const;
// full regex -- can be used with 'g' (global) option
export const WIKI = {
// base [[wikilink]] for wikiattrs, since they only support file level links
BASE : new RegExp(
MARKER.OPEN.source
+ CAP_GRP.FILENAME.source
+ MARKER.CLOSE.source
, 'i'),
// javascript/typescript regex does not support + or * in lookarounds (from: https://stackoverflow.com/questions/9030305/regular-expression-lookbehind-doesnt-work-with-quantifiers-or)
// link / inline / text
LINK : new RegExp( // capture indices (0 is full match)
'(?:'
+ '(?:'
+ MARKER.PREFIX.source
+ CAP_GRP.TYPE.source // 1
+ MARKER.TYPE.source
+ ')?'
// skip embed
+ MARKER.NOT_EMBED.source
+ MARKER.OPEN.source
+ CAP_GRP.FILENAME.source // 2
// + '(?:'
// + '(?:' + CAP_GRP.HEADER.source + CAP_GRP.HEADER.source + ')?' //
// + '|'
// + '(?:' + CAP_GRP.BLOCK.source + CAP_GRP.BLOCK_ID.source + ')?'//
// + '|'
// + ')?'
+ '(?:' + MARKER.LABEL.source + CAP_GRP.LABEL.source + ')?' // 3
+ MARKER.CLOSE.source
+ ')'
, 'i'),
EMBED : new RegExp( // capture indices (0 is full match)
'(?:'
// skip embed
+ MARKER.EMBED.source
+ MARKER.OPEN.source
+ CAP_GRP.FILENAME.source // 1
+ MARKER.CLOSE.source
+ ')'
, 'i'),
// since javascript/typescript regex does not support the \G anchor (see: https://ruby-doc.org/core-2.5.1/Regexp.html#class-Regexp-label-Anchors),
// filenames should be extracted from list items in the full match string
// ('WIKI.BASE' is useful for this)
// attr / block / flow
ATTR : new RegExp(
ATTR_UTIL.PREFIX.source // 1
+ '(?:'
// comma-separated
+ '(?:'
+ '(?<!\n)'
+ ATTR_UTIL.LIST_COMMA.source // only captures first and last item
+ '\n'
+ ')'
+ '|'
// mkdwn-list-separated
+ '\n'
+ '(?:'
+ ATTR_LINE.LIST_ITEM.source // only captures last item
+ '\n'
+ ')+'
+ ')'
, 'im'),
} as const;
// <------------------------------------------------------------------------>
// utilities to extract out target wikitext
// (useful for entity interactions, renaming things, etc.)
// <------------------------------------------------------------------------>
export const GET = { // capture indices (0 is full match)
// link / inline / text
LINKTYPE : new RegExp(
MARKER.PREFIX.source
+ CAP_GRP.TYPE.source // 1
+ MARKER.TYPE.source
+ MARKER.OPEN.source
+ VALID_CHARS.FILENAME.source
// + '(?:'
// + '(?:' + CAP_GRP.HEADER.source + CAP_GRP.HEADER.source + ')?' //
// + '|'
// + '(?:' + CAP_GRP.BLOCK.source + CAP_GRP.BLOCK_ID.source + ')?'//
// + '|'
// + ')?'
+ '(?:' + MARKER.LABEL.source + VALID_CHARS.LABEL.source + ')?'
+ MARKER.CLOSE.source
+ '(?!\n)'
, 'i'),
// attr / block / flow
ATTRTYPE : new RegExp(
'^'
+ '(?:' + MARKER.PREFIX.source + ')?'
+ CAP_GRP.TYPE.source // 1
+ MARKER.TYPE.source
+ '(?:'
// single / comma
+ '(?:'
+ _BASE.source
+ '(' + ', *' + _BASE.source + ' *' + ')*'
+ '\n'
+ ')'
+ '|'
// mkdn-list-separated
+ '(?:' + '\n' + '(?:'
+ '^ *' + MARKER.BULLET.source + _BASE.source + '\n'
+ ')+' + ')'
+ ')'
, 'im'),
REFTYPE : new RegExp(
// attr
'(?:'
+ '^'
+ '(?:' + MARKER.PREFIX.source + ')?'
+ CAP_GRP.TYPE.source + MARKER.TYPE.source // 1 (attr)
+ '(?:'
// single / comma
+ '(?:'
+ _BASE.source + '$' + '|' + ','
+ ')'
+ '|'
// mkdn list
+ '$'
+')'
+ ')'
+ '|'
// link
+ '(?:'
+ MARKER.PREFIX.source
+ CAP_GRP.TYPE.source + MARKER.TYPE.source // 2 (link)
+ MARKER.OPEN.source
+ ')'
, 'im'),
FILENAME : new RegExp(
MARKER.OPEN.source
+ CAP_GRP.FILENAME.source // 1
+ '(?:'
+ MARKER.HEADER.source // includes BLOCK
+ '|'
+ MARKER.LABEL.source
+ '|'
+ MARKER.CLOSE.source
+ ')'
, 'i'),
// HEADER : new RegExp(MARKER.LINK_LEFT.source
// + '(?:' + VALID_CHARS.FILENAME.source + ')'
// + MARKER.HEADER.source
// + CAP_GRP.HEADER.source
// + '(?:'
// + MARKER.LINK_LABEL.source
// + '|' + MARKER.LINK_RIGHT.source
// + ')'
// , 'i'),
// BLOCK_ID : new RegExp(MARKER.LINK_LEFT.source
// + '(?:' + VALID_CHARS.FILENAME.source + ')'
// + MARKER.BLOCK.source
// + CAP_GRP.BLOCK_ID.source
// + '(?:'
// + MARKER.LINK_LABEL.source
// + '|' + MARKER.LINK_RIGHT.source
// + ')'
// , 'i'),
} as const;
}