@aljazmc/syntax 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -1
- package/bin/index.js +1515 -2
- package/bin/types/index.d.ts +8 -1
- package/bin/types/markdown/ast.d.ts +156 -0
- package/bin/types/markdown/blocks.d.ts +15 -0
- package/bin/types/markdown/inlines.d.ts +7 -0
- package/bin/types/markdown/scan.d.ts +10 -0
- package/package.json +2 -2
package/bin/types/index.d.ts
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import type { Program } from "./ast.js";
|
|
2
|
+
import type { Root } from "./markdown/ast.js";
|
|
3
|
+
import { type MarkdownOptions } from "./markdown/blocks.js";
|
|
2
4
|
import type { Options } from "./parser/base.js";
|
|
3
5
|
import type { Comment } from "./scanner.js";
|
|
4
6
|
export type * from "./ast.js";
|
|
5
7
|
export { default as Lines } from "./lines.js";
|
|
8
|
+
export type * as Markdown from "./markdown/ast.js";
|
|
6
9
|
export type { Comment, Token } from "./scanner.js";
|
|
7
10
|
export { ParseError } from "./scanner.js";
|
|
8
11
|
export { default as Walker } from "./walker.js";
|
|
9
|
-
export type { Options };
|
|
12
|
+
export type { MarkdownOptions, Options };
|
|
10
13
|
export interface Parsed {
|
|
11
14
|
program: Program;
|
|
12
15
|
comments: Comment[];
|
|
@@ -19,6 +22,10 @@ export interface Language {
|
|
|
19
22
|
}
|
|
20
23
|
/** The language of a JavaScript, TypeScript, JSX or JSON file, undefined for others. */
|
|
21
24
|
export declare function language(file: string): Language | undefined;
|
|
25
|
+
/** Whether a file is Markdown, by its extension. */
|
|
26
|
+
export declare function isMarkdown(file: string): boolean;
|
|
27
|
+
/** Parses CommonMark, with GitHub's tables, task lists and strikethrough unless `gfm` is false. */
|
|
28
|
+
export declare function parseMarkdown(source: string, options?: MarkdownOptions): Root;
|
|
22
29
|
export declare function parse(source: string, options: Options): Parsed;
|
|
23
30
|
/** Parses a single expression, like a JSON document, as an expression statement program. */
|
|
24
31
|
export declare function parseExpression(source: string): Parsed;
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/** Blocks have offsets into the source and 1-based lines, inlines offsets into their block's text. */
|
|
2
|
+
interface Base<T extends string> {
|
|
3
|
+
type: T;
|
|
4
|
+
start: number;
|
|
5
|
+
end: number;
|
|
6
|
+
}
|
|
7
|
+
interface BlockBase<T extends string> extends Base<T> {
|
|
8
|
+
/** The first and last line with content, blank lines around are not part of a block. */
|
|
9
|
+
line: number;
|
|
10
|
+
endLine: number;
|
|
11
|
+
}
|
|
12
|
+
export interface Root extends BlockBase<"Root"> {
|
|
13
|
+
/** Whether the source starts with a byte order mark. */
|
|
14
|
+
bom: boolean;
|
|
15
|
+
/** The raw front matter, with its `---` fences, when parsing it is enabled. */
|
|
16
|
+
frontmatter: Frontmatter | null;
|
|
17
|
+
children: Block[];
|
|
18
|
+
}
|
|
19
|
+
export interface Frontmatter extends BlockBase<"Frontmatter"> {
|
|
20
|
+
raw: string;
|
|
21
|
+
}
|
|
22
|
+
export interface Heading extends BlockBase<"Heading"> {
|
|
23
|
+
level: number;
|
|
24
|
+
/** `Title\n=====` rather than `# Title`. */
|
|
25
|
+
setext: boolean;
|
|
26
|
+
/** The content, without the markers and the indentation of each line. */
|
|
27
|
+
text: string;
|
|
28
|
+
/** The `===` or `---` line of setext headings, trimmed. */
|
|
29
|
+
underline: string;
|
|
30
|
+
children: Inline[];
|
|
31
|
+
}
|
|
32
|
+
export interface Paragraph extends BlockBase<"Paragraph"> {
|
|
33
|
+
/** The lines joined by `\n`, without the indentation of each line. */
|
|
34
|
+
text: string;
|
|
35
|
+
/** The indentation of each line, after the prefixes of containers. */
|
|
36
|
+
indents: string[];
|
|
37
|
+
/** What precedes each line in the source, like the indentation of a list item or `> `. */
|
|
38
|
+
prefixes: string[];
|
|
39
|
+
/** Whether each line continues the paragraph lazily, without the prefixes of its containers. */
|
|
40
|
+
lazy: boolean[];
|
|
41
|
+
children: Inline[];
|
|
42
|
+
}
|
|
43
|
+
export interface ThematicBreak extends BlockBase<"ThematicBreak"> {
|
|
44
|
+
/** The line as written, without indentation, like `* * *`. */
|
|
45
|
+
raw: string;
|
|
46
|
+
}
|
|
47
|
+
export interface CodeBlock extends BlockBase<"CodeBlock"> {
|
|
48
|
+
/** The opening fence, like "```" or "~~~~", or "" for indented code. */
|
|
49
|
+
fence: string;
|
|
50
|
+
/** The columns the opening fence is indented by. */
|
|
51
|
+
indent: number;
|
|
52
|
+
/** The info string, trimmed. */
|
|
53
|
+
info: string;
|
|
54
|
+
/** The lines of code, after the container prefixes, indented code without its four columns. */
|
|
55
|
+
lines: string[];
|
|
56
|
+
/** What precedes each line in the source, like the indentation of a list item or `> `. */
|
|
57
|
+
prefixes: string[];
|
|
58
|
+
/** Whether a fenced block has its closing fence. */
|
|
59
|
+
closed: boolean;
|
|
60
|
+
}
|
|
61
|
+
export interface HtmlBlock extends BlockBase<"HtmlBlock"> {
|
|
62
|
+
/** The CommonMark start condition, 1 to 7. */
|
|
63
|
+
kind: number;
|
|
64
|
+
lines: string[];
|
|
65
|
+
}
|
|
66
|
+
export interface Definition extends BlockBase<"Definition"> {
|
|
67
|
+
/** The indentation before the label. */
|
|
68
|
+
indent: string;
|
|
69
|
+
label: string;
|
|
70
|
+
destination: string;
|
|
71
|
+
/** The title with its quotes or parentheses, if any. */
|
|
72
|
+
title: string | null;
|
|
73
|
+
}
|
|
74
|
+
export interface BlockQuote extends BlockBase<"BlockQuote"> {
|
|
75
|
+
children: Block[];
|
|
76
|
+
}
|
|
77
|
+
export interface List extends BlockBase<"List"> {
|
|
78
|
+
ordered: boolean;
|
|
79
|
+
/** `-`, `*` or `+`, or the delimiter of ordered lists, `.` or `)`. */
|
|
80
|
+
delimiter: string;
|
|
81
|
+
/** Whether blank lines separate no items and no blocks of an item. */
|
|
82
|
+
tight: boolean;
|
|
83
|
+
children: ListItem[];
|
|
84
|
+
}
|
|
85
|
+
export interface ListItem extends BlockBase<"ListItem"> {
|
|
86
|
+
/** The marker as written, like `-` or `01.`. */
|
|
87
|
+
marker: string;
|
|
88
|
+
/** The number of ordered items. */
|
|
89
|
+
number: number | null;
|
|
90
|
+
/** Columns before the marker. */
|
|
91
|
+
indent: number;
|
|
92
|
+
/** Spaces after the marker as written, before the content, at least 1 unless the item is empty. */
|
|
93
|
+
spaces: number;
|
|
94
|
+
/** Task list items, `[ ]` or `[x]`, have their state, the box stays in the first paragraph. */
|
|
95
|
+
checked: boolean | null;
|
|
96
|
+
children: Block[];
|
|
97
|
+
}
|
|
98
|
+
export type Alignment = "left" | "center" | "right" | null;
|
|
99
|
+
export interface Table extends BlockBase<"Table"> {
|
|
100
|
+
alignments: Alignment[];
|
|
101
|
+
/** The header first, then the body rows, without the delimiter row. */
|
|
102
|
+
rows: TableRow[];
|
|
103
|
+
}
|
|
104
|
+
export interface TableRow extends BlockBase<"TableRow"> {
|
|
105
|
+
cells: TableCell[];
|
|
106
|
+
}
|
|
107
|
+
export interface TableCell extends Base<"TableCell"> {
|
|
108
|
+
text: string;
|
|
109
|
+
children: Inline[];
|
|
110
|
+
}
|
|
111
|
+
export type Block = BlockQuote | CodeBlock | Definition | Heading | HtmlBlock | List | Paragraph | Table | ThematicBreak;
|
|
112
|
+
export interface Text extends Base<"Text"> {
|
|
113
|
+
/** As written, with escapes and entities. */
|
|
114
|
+
raw: string;
|
|
115
|
+
}
|
|
116
|
+
export interface Break extends Base<"Break"> {
|
|
117
|
+
/** A hard break, two spaces or a backslash before the line ending. */
|
|
118
|
+
hard: boolean;
|
|
119
|
+
raw: string;
|
|
120
|
+
}
|
|
121
|
+
export interface Code extends Base<"Code"> {
|
|
122
|
+
raw: string;
|
|
123
|
+
}
|
|
124
|
+
/** `*a*`, `**a**` and `~~a~~` with their delimiters, like `_` or `**`. */
|
|
125
|
+
export interface Emphasis extends Base<"Emphasis"> {
|
|
126
|
+
delimiter: string;
|
|
127
|
+
children: Inline[];
|
|
128
|
+
}
|
|
129
|
+
export interface Strong extends Base<"Strong"> {
|
|
130
|
+
delimiter: string;
|
|
131
|
+
children: Inline[];
|
|
132
|
+
}
|
|
133
|
+
export interface Strikethrough extends Base<"Strikethrough"> {
|
|
134
|
+
delimiter: string;
|
|
135
|
+
children: Inline[];
|
|
136
|
+
}
|
|
137
|
+
export interface Link extends Base<"Link" | "Image"> {
|
|
138
|
+
children: Inline[];
|
|
139
|
+
/** From `[text](` to the end. */
|
|
140
|
+
tail: string;
|
|
141
|
+
/** Inline links have a destination, others a label, like `[a][b]`, `[a][]` and `[a]`. */
|
|
142
|
+
reference: "full" | "collapsed" | "shortcut" | null;
|
|
143
|
+
/** The label of references, with its brackets. */
|
|
144
|
+
label: string | null;
|
|
145
|
+
destination: string;
|
|
146
|
+
title: string | null;
|
|
147
|
+
}
|
|
148
|
+
export interface Autolink extends Base<"Autolink"> {
|
|
149
|
+
raw: string;
|
|
150
|
+
}
|
|
151
|
+
export interface Html extends Base<"Html"> {
|
|
152
|
+
raw: string;
|
|
153
|
+
}
|
|
154
|
+
export type Inline = Autolink | Break | Code | Emphasis | Html | Link | Strikethrough | Strong | Text;
|
|
155
|
+
export type Node = Root | Frontmatter | Block | ListItem | TableRow | TableCell | Inline;
|
|
156
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Root } from "./ast.js";
|
|
2
|
+
export interface MarkdownOptions {
|
|
3
|
+
/** GitHub Flavored Markdown: tables, task list items and strikethrough. */
|
|
4
|
+
gfm?: boolean;
|
|
5
|
+
/** A `---` fenced block of YAML at the very start of the file. */
|
|
6
|
+
frontmatter?: boolean;
|
|
7
|
+
}
|
|
8
|
+
/** Parses Markdown blocks line by line, like CommonMark's reference parser. */
|
|
9
|
+
export default class Blocks {
|
|
10
|
+
#private;
|
|
11
|
+
constructor(source: string, options?: MarkdownOptions);
|
|
12
|
+
parse(frontmatter?: boolean): Root;
|
|
13
|
+
/** Splits a table row into its cells, on pipes that are not escaped. */
|
|
14
|
+
static cells(row: string): string[];
|
|
15
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare const OPEN_TAG = "<[A-Za-z][A-Za-z0-9-]*(?:\\s+[a-zA-Z_:][a-zA-Z0-9:._-]*(?:\\s*=\\s*(?:[^\"'=<>`\\x00-\\x20]+|'[^']*'|\"[^\"]*\"))?)*\\s*/?>";
|
|
2
|
+
export declare const CLOSE_TAG = "</[A-Za-z][A-Za-z0-9-]*\\s*[>]";
|
|
3
|
+
export default class Scan {
|
|
4
|
+
/** Normalizes a link label for matching, like `[Foo Bar]` and `[foo bar]`. */
|
|
5
|
+
static label(label: string): string;
|
|
6
|
+
/** A link destination, `<...>` or without spaces and with balanced parentheses. */
|
|
7
|
+
static destination(text: string, from: number): [number, string] | undefined;
|
|
8
|
+
/** The position after a link title in quotes or parentheses. */
|
|
9
|
+
static title(text: string, from: number): number | undefined;
|
|
10
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aljazmc/syntax",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Shared, dependency-free JavaScript, TypeScript, JSX and
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "Shared, dependency-free JavaScript, TypeScript, JSX, JSON and Markdown parser for @aljazmc packages.",
|
|
5
5
|
"author": "aljazmc",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"type": "module",
|