-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
syntax.js
186 lines (160 loc) · 5.29 KB
/
syntax.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
/**
* @typedef {import('micromark-util-types').Extension} Extension
* @typedef {import('micromark-util-types').Resolver} Resolver
* @typedef {import('micromark-util-types').Tokenizer} Tokenizer
* @typedef {import('micromark-util-types').State} State
* @typedef {import('micromark-util-types').Token} Token
* @typedef {import('micromark-util-types').Event} Event
*/
/**
* @typedef Options
* Configuration (optional).
* @property {boolean} [singleTilde=true]
* Whether to support strikethrough with a single tilde (`boolean`, default:
* `true`).
* Single tildes work on github.com, but are technically prohibited by the
* GFM spec.
*/
import {ok as assert} from 'uvu/assert'
import {splice} from 'micromark-util-chunked'
import {classifyCharacter} from 'micromark-util-classify-character'
import {resolveAll} from 'micromark-util-resolve-all'
import {codes} from 'micromark-util-symbol/codes.js'
import {constants} from 'micromark-util-symbol/constants.js'
import {types} from 'micromark-util-symbol/types.js'
/**
* @param {Options} [options]
* @returns {Extension}
*/
export function gfmStrikethrough(options = {}) {
let single = options.singleTilde
const tokenizer = {
tokenize: tokenizeStrikethrough,
resolveAll: resolveAllStrikethrough
}
if (single === null || single === undefined) {
single = true
}
return {
text: {[codes.tilde]: tokenizer},
insideSpan: {null: [tokenizer]},
attentionMarkers: {null: [codes.tilde]}
}
/**
* Take events and resolve strikethrough.
*
* @type {Resolver}
*/
function resolveAllStrikethrough(events, context) {
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 === 'strikethroughSequenceTemporary' &&
events[index][1]._close
) {
let open = index
// 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 === 'strikethroughSequenceTemporary' &&
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 = 'strikethroughSequence'
events[open][1].type = 'strikethroughSequence'
const strikethrough = {
type: 'strikethrough',
start: Object.assign({}, events[open][1].start),
end: Object.assign({}, events[index][1].end)
}
const text = {
type: 'strikethroughText',
start: Object.assign({}, events[open][1].end),
end: Object.assign({}, events[index][1].start)
}
// Opening.
const nextEvents = [
['enter', strikethrough, 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', strikethrough, context]
])
splice(events, open - 1, index - open + 3, nextEvents)
index = open + nextEvents.length - 2
break
}
}
}
}
index = -1
while (++index < events.length) {
if (events[index][1].type === 'strikethroughSequenceTemporary') {
events[index][1].type = types.data
}
}
return events
}
/** @type {Tokenizer} */
function tokenizeStrikethrough(effects, ok, nok) {
const previous = this.previous
const events = this.events
let size = 0
return start
/** @type {State} */
function start(code) {
assert(code === codes.tilde, 'expected `~`')
if (
previous === codes.tilde &&
events[events.length - 1][1].type !== types.characterEscape
) {
return nok(code)
}
effects.enter('strikethroughSequenceTemporary')
return more(code)
}
/** @type {State} */
function more(code) {
const before = classifyCharacter(previous)
if (code === codes.tilde) {
// If this is the third marker, exit.
if (size > 1) return nok(code)
effects.consume(code)
size++
return more
}
if (size < 2 && !single) return nok(code)
const token = effects.exit('strikethroughSequenceTemporary')
const after = classifyCharacter(code)
token._open =
!after || (after === constants.attentionSideAfter && Boolean(before))
token._close =
!before || (before === constants.attentionSideAfter && Boolean(after))
return ok(code)
}
}
}