1 | /**
|
---|
2 | * Syntactic units in unist syntax trees are called nodes.
|
---|
3 | *
|
---|
4 | * @typeParam TData Information from the ecosystem. Useful for more specific {@link Node.data}.
|
---|
5 | */
|
---|
6 | export interface Node<TData extends object = Data> {
|
---|
7 | /**
|
---|
8 | * The variant of a node.
|
---|
9 | */
|
---|
10 | type: string;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * Information from the ecosystem.
|
---|
14 | */
|
---|
15 | data?: TData | undefined;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Location of a node in a source document.
|
---|
19 | * Must not be present if a node is generated.
|
---|
20 | */
|
---|
21 | position?: Position | undefined;
|
---|
22 | }
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Information associated by the ecosystem with the node.
|
---|
26 | * Space is guaranteed to never be specified by unist or specifications
|
---|
27 | * implementing unist.
|
---|
28 | */
|
---|
29 | export interface Data {
|
---|
30 | [key: string]: unknown;
|
---|
31 | }
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Location of a node in a source file.
|
---|
35 | */
|
---|
36 | export interface Position {
|
---|
37 | /**
|
---|
38 | * Place of the first character of the parsed source region.
|
---|
39 | */
|
---|
40 | start: Point;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Place of the first character after the parsed source region.
|
---|
44 | */
|
---|
45 | end: Point;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Start column at each index (plus start line) in the source region,
|
---|
49 | * for elements that span multiple lines.
|
---|
50 | */
|
---|
51 | indent?: number[] | undefined;
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * One place in a source file.
|
---|
56 | */
|
---|
57 | export interface Point {
|
---|
58 | /**
|
---|
59 | * Line in a source file (1-indexed integer).
|
---|
60 | */
|
---|
61 | line: number;
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Column in a source file (1-indexed integer).
|
---|
65 | */
|
---|
66 | column: number;
|
---|
67 | /**
|
---|
68 | * Character in a source file (0-indexed integer).
|
---|
69 | */
|
---|
70 | offset?: number | undefined;
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Util for extracting type of {@link Node.data}
|
---|
75 | *
|
---|
76 | * @typeParam TNode Specific node type such as {@link Node} with {@link Data}, {@link Literal}, etc.
|
---|
77 | *
|
---|
78 | * @example `NodeData<Node<{ key: string }>>` -> `{ key: string }`
|
---|
79 | */
|
---|
80 | export type NodeData<TNode extends Node<object>> = TNode extends Node<infer TData> ? TData : never;
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Nodes containing other nodes.
|
---|
84 | *
|
---|
85 | * @typeParam ChildNode Node item of {@link Parent.children}
|
---|
86 | */
|
---|
87 | export interface Parent<ChildNode extends Node<object> = Node, TData extends object = NodeData<ChildNode>>
|
---|
88 | extends Node<TData>
|
---|
89 | {
|
---|
90 | /**
|
---|
91 | * List representing the children of a node.
|
---|
92 | */
|
---|
93 | children: ChildNode[];
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Nodes containing a value.
|
---|
98 | *
|
---|
99 | * @typeParam Value Specific value type of {@link Literal.value} such as `string` for `Text` node
|
---|
100 | */
|
---|
101 | export interface Literal<Value = unknown, TData extends object = Data> extends Node<TData> {
|
---|
102 | value: Value;
|
---|
103 | }
|
---|