-
Notifications
You must be signed in to change notification settings - Fork 10
/
DecorationNode.ts
88 lines (78 loc) · 1.73 KB
/
DecorationNode.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
import { convertToNodes } from "./index";
import { createNodeParser } from "./creator";
import { createPlainNode } from "./PlainNode";
import type { NodeParser } from "./index";
import type { DecorationNode, PlainNode } from "./type";
import type { NodeCreator } from "./creator";
const decorationRegExp = /\[[!"#%&'()*+,\-./{|}<>_~]+ (?:\[[^[\]]+\]|[^\]])+\]/;
type DecorationChar =
| "*"
| "!"
| '"'
| "#"
| "%"
| "&"
| "'"
| "("
| ")"
| "+"
| ","
| "-"
| "."
| "/"
| "{"
| "|"
| "}"
| "<"
| ">"
| "_"
| "~";
type AsteriskDecorationChar =
| "*-1"
| "*-2"
| "*-3"
| "*-4"
| "*-5"
| "*-6"
| "*-7"
| "*-8"
| "*-9"
| "*-10";
/**
* character type of decoration
*/
export type Decoration = Exclude<DecorationChar, "*"> | AsteriskDecorationChar;
const createDecorationNode: NodeCreator<DecorationNode | PlainNode> = (
raw,
opts,
) => {
if (opts.context === "table") {
return createPlainNode(raw, opts);
}
const separatorIndex = raw.indexOf(" ");
const rawDecos = raw.substring(1, separatorIndex);
const text = raw.substring(separatorIndex + 1, raw.length - 1);
const decoSet = new Set<string>(rawDecos);
if (decoSet.has("*")) {
const asteriskCount = rawDecos.split("*").length - 1;
decoSet.delete("*");
decoSet.add(`*-${Math.min(asteriskCount, 10)}` as AsteriskDecorationChar);
}
return [
{
type: "decoration",
raw,
rawDecos,
decos: Array.from(decoSet) as Decoration[],
nodes: convertToNodes(text, { ...opts, nested: true }),
},
];
};
export const DecorationNodeParser: NodeParser = createNodeParser(
createDecorationNode,
{
parseOnNested: false,
parseOnQuoted: true,
patterns: [decorationRegExp],
},
);