@wdprlib/ast 2.0.0 → 2.2.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/src/css.ts ADDED
@@ -0,0 +1,83 @@
1
+ /**
2
+ * Shared CSS value definitions used across parser and renderer.
3
+ *
4
+ * @module
5
+ */
6
+
7
+ /**
8
+ * All CSS length units (plus percentage) accepted where Wikidot markup
9
+ * takes a size value.
10
+ *
11
+ * This is intentionally a superset of what legacy Wikidot accepts: wdpr
12
+ * extends size validation to modern CSS units (viewport, container-query,
13
+ * and root-relative units) instead of mirroring Wikidot's historical
14
+ * `px|em|%` allowlists. Units are canonicalized to lowercase.
15
+ *
16
+ * @group CSS
17
+ */
18
+ export const CSS_LENGTH_UNITS = [
19
+ // Container-query units
20
+ "cqmax",
21
+ "cqmin",
22
+ "cqw",
23
+ "cqh",
24
+ "cqi",
25
+ "cqb",
26
+ // Viewport units
27
+ "svmin",
28
+ "svmax",
29
+ "lvmin",
30
+ "lvmax",
31
+ "dvmin",
32
+ "dvmax",
33
+ "vmin",
34
+ "vmax",
35
+ "svw",
36
+ "svh",
37
+ "svi",
38
+ "svb",
39
+ "lvw",
40
+ "lvh",
41
+ "lvi",
42
+ "lvb",
43
+ "dvw",
44
+ "dvh",
45
+ "dvi",
46
+ "dvb",
47
+ "vw",
48
+ "vh",
49
+ "vi",
50
+ "vb",
51
+ // Root-relative font units
52
+ "rcap",
53
+ "rem",
54
+ "rex",
55
+ "rch",
56
+ "ric",
57
+ "rlh",
58
+ // Font-relative units
59
+ "cap",
60
+ "em",
61
+ "ex",
62
+ "ch",
63
+ "ic",
64
+ "lh",
65
+ // Absolute units
66
+ "cm",
67
+ "mm",
68
+ "in",
69
+ "pc",
70
+ "pt",
71
+ "px",
72
+ "q",
73
+ // Percentage
74
+ "%",
75
+ ] as const;
76
+
77
+ /**
78
+ * A CSS length unit (or percentage) accepted in size values,
79
+ * canonicalized to lowercase.
80
+ *
81
+ * @group CSS
82
+ */
83
+ export type CssLengthUnit = (typeof CSS_LENGTH_UNITS)[number];
@@ -0,0 +1,104 @@
1
+ /**
2
+ * Diagnostic types for reporting parse-time issues.
3
+ *
4
+ * When the parser encounters syntactically questionable or invalid markup
5
+ * (e.g. an unclosed `[[div]]` block), it records a {@link Diagnostic} rather
6
+ * than throwing an error. The parser is lenient: it always produces an AST,
7
+ * even when diagnostics are present.
8
+ *
9
+ * Diagnostics are returned alongside the AST via {@link ParseResult}.
10
+ *
11
+ * @since 2.0.0
12
+ * @module
13
+ */
14
+
15
+ import type { Position } from "./position";
16
+ import type { SyntaxTree } from "./element";
17
+
18
+ /**
19
+ * Severity level of a diagnostic.
20
+ *
21
+ * - `"error"` — the markup is structurally broken (e.g. inline `[[div]]`
22
+ * without a newline after `]]`).
23
+ * - `"warning"` — the markup is likely unintentional but the parser can
24
+ * recover (e.g. a missing `[[/div]]` close tag).
25
+ * - `"info"` — informational hints (e.g. deprecated syntax).
26
+ *
27
+ * @since 2.0.0
28
+ * @group Diagnostics
29
+ */
30
+ export type DiagnosticSeverity = "error" | "warning" | "info";
31
+
32
+ /**
33
+ * A single diagnostic emitted during parsing.
34
+ *
35
+ * Each diagnostic pinpoints a source location via {@link Position} and
36
+ * carries a machine-readable {@link Diagnostic.code | code} string for
37
+ * programmatic filtering (e.g. `"unclosed-block"`, `"inline-block-element"`).
38
+ *
39
+ * @example
40
+ * ```ts
41
+ * import { parse } from "@wdprlib/parser";
42
+ *
43
+ * const { ast, diagnostics } = parse("[[div]]\nHello");
44
+ * for (const d of diagnostics) {
45
+ * console.log(`[${d.severity}] ${d.message} (line ${d.position.start.line})`);
46
+ * }
47
+ * ```
48
+ *
49
+ * @since 2.0.0
50
+ * @group Diagnostics
51
+ */
52
+ export interface Diagnostic {
53
+ /** How severe the issue is. */
54
+ severity: DiagnosticSeverity;
55
+
56
+ /**
57
+ * Machine-readable identifier for the diagnostic kind.
58
+ *
59
+ * Current codes:
60
+ * - `"unclosed-block"` — a block element has no matching close tag.
61
+ * - `"inline-block-element"` — a block element (e.g. `[[div]]`) is used
62
+ * inline without the required trailing newline.
63
+ */
64
+ code: string;
65
+
66
+ /** Human-readable description of the issue. */
67
+ message: string;
68
+
69
+ /** Source range where the issue was detected. */
70
+ position: Position;
71
+
72
+ /**
73
+ * An optional related source range that provides additional context
74
+ * (e.g. the opening tag position when reporting a missing close tag).
75
+ */
76
+ relatedPosition?: Position;
77
+ }
78
+
79
+ /**
80
+ * The result of parsing a Wikidot markup string.
81
+ *
82
+ * Contains both the parsed AST and any diagnostics emitted during parsing.
83
+ * The AST is always produced, even when diagnostics are present — the parser
84
+ * is lenient and recovers from errors.
85
+ *
86
+ * @example
87
+ * ```ts
88
+ * import { parse } from "@wdprlib/parser";
89
+ *
90
+ * const result = parse("**bold** and //italic//");
91
+ * console.log(result.ast.elements); // AST nodes
92
+ * console.log(result.diagnostics); // [] (no issues)
93
+ * ```
94
+ *
95
+ * @since 2.0.0
96
+ * @group Diagnostics
97
+ */
98
+ export interface ParseResult {
99
+ /** The parsed syntax tree. */
100
+ ast: SyntaxTree;
101
+
102
+ /** Diagnostics emitted during parsing (empty when the input is clean). */
103
+ diagnostics: Diagnostic[];
104
+ }