-
Notifications
You must be signed in to change notification settings - Fork 0
/
remarkSubSuper.ts
263 lines (226 loc) · 9.27 KB
/
remarkSubSuper.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
// @ts-nocheck
import { splice } from 'micromark-util-chunked'
import { classifyCharacter } from 'micromark-util-classify-character'
import { resolveAll } from 'micromark-util-resolve-all'
import { constants, codes, types } from 'micromark-util-symbol'
import { Extension } from 'micromark-util-types'
import { Resolver } from 'micromark-util-types'
import { Tokenizer } from 'micromark-util-types'
import { State } from 'micromark-util-types'
import { Root } from 'mdast'
import { Plugin, Processor } from 'unified'
import { Extension as FromMarkdownExtension } from 'mdast-util-from-markdown'
import { Handle as FromMarkdownHandle } from 'mdast-util-from-markdown'
import { Options as ToMarkdownExtension } from 'mdast-util-to-markdown'
import { Handle as ToMarkdownHandle } from 'mdast-util-to-markdown'
import { ConstructName, Handle, Handlers } from 'mdast-util-to-markdown/lib/types'
function mdSubSup(): Extension {
const tokenizer = {
tokenize: tokenizeSubSup,
resolveAll: resolveAllSubSup,
}
return {
text: {
[codes.tilde]: tokenizer,
[codes.caret]: tokenizer,
},
insideSpan: {null: [tokenizer]},
attentionMarkers: {null: [codes.tilde, codes.caret]},
}
function resolveAllSubSup(...[events, context]: Parameters<Resolver>): ReturnType<Resolver> {
let index = -1
// Walk through all events.
while(++index < events.length) {
// Find a token that can close.
if(
events[index][0] === 'enter' &&
(events[index][1].type === 'subSequenceTemporary' || events[index][1].type === 'superSequenceTemporary') &&
events[index][1]._close
) {
let open = index
const enterType = events[index][1].type
const type = enterType === 'subSequenceTemporary' ? 'sub' : 'super'
// Now walk back to find an opener.
while(open--) {
// Find a token that can open the closer.
if(
events[open][0] === 'exit' &&
events[open][1].type === enterType &&
events[open][1]._open &&
// If the sizes are the same:
events[index][1].end.offset - events[index][1].start.offset ===
events[open][1].end.offset - events[open][1].start.offset
) {
events[index][1].type = type + 'Sequence'
events[open][1].type = type + 'Sequence'
const subSuper = {
type: type,
start: Object.assign({}, events[open][1].start),
end: Object.assign({}, events[index][1].end),
}
const text = {
type: type + 'Text',
start: Object.assign({}, events[open][1].end),
end: Object.assign({}, events[index][1].start),
}
// Opening.
const nextEvents = [
['enter', subSuper, context],
['enter', events[open][1], context],
['exit', events[open][1], context],
['enter', text, context],
]
// Between.
splice(
nextEvents,
nextEvents.length,
0,
resolveAll(
context.parser.constructs.insideSpan.null,
events.slice(open + 1, index),
context,
),
)
// Closing.
splice(nextEvents, nextEvents.length, 0, [
['exit', text, context],
['enter', events[index][1], context],
['exit', events[index][1], context],
['exit', subSuper, context],
])
splice(events, open - 1, index - open + 3, nextEvents)
index = open + nextEvents.length - 2
break
}
}
}
}
index = -1
while(++index < events.length) {
// todo: what does this do?
if(events[index][1].type === 'subSequenceTemporary' || events[index][1].type === 'superSequenceTemporary') {
events[index][1].type = types.data
}
}
return events
}
function tokenizeSubSup(this: ThisParameterType<Tokenizer>, ...[effects, ok, nok]: Parameters<Tokenizer>): ReturnType<Tokenizer> {
const previous = this.previous
const events = this.events
let size = 0
let lastOpened: string | undefined = undefined
return start
function start(...[code]: Parameters<State>): ReturnType<State> {
// assert(code === codes.tilde || code === codes.caret, 'expected `~|^`')
if(
previous === code &&
events[events.length - 1][1].type !== types.characterEscape
) {
return nok(code)
}
const type = code === codes.tilde ? 'sub' : 'super'
lastOpened = type + 'SequenceTemporary'
effects.enter(lastOpened)
return more(code)
}
function more(code) {
const before = classifyCharacter(previous)
if(code === codes.tilde || code === codes.caret) {
if(size > 1) return nok(code)
effects.consume(code)
size++
return more
}
if(size > 1) return nok(code)
if(lastOpened) {
const token = effects.exit(lastOpened)
const after = classifyCharacter(code)
token._open =
!after || (after === constants.attentionSideAfter && Boolean(before))
token._close =
!before || (before === constants.attentionSideAfter && Boolean(after))
}
return ok(code)
}
}
}
const subSupFromMarkdown: FromMarkdownExtension = {
canContainEols: [],
enter: {
sub: enterSub,
super: enterSup,
},
exit: {
sub: exitSubSup,
super: exitSubSup,
},
}
function enterSub(this: ThisParameterType<FromMarkdownHandle>, ...[token]: Parameters<FromMarkdownHandle>): ReturnType<FromMarkdownHandle> {
this.enter({
// @ts-ignore
type: 'sub',
data: {hName: 'sub'},
children: [],
}, token)
}
function exitSubSup(this: ThisParameterType<FromMarkdownHandle>, ...[token]: Parameters<FromMarkdownHandle>): ReturnType<FromMarkdownHandle> {
this.exit(token)
}
function enterSup(this: ThisParameterType<FromMarkdownHandle>, ...[token]: Parameters<FromMarkdownHandle>): ReturnType<FromMarkdownHandle> {
this.enter({
// @ts-ignore
type: 'super',
data: {hName: 'sup'},
children: [],
}, token)
}
export const subToMarkdown: ToMarkdownExtension & { handlers: Partial<Handlers & Record<'sub', Handle>> | null | undefined } = {
unsafe: [{character: '~', inConstruct: 'phrasing'}],
handlers: {sub: handleSub},
}
function handleSub(...[node, , state, info]: Parameters<ToMarkdownHandle>): ReturnType<ToMarkdownHandle> {
const tracker = state.createTracker(info)
const exit = state.enter('sub' as ConstructName)
let value = tracker.move('~')
value += state.containerPhrasing(node, state, {
...tracker.current(),
before: value,
after: '~',
})
value += tracker.move('~')
exit()
return value
}
export const superToMarkdown: ToMarkdownExtension & { handlers: Partial<Handlers & Record<'super', Handle>> | null | undefined } = {
unsafe: [{character: '^', inConstruct: 'phrasing'}],
handlers: {super: handleSuper},
}
function handleSuper(...[node, , context, safeOptions]: Parameters<ToMarkdownHandle>): ReturnType<ToMarkdownHandle> {
const tracker = track(safeOptions)
const exit = context.enter('sub' as ConstructName)
let value = tracker.move('^')
value += containerPhrasing(node, context, {
...tracker.current(),
before: value,
after: '^',
})
value += tracker.move('^')
exit()
return value
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function remarkSubSuper(this: Processor, ..._args: Parameters<Plugin<[] | void[], Root>>): void {
const data = this.data()
add('micromarkExtensions', mdSubSup())
add('fromMarkdownExtensions', subSupFromMarkdown)
// todo: check the `toMarkdown` plugins, seems to work but unclear what peek does
add('toMarkdownExtensions', subToMarkdown)
add('toMarkdownExtensions', superToMarkdown)
function add(field: string, value: unknown) {
const list = (
/* c8 ignore next 2 */
data[field] ? data[field] : (data[field] = [])
) as unknown[]
list.push(value)
}
}