UNPKG

2.54 kBTypeScriptView Raw
1// ## Interfaces
2
3/**
4 * Info associated with nodes by the ecosystem.
5 *
6 * This space is guaranteed to never be specified by unist or specifications
7 * implementing unist.
8 * But you can use it in utilities and plugins to store data.
9 *
10 * This type can be augmented to register custom data.
11 * For example:
12 *
13 * ```ts
14 * declare module 'unist' {
15 * interface Data {
16 * // `someNode.data.myId` is typed as `number | undefined`
17 * myId?: number | undefined
18 * }
19 * }
20 * ```
21 */
22export interface Data {}
23
24/**
25 * One place in a source file.
26 */
27export interface Point {
28 /**
29 * Line in a source file (1-indexed integer).
30 */
31 line: number;
32
33 /**
34 * Column in a source file (1-indexed integer).
35 */
36 column: number;
37 /**
38 * Character in a source file (0-indexed integer).
39 */
40 offset?: number | undefined;
41}
42
43/**
44 * Position of a node in a source document.
45 *
46 * A position is a range between two points.
47 */
48export interface Position {
49 /**
50 * Place of the first character of the parsed source region.
51 */
52 start: Point;
53
54 /**
55 * Place of the first character after the parsed source region.
56 */
57 end: Point;
58}
59
60// ## Abstract nodes
61
62/**
63 * Abstract unist node that contains the smallest possible value.
64 *
65 * This interface is supposed to be extended.
66 *
67 * For example, in HTML, a `text` node is a leaf that contains text.
68 */
69export interface Literal extends Node {
70 /**
71 * Plain value.
72 */
73 value: unknown;
74}
75
76/**
77 * Abstract unist node.
78 *
79 * The syntactic unit in unist syntax trees are called nodes.
80 *
81 * This interface is supposed to be extended.
82 * If you can use {@link Literal} or {@link Parent}, you should.
83 * But for example in markdown, a `thematicBreak` (`***`), is neither literal
84 * nor parent, but still a node.
85 */
86export interface Node {
87 /**
88 * Node type.
89 */
90 type: string;
91
92 /**
93 * Info from the ecosystem.
94 */
95 data?: Data | undefined;
96
97 /**
98 * Position of a node in a source document.
99 *
100 * Nodes that are generated (not in the original source document) must not
101 * have a position.
102 */
103 position?: Position | undefined;
104}
105
106/**
107 * Abstract unist node that contains other nodes (*children*).
108 *
109 * This interface is supposed to be extended.
110 *
111 * For example, in XML, an element is a parent of different things, such as
112 * comments, text, and further elements.
113 */
114export interface Parent extends Node {
115 /**
116 * List of children.
117 */
118 children: Node[];
119}