@aljazmc/syntax 0.4.0 → 0.5.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 +18 -1
- package/bin/index.js +2246 -1609
- package/bin/types/html/ast.d.ts +85 -0
- package/bin/types/html/parser.d.ts +12 -0
- package/bin/types/html/tags.d.ts +2 -0
- package/bin/types/index.d.ts +7 -0
- package/package.json +3 -3
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/** Nodes have offsets into the source, `end` is exclusive. */
|
|
2
|
+
interface Base<T extends string> {
|
|
3
|
+
type: T;
|
|
4
|
+
start: number;
|
|
5
|
+
end: number;
|
|
6
|
+
}
|
|
7
|
+
export interface Root extends Base<"Root"> {
|
|
8
|
+
/** Whether the source starts with a byte order mark. */
|
|
9
|
+
bom: boolean;
|
|
10
|
+
/** A leading processing instruction, like `<?xml version="1.0"?>`. */
|
|
11
|
+
instruction: Instruction | null;
|
|
12
|
+
doctype: Doctype | null;
|
|
13
|
+
children: Child[];
|
|
14
|
+
/** Every `<!-- -->` comment, in source order. Comments are not children. */
|
|
15
|
+
comments: Comment[];
|
|
16
|
+
}
|
|
17
|
+
/** What elements contain. Whitespace and comments between them are not nodes. */
|
|
18
|
+
export type Child = Element | Text | Cdata;
|
|
19
|
+
export interface Element extends Base<"Element"> {
|
|
20
|
+
/** The name as written, like `DIV` or `my-element`. */
|
|
21
|
+
name: string;
|
|
22
|
+
/** The known tag the name matches, like `div` for `DIV`, null for custom and unknown elements. */
|
|
23
|
+
tag: string | null;
|
|
24
|
+
attributes: Attribute[];
|
|
25
|
+
/** The end of the opening tag, after its `>`. */
|
|
26
|
+
openingEnd: number;
|
|
27
|
+
/** Written with `/` before `>`. */
|
|
28
|
+
selfClosing: boolean;
|
|
29
|
+
/** The closing tag, null for self-closing and void elements. */
|
|
30
|
+
closing: ClosingTag | null;
|
|
31
|
+
/** Script, style and preformatted elements hold their content as one raw text. */
|
|
32
|
+
raw: boolean;
|
|
33
|
+
children: Child[];
|
|
34
|
+
}
|
|
35
|
+
export interface ClosingTag extends Base<"ClosingTag"> {
|
|
36
|
+
/** The name as written. */
|
|
37
|
+
name: string;
|
|
38
|
+
}
|
|
39
|
+
export interface Attribute extends Base<"Attribute"> {
|
|
40
|
+
/** The name as written. */
|
|
41
|
+
name: string;
|
|
42
|
+
/** The end of the name. */
|
|
43
|
+
nameEnd: number;
|
|
44
|
+
value: AttributeValue | null;
|
|
45
|
+
}
|
|
46
|
+
export interface AttributeValue extends Base<"AttributeValue"> {
|
|
47
|
+
/** As written, with its quotes. */
|
|
48
|
+
raw: string;
|
|
49
|
+
/** Without its quotes, character references are not decoded. */
|
|
50
|
+
text: string;
|
|
51
|
+
quote: '"' | "'" | "";
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* A run of text, without the whitespace around it, which is trivia like in
|
|
55
|
+
* Biome. A blank line ends a text, the next one starts after it.
|
|
56
|
+
*/
|
|
57
|
+
export interface Text extends Base<"Text"> {
|
|
58
|
+
/** The text as written, character references are not decoded. */
|
|
59
|
+
value: string;
|
|
60
|
+
}
|
|
61
|
+
export interface Cdata extends Base<"Cdata"> {
|
|
62
|
+
/** As written, with `<![CDATA[` and `]]>`. */
|
|
63
|
+
value: string;
|
|
64
|
+
}
|
|
65
|
+
export interface Comment extends Base<"Comment"> {
|
|
66
|
+
/** As written, with `<!--` and `-->`. */
|
|
67
|
+
value: string;
|
|
68
|
+
}
|
|
69
|
+
export interface Doctype extends Base<"Doctype"> {
|
|
70
|
+
/** The keyword as written, like `DOCTYPE`, or another word of a declaration that is no doctype. */
|
|
71
|
+
keyword: string;
|
|
72
|
+
/** The document type as written, like `html`, or null. */
|
|
73
|
+
name: string | null;
|
|
74
|
+
/** A word after the name, like `PUBLIC`. */
|
|
75
|
+
literal: string | null;
|
|
76
|
+
/** The quoted identifiers, as written. */
|
|
77
|
+
identifiers: string[];
|
|
78
|
+
}
|
|
79
|
+
export interface Instruction extends Base<"Instruction"> {
|
|
80
|
+
/** The name as written, like `xml`. */
|
|
81
|
+
name: string;
|
|
82
|
+
attributes: Attribute[];
|
|
83
|
+
}
|
|
84
|
+
export type Node = Root | Element | ClosingTag | Attribute | AttributeValue | Text | Cdata | Comment | Doctype | Instruction;
|
|
85
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Root } from "./ast.js";
|
|
2
|
+
/** The known tag a name matches, exactly like SVG's `feBlend`, or lower-cased like HTML's `DIV`. */
|
|
3
|
+
export declare function tagOf(name: string): string | null;
|
|
4
|
+
/**
|
|
5
|
+
* Parses HTML like Biome: elements are closed explicitly, only void ones may
|
|
6
|
+
* have no closing tag, and whitespace and comments are trivia between nodes.
|
|
7
|
+
*/
|
|
8
|
+
export default class Parser {
|
|
9
|
+
#private;
|
|
10
|
+
constructor(source: string);
|
|
11
|
+
parse(): Root;
|
|
12
|
+
}
|
package/bin/types/index.d.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import type { Program } from "./ast.js";
|
|
2
|
+
import type { Root as HtmlRoot } from "./html/ast.js";
|
|
2
3
|
import type { Root } from "./markdown/ast.js";
|
|
3
4
|
import { type MarkdownOptions } from "./markdown/blocks.js";
|
|
4
5
|
import type { Options } from "./parser/base.js";
|
|
5
6
|
import type { Comment } from "./scanner.js";
|
|
6
7
|
export type * from "./ast.js";
|
|
8
|
+
export type * as Html from "./html/ast.js";
|
|
9
|
+
export { tagOf } from "./html/parser.js";
|
|
7
10
|
export { default as Lines } from "./lines.js";
|
|
8
11
|
export type * as Markdown from "./markdown/ast.js";
|
|
9
12
|
export type { Comment, Token } from "./scanner.js";
|
|
@@ -24,6 +27,10 @@ export interface Language {
|
|
|
24
27
|
export declare function language(file: string): Language | undefined;
|
|
25
28
|
/** Whether a file is Markdown, by its extension. */
|
|
26
29
|
export declare function isMarkdown(file: string): boolean;
|
|
30
|
+
/** Whether a file is HTML, by its extension. */
|
|
31
|
+
export declare function isHtml(file: string): boolean;
|
|
32
|
+
/** Parses HTML like Biome, throwing a ParseError at the first error. */
|
|
33
|
+
export declare function parseHtml(source: string): HtmlRoot;
|
|
27
34
|
/** Parses CommonMark, with GitHub's tables, task lists and strikethrough unless `gfm` is false. */
|
|
28
35
|
export declare function parseMarkdown(source: string, options?: MarkdownOptions): Root;
|
|
29
36
|
export declare function parse(source: string, options: Options): Parsed;
|
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, JSON and
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "Shared, dependency-free JavaScript, TypeScript, JSX, JSON, Markdown and HTML parser for @aljazmc packages.",
|
|
5
5
|
"author": "aljazmc",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"type": "module",
|
|
@@ -32,6 +32,6 @@
|
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"@jest/globals": "^30.5.2",
|
|
35
|
-
"@types/node": "^26.6.
|
|
35
|
+
"@types/node": "^26.6.6"
|
|
36
36
|
}
|
|
37
37
|
}
|