@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.
@@ -0,0 +1,73 @@
1
+ /**
2
+ * Source position tracking for Wikidot markup.
3
+ *
4
+ * Every token produced by the parser can carry a {@link Position} that maps it
5
+ * back to its original location in the source text. This is used for error
6
+ * reporting, source-map generation, and editor integration.
7
+ *
8
+ * Both {@link Point} and {@link Position} follow the
9
+ * [unist Position](https://github.com/syntax-tree/unist#position) convention:
10
+ * lines and columns are **1-based**, offsets are **0-based**.
11
+ *
12
+ * @module
13
+ */
14
+
15
+ /**
16
+ * A single point in the source text.
17
+ *
18
+ * Represents one end (start or end) of a {@link Position} range.
19
+ * Line and column are 1-based to match text-editor conventions;
20
+ * offset is 0-based for direct use with `String.prototype.slice()`.
21
+ *
22
+ * @group Source Position
23
+ */
24
+ export interface Point {
25
+ /** Line number in the source text (1-based: the first line is line 1) */
26
+ line: number;
27
+ /** Column number within the line (1-based: the first character is column 1) */
28
+ column: number;
29
+ /** Character offset from the beginning of the source string (0-based) */
30
+ offset: number;
31
+ }
32
+
33
+ /**
34
+ * A contiguous range in the source text, defined by a start and end {@link Point}.
35
+ *
36
+ * The range is inclusive of `start` and exclusive of `end` — i.e., the
37
+ * character at `end.offset` is **not** part of the range.
38
+ *
39
+ * @group Source Position
40
+ */
41
+ export interface Position {
42
+ /** The first character of the range */
43
+ start: Point;
44
+ /** One past the last character of the range */
45
+ end: Point;
46
+ }
47
+
48
+ /**
49
+ * Create a {@link Point} value.
50
+ *
51
+ * @param line - 1-based line number
52
+ * @param column - 1-based column number
53
+ * @param offset - 0-based character offset
54
+ * @returns A frozen {@link Point} object
55
+ *
56
+ * @group Source Position
57
+ */
58
+ export function createPoint(line: number, column: number, offset: number): Point {
59
+ return { line, column, offset };
60
+ }
61
+
62
+ /**
63
+ * Create a {@link Position} range from two {@link Point}s.
64
+ *
65
+ * @param start - Beginning of the range (inclusive)
66
+ * @param end - End of the range (exclusive)
67
+ * @returns A {@link Position} spanning `start..end`
68
+ *
69
+ * @group Source Position
70
+ */
71
+ export function createPosition(start: Point, end: Point): Position {
72
+ return { start, end };
73
+ }
@@ -0,0 +1,157 @@
1
+ /**
2
+ * Context-dependent settings for the Wikidot parser and renderer.
3
+ *
4
+ * Wikidot content appears in several different contexts — full wiki pages,
5
+ * draft previews, forum posts, and direct messages — each with different
6
+ * security and capability requirements. {@link WikitextSettings} captures
7
+ * those differences so the parser/renderer can enable or disable features
8
+ * accordingly.
9
+ *
10
+ * Use {@link createSettings} to get sane defaults for a given
11
+ * {@link WikitextMode}, then override individual fields as needed.
12
+ *
13
+ * @module
14
+ */
15
+
16
+ /**
17
+ * The context in which wikitext is being parsed and rendered.
18
+ *
19
+ * Each mode implies a different set of defaults for
20
+ * {@link WikitextSettings}. The modes correspond to the places where
21
+ * user-authored wikitext can appear on a Wikidot site.
22
+ *
23
+ * | Mode | Page syntax | Local paths | True IDs | Style elements | HTML blocks |
24
+ * |--------------------|:-----------:|:-----------:|:--------:|:--------------:|:-----------:|
25
+ * | `"page"` | yes | yes | yes | yes | yes |
26
+ * | `"draft"` | yes | yes | no | no | no |
27
+ * | `"forum-post"` | no | no | no | no | no |
28
+ * | `"direct-message"` | no | no | no | no | no |
29
+ *
30
+ * @group Settings
31
+ */
32
+ export type WikitextMode = "page" | "draft" | "forum-post" | "direct-message";
33
+
34
+ /**
35
+ * Controls which parser and renderer features are active.
36
+ *
37
+ * These flags gate syntax availability and rendering behaviour based on the
38
+ * context where the wikitext appears. Construct via {@link createSettings}
39
+ * and override individual fields when non-default behaviour is needed.
40
+ *
41
+ * @group Settings
42
+ */
43
+ export interface WikitextSettings {
44
+ /** The context mode this settings object was created for */
45
+ mode: WikitextMode;
46
+
47
+ /**
48
+ * Whether page-contextual syntax is permitted.
49
+ *
50
+ * When `true`, the parser recognises `[[include]]`, `[[module]]`, and
51
+ * `[[toc]]` blocks. These constructs are meaningful only inside a full
52
+ * wiki page and are disabled in forum posts and direct messages.
53
+ */
54
+ enablePageSyntax: boolean;
55
+
56
+ /**
57
+ * Whether local file references (`file1`, `file2`, `file3`) are allowed
58
+ * in image sources.
59
+ *
60
+ * Local files belong to a specific wiki page. In contexts that lack a
61
+ * "current page" — such as forum posts and direct messages — local file
62
+ * references are meaningless and should be rejected.
63
+ */
64
+ allowLocalPaths: boolean;
65
+
66
+ /**
67
+ * Whether heading and footnote IDs use stable sequential values
68
+ * (`toc0`, `toc1`, ...) or randomised strings.
69
+ *
70
+ * Stable IDs are appropriate when a single rendered page owns the full
71
+ * document. Randomised IDs prevent collisions when multiple rendered
72
+ * fragments (e.g. a live draft preview) coexist on the same HTML page.
73
+ */
74
+ useTrueIds: boolean;
75
+
76
+ /**
77
+ * Whether `[[module CSS]]` blocks are rendered as `<style>` tags.
78
+ *
79
+ * User-authored CSS can break page layout, so it is allowed only on
80
+ * full wiki pages. In draft previews, forum posts, and direct messages
81
+ * the CSS module is silently ignored.
82
+ */
83
+ allowStyleElements: boolean;
84
+
85
+ /**
86
+ * Whether `[[html]]` blocks are recognised by the parser and rendered.
87
+ *
88
+ * HTML blocks embed raw HTML that the renderer serves inside a sandboxed
89
+ * iframe. The capability is meaningful only in contexts that can host
90
+ * the auxiliary iframe URL, so it is disabled in drafts, forum posts,
91
+ * and direct messages.
92
+ *
93
+ * When `false`, the parser still consumes the entire `[[html]]...[[/html]]`
94
+ * span (so the raw body cannot leak as text) but emits no AST node, and
95
+ * the renderer skips any pre-existing `html` element it encounters.
96
+ *
97
+ * Wikidot's legacy `Text_Wiki` keeps `Html` in its `$disable` list by
98
+ * default (`lib/Text_Wiki/Text/Wiki.php` line 145-147), so an authentic
99
+ * Wikidot-compat default would be `false` even in `"page"` mode. wp
100
+ * keeps `"page"` at `true` for now to preserve existing consumers; a
101
+ * future change may align with Wikidot.
102
+ */
103
+ allowHtmlBlocks: boolean;
104
+ }
105
+
106
+ /**
107
+ * Create a {@link WikitextSettings} with sensible defaults for the given mode.
108
+ *
109
+ * See the table on {@link WikitextMode} for which flags each mode enables.
110
+ *
111
+ * @param mode - The context in which wikitext will be parsed
112
+ * @returns A new settings object with defaults for that mode
113
+ *
114
+ * @group Settings
115
+ */
116
+ export function createSettings(mode: WikitextMode): WikitextSettings {
117
+ switch (mode) {
118
+ case "page":
119
+ return {
120
+ mode,
121
+ enablePageSyntax: true,
122
+ allowLocalPaths: true,
123
+ useTrueIds: true,
124
+ allowStyleElements: true,
125
+ allowHtmlBlocks: true,
126
+ };
127
+ case "draft":
128
+ return {
129
+ mode,
130
+ enablePageSyntax: true,
131
+ allowLocalPaths: true,
132
+ useTrueIds: false,
133
+ allowStyleElements: false,
134
+ allowHtmlBlocks: false,
135
+ };
136
+ case "forum-post":
137
+ case "direct-message":
138
+ return {
139
+ mode,
140
+ enablePageSyntax: false,
141
+ allowLocalPaths: false,
142
+ useTrueIds: false,
143
+ allowStyleElements: false,
144
+ allowHtmlBlocks: false,
145
+ };
146
+ }
147
+ }
148
+
149
+ /**
150
+ * Pre-built settings for `"page"` mode — the most common context.
151
+ *
152
+ * Equivalent to `createSettings("page")`. Provided as a convenience
153
+ * for call-sites that always operate on full wiki pages.
154
+ *
155
+ * @group Settings
156
+ */
157
+ export const DEFAULT_SETTINGS: WikitextSettings = createSettings("page");