UNPKG

6.05 kBPlain TextView Raw
1// ================================================================================
2// Node Abstract Syntax Tree
3// ================================================================================
4/**
5 * AST Node types list on TxtNode.
6 * Constant value of types
7 * @see https://github.com/textlint/textlint/blob/master/docs/txtnode.md
8 */
9import type { ASTNodeTypes } from "./ASTNodeTypes";
10
11/**
12 * Key of ASTNodeTypes or any string
13 * For example, TxtNodeType is "Document".
14 */
15export type TxtNodeType = keyof typeof ASTNodeTypes;
16
17/**
18 * Any TxtNode types
19 */
20export type AnyTxtNode = TxtNode | TxtTextNode | TxtParentNode;
21
22/**
23 * Position's line start with 1.
24 * Position's column start with 0.
25 * This is for compatibility with JavaScript AST.
26 * https://gist.github.com/azu/8866b2cb9b7a933e01fe
27 */
28export type TxtNodePosition = {
29 line: number; // start with 1
30 column: number; // start with 0
31};
32
33/**
34 * Location
35 */
36export type TxtNodeLocation = {
37 start: TxtNodePosition;
38 end: TxtNodePosition;
39};
40
41/**
42 * Range starts with 0
43 */
44export type TxtNodeRange = readonly [startIndex: number, endIndex: number];
45
46/**
47 * TxtNode is abstract interface of AST Node.
48 * Probably, Real TxtNode implementation has more properties.
49 */
50export interface TxtNode {
51 type: TxtNodeType;
52 raw: string;
53 range: TxtNodeRange;
54 loc: TxtNodeLocation;
55 // `parent` is created by runtime
56 parent?: TxtParentNode;
57
58 [index: string]: any;
59}
60
61/**
62 * Text Node.
63 * Text Node has inline value.
64 * For example, `Str` Node is an TxtTextNode.
65 */
66export interface TxtTextNode extends TxtNode {
67 value: string;
68}
69
70/**
71 * Parent Node.
72 * Parent Node has children that are consist of TxtParentNode or TxtTextNode
73 */
74export interface TxtParentNode extends TxtNode {
75 children: Content[];
76}
77
78// ================================================================================
79// Node types
80// ================================================================================
81export type AlignType = "left" | "right" | "center" | null;
82export type ReferenceType = "shortcut" | "collapsed" | "full";
83
84export type Content = TopLevelContent | ListContent | TableContent | RowContent | PhrasingContent;
85
86/**
87 * All node definition types.
88 */
89export type TopLevelContent = BlockContent;
90/**
91 * All node types that may be used where markdown block content is accepted.
92 * These types are accepted inside block quotes, list items, and roots.
93 */
94export type BlockContent =
95 | TxtParagraphNode
96 | TxtHeaderNode
97 | TxtHorizontalRuleNode
98 | TxtBlockQuoteNode
99 | TxtListNode
100 | TxtTableNode
101 | TxtHtmlNode
102 | TxtCodeBlockNode;
103/**
104 * All node types that are acceptable inside lists.
105 */
106export type ListContent = TxtListItemNode;
107/**
108 * All node types that are acceptable inside tables (not table cells).
109 */
110export type TableContent = TxtTableRowNode;
111/**
112 * All node types that are acceptable inside tables rows (not table cells)
113 */
114export type RowContent = TxtTableCellNode;
115/**
116 * All node types that are acceptable in a (interactive) phrasing context (so not in links).
117 */
118export type PhrasingContent = TxtLinkNode | StaticPhrasingContent;
119/**
120 * All node types that are acceptable in a static phrasing context.
121 */
122export type StaticPhrasingContent =
123 | TxtStrNode
124 | TxtEmphasisNode
125 | TxtStrongNode
126 | TxtDeleteNode
127 | TxtHtmlNode
128 | TxtCodeNode
129 | TxtBreakNode
130 | TxtImageNode;
131
132export interface TxtDocumentNode extends TxtParentNode {
133 type: "Document";
134}
135
136export interface TxtParagraphNode extends TxtParentNode {
137 type: "Paragraph";
138 children: PhrasingContent[];
139}
140
141export interface TxtHeaderNode extends TxtParentNode {
142 type: "Header";
143 depth: 1 | 2 | 3 | 4 | 5 | 6;
144 children: PhrasingContent[];
145}
146
147export interface TxtHorizontalRuleNode extends TxtNode {
148 type: "HorizontalRule";
149}
150
151export interface TxtBlockQuoteNode extends TxtParentNode {
152 type: "BlockQuote";
153 children: BlockContent[];
154}
155
156export interface TxtListNode extends TxtParentNode {
157 type: "List";
158 ordered?: boolean | null | undefined;
159 start?: number | null | undefined;
160 spread?: boolean | null | undefined;
161 children: ListContent[];
162}
163
164export interface TxtListItemNode extends TxtParentNode {
165 type: "ListItem";
166 checked?: boolean | null | undefined;
167 spread?: boolean | null | undefined;
168 children: BlockContent[];
169}
170
171export interface TxtTableNode extends TxtParentNode {
172 type: "Table";
173 align?: AlignType[] | null | undefined;
174 children: TableContent[];
175}
176
177export interface TxtTableRowNode extends TxtParentNode {
178 type: "TableRow";
179 children: RowContent[];
180}
181
182export interface TxtTableCellNode extends TxtParentNode {
183 type: "TableCell";
184 children: PhrasingContent[];
185}
186
187export interface TxtHtmlNode extends TxtTextNode {
188 type: "Html";
189}
190
191export interface TxtCommentNode extends TxtTextNode {
192 type: "Comment";
193}
194
195export interface TxtCodeBlockNode extends TxtTextNode {
196 type: "CodeBlock";
197 lang?: string | null | undefined;
198 meta?: string | null | undefined;
199}
200
201export interface TxtStrNode extends TxtTextNode {
202 type: "Str";
203}
204
205export interface TxtEmphasisNode extends TxtParentNode {
206 type: "Emphasis";
207 children: PhrasingContent[];
208}
209
210export interface TxtStrongNode extends TxtParentNode {
211 type: "Strong";
212 children: PhrasingContent[];
213}
214
215export interface TxtDeleteNode extends TxtParentNode {
216 type: "Delete";
217 children: PhrasingContent[];
218}
219
220// Inline Code
221export interface TxtCodeNode extends TxtTextNode {
222 type: "Code";
223}
224
225export interface TxtBreakNode extends TxtNode {
226 type: "Break";
227}
228
229export interface TxtLinkNode extends TxtParentNode, TxtResource {
230 type: "Link";
231 children: StaticPhrasingContent[];
232}
233
234export interface TxtImageNode extends TxtNode, TxtResource, TxtAlternative {
235 type: "Image";
236}
237
238// Mixin
239export interface TxtResource {
240 url: string;
241 title?: string | null | undefined;
242}
243
244export interface TxtAlternative {
245 alt?: string | null | undefined;
246}