-
Notifications
You must be signed in to change notification settings - Fork 38
/
SimpleAttentionPlugin.js
65 lines (56 loc) · 1.47 KB
/
SimpleAttentionPlugin.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
// @ts-check
// js instead of ts in order for mdx language server to work
import { attention } from "micromark-core-commonmark";
import { codes } from "micromark-util-symbol";
/**
* @typedef {import("micromark-util-types").Extension} MicromarkExtension
* @typedef {import("micromark-util-types").TokenizeContext} TokenizeContext
* @typedef {import("micromark-util-types").Tokenizer} Tokenizer
* @typedef {import("micromark-util-types").State} State
* @typedef {import("micromark-util-types").Code} Code
* @typedef {import("unified").Processor} Processor
* @typedef {import("remark-parse")}
*/
const micromarkExtension = {
text: {
[codes.asterisk]: {
...attention,
tokenize,
},
},
};
/**
* @this {TokenizeContext}
* @type {Tokenizer}
*/
function tokenize(effects, ok, nok) {
/** @type {Code} */
let marker;
/** @type {State} */
const start = (code) => {
marker = code;
effects.enter("attentionSequence");
return inside(code);
};
/** @type {State} */
const inside = (code) => {
if (code === marker) {
effects.consume(code);
return inside;
}
const token = effects.exit("attentionSequence");
// Always populated by defaults.
token._open = true;
token._close = true;
return ok(code);
};
return start;
}
/**
* @this {Processor}
*/
export default function simpleAttentionPlugin() {
const data = this.data();
data.micromarkExtensions ??= [];
data.micromarkExtensions.push(micromarkExtension);
}