@wdprlib/render 1.2.0 → 1.2.1

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/dist/index.cjs CHANGED
@@ -324,11 +324,17 @@ function isValidCssColor(color) {
324
324
  if (/^#[0-9a-f]{3}([0-9a-f])?$/.test(trimmed) || /^#[0-9a-f]{6}([0-9a-f]{2})?$/.test(trimmed)) {
325
325
  return true;
326
326
  }
327
- if (/^rgba?\(\s*\d{1,3}\s*,\s*\d{1,3}\s*,\s*\d{1,3}\s*(,\s*(0|1|0?\.\d+))?\s*\)$/.test(trimmed)) {
328
- return true;
329
- }
330
- if (/^hsla?\(\s*\d{1,3}\s*,\s*\d{1,3}%\s*,\s*\d{1,3}%\s*(,\s*(0|1|0?\.\d+))?\s*\)$/.test(trimmed)) {
331
- return true;
327
+ const fnMatch = trimmed.match(/^(rgba?|hsla?)\(([^)]*)\)$/);
328
+ if (fnMatch) {
329
+ const fn = fnMatch[1];
330
+ const args = fnMatch[2].split(",").map((s) => s.trim()).join(",");
331
+ if (fn.startsWith("rgb")) {
332
+ if (/^\d{1,3},\d{1,3},\d{1,3}(,(0|1|0?\.\d+))?$/.test(args))
333
+ return true;
334
+ } else {
335
+ if (/^\d{1,3},\d{1,3}%,\d{1,3}%(,(0|1|0?\.\d+))?$/.test(args))
336
+ return true;
337
+ }
332
338
  }
333
339
  return false;
334
340
  }
@@ -4117,7 +4123,7 @@ function renderJoin(ctx, data) {
4117
4123
  const buttonText = data["button-text"] ?? "Join";
4118
4124
  const attrs = data.attributes ?? {};
4119
4125
  const className = attrs.class ?? "join-box";
4120
- ctx.push(`<div class="${escapeHtml(className)}">`);
4126
+ ctx.push(`<div class="${escapeAttr(className)}">`);
4121
4127
  ctx.push(`<a href="javascript:;">${escapeHtml(buttonText)}</a>`);
4122
4128
  ctx.push("</div>");
4123
4129
  }
package/dist/index.d.cts CHANGED
@@ -19,71 +19,113 @@ interface EmbedAllowlistEntry {
19
19
  */
20
20
  declare const DEFAULT_EMBED_ALLOWLIST: EmbedAllowlistEntry[] | null;
21
21
  /**
22
- * Page context for resolving links, images, etc.
22
+ * Contextual information about the wiki page being rendered.
23
+ *
24
+ * The renderer uses this to resolve relative links, local file paths,
25
+ * page-existence checks (adding a `"newpage"` CSS class to links
26
+ * targeting non-existent pages), and `[[iftags]]` evaluation.
27
+ *
28
+ * @group Render Options
23
29
  */
24
30
  interface PageContext {
25
- /** Current page's full name (e.g., "secret:test2") */
31
+ /** Full page name including category prefix (e.g. `"secret:test2"`) */
26
32
  pageName: string;
27
- /** Site slug (e.g., "scp-wiki") */
33
+ /** Site slug used to build inter-site URLs (e.g. `"scp-wiki"`) */
28
34
  site?: string;
29
- /** Site domain (e.g., "scp-wiki.wikidot.com") */
35
+ /** Site domain used for absolute URL generation (e.g. `"scp-wiki.wikidot.com"`) */
30
36
  domain?: string;
31
- /** Check if a page exists (for "newpage" class on links) */
37
+ /**
38
+ * Returns whether a page exists on the site.
39
+ * When a target page does not exist, the renderer adds `class="newpage"`
40
+ * to the link element — the standard Wikidot convention for red-links.
41
+ */
32
42
  pageExists?: (page: string) => boolean;
33
- /** Page tags for [[iftags]] conditional rendering */
43
+ /** Page tags used for client-side `[[iftags]]` evaluation during rendering */
34
44
  tags?: string[];
35
45
  }
36
46
  /**
37
- * Resolved user information for rendering
47
+ * User profile data returned by a user-resolver callback.
48
+ *
49
+ * Passed to the renderer to produce the Wikidot user-info markup
50
+ * (`[[user username]]`). When a field is omitted the corresponding
51
+ * UI element is simply not emitted.
52
+ *
53
+ * @group Render Options
38
54
  */
39
55
  interface ResolvedUser {
40
- /** User's display name (defaults to username if not provided) */
56
+ /** Display name shown in the rendered output (falls back to the raw username) */
41
57
  name?: string;
42
- /** User profile URL. If not provided, link becomes non-navigable */
58
+ /** Profile URL. When omitted the username is rendered as a non-navigable `<span>` */
43
59
  url?: string;
44
- /** Avatar image URL. If not provided, no avatar is rendered */
60
+ /** Avatar image URL. When omitted no avatar `<img>` is rendered */
45
61
  avatarUrl?: string;
46
- /** Karma image URL for avatar background (Wikidot-specific feature) */
62
+ /** Karma-badge image URL shown behind the avatar (Wikidot-specific feature) */
47
63
  karmaUrl?: string;
48
64
  }
49
65
  /**
50
- * Resolver functions for dynamic content
66
+ * Async/sync resolver callbacks for content that depends on external data.
67
+ *
68
+ * Unlike the parser's `DataProvider` (which fetches bulk data for
69
+ * module expansion), these resolvers are called per-element during the
70
+ * rendering pass.
71
+ *
72
+ * @group Render Options
51
73
  */
52
74
  interface RenderResolvers {
53
75
  /**
54
- * Resolve user information from username.
55
- * Returns user data for rendering, or null if user not found.
56
- * If not provided, user elements are rendered as plain text.
76
+ * Look up a user profile by username.
77
+ *
78
+ * @param username - The raw username from `[[user username]]`
79
+ * @returns Profile data for rendering, or `null` if the user is unknown.
80
+ * When `null` or when the resolver is omitted, the username is
81
+ * rendered as plain text.
57
82
  */
58
83
  user?: (username: string) => ResolvedUser | null;
59
84
  /**
60
- * Returns URL for htmlBlock iframe src.
61
- * Called with the index of the htmlBlock (0-based, matching tree["html-blocks"] order).
62
- * If not provided or returns empty string, uses default pattern: /{pageName}/html/{hash}-{nonce}
85
+ * Build an iframe `src` URL for an `[[html]]` block.
63
86
  *
64
- * SECURITY NOTE: The returned URL is used directly in iframe src attribute.
65
- * The application is responsible for validating the URL scheme (e.g., rejecting javascript:, data:).
87
+ * @param index - Zero-based index matching `SyntaxTree["html-blocks"]`
88
+ * @returns The URL string. When empty or when the resolver is omitted,
89
+ * the default pattern `/{pageName}/html/{hash}-{nonce}` is used.
90
+ *
91
+ * @security The returned URL is injected directly into the iframe `src`
92
+ * attribute. The caller must validate the scheme to reject `javascript:`,
93
+ * `data:`, and other dangerous protocols.
66
94
  */
67
95
  htmlBlockUrl?: (index: number) => string;
68
96
  }
69
97
  /**
70
- * Options for HTML rendering
98
+ * Full configuration for `renderToHtml()`.
99
+ *
100
+ * Every field is optional; defaults produce safe, standalone HTML output
101
+ * suitable for a full wiki page.
102
+ *
103
+ * @group Render Options
71
104
  */
72
105
  interface RenderOptions {
73
106
  /**
74
- * Base URL used to resolve protocol-relative URLs (e.g., "//example.com/path").
75
- * The protocol of this URL is inherited by protocol-relative references.
76
- * Example: "https://scp-wiki.wikidot.com" or "http://scp-jp.wikidot.com"
77
- * If not provided, protocol-relative URLs default to HTTPS.
107
+ * Base URL for resolving protocol-relative URLs (e.g. `"//example.com/path"`).
108
+ *
109
+ * The scheme of this URL (`http:` or `https:`) is prepended to
110
+ * protocol-relative references. When omitted, HTTPS is assumed.
111
+ *
112
+ * @example "https://scp-wiki.wikidot.com"
78
113
  */
79
114
  baseUrl?: string;
80
- /** Wikitext settings controlling rendering behavior */
115
+ /**
116
+ * Context-dependent feature flags.
117
+ * Defaults to page-mode settings when omitted.
118
+ */
81
119
  settings?: WikitextSettings;
82
- /** Page context for resolving file paths, links, etc. */
120
+ /** Page context for resolving relative links, local file paths, etc. */
83
121
  page?: PageContext;
84
- /** Pre-collected footnote elements from SyntaxTree.footnotes */
122
+ /**
123
+ * Pre-collected footnote element arrays from `SyntaxTree.footnotes`.
124
+ * Passed through so the renderer can emit footnote bodies in the
125
+ * `[[footnoteblock]]` section.
126
+ */
85
127
  footnotes?: Element[][];
86
- /** Resolver functions for dynamic content */
128
+ /** Callbacks for resolving users, HTML-block URLs, etc. */
87
129
  resolvers?: RenderResolvers;
88
130
  /**
89
131
  * Sandbox attribute value for htmlBlock iframes.
@@ -106,7 +148,18 @@ interface RenderOptions {
106
148
  embedAllowlist?: EmbedAllowlistEntry[] | null;
107
149
  }
108
150
  /**
109
- * Render a SyntaxTree to HTML string
151
+ * Render a {@link SyntaxTree} to an HTML string.
152
+ *
153
+ * This is the main entry point of `@wdprlib/render`. It walks the AST
154
+ * produced by `@wdprlib/parser`, serialises each element to HTML, and
155
+ * appends any collected `[[module CSS]]` styles at the end (when
156
+ * `WikitextSettings.allowStyleElements` is `true`).
157
+ *
158
+ * @param tree - Parsed AST (from `parse()` or `resolveModules()`)
159
+ * @param options - Rendering configuration
160
+ * @returns Complete HTML string
161
+ *
162
+ * @group Render
110
163
  */
111
164
  declare function renderToHtml(tree: SyntaxTree2, options?: RenderOptions): string;
112
165
  import { WikitextMode, WikitextSettings as WikitextSettings3 } from "@wdprlib/ast";
package/dist/index.d.ts CHANGED
@@ -19,71 +19,113 @@ interface EmbedAllowlistEntry {
19
19
  */
20
20
  declare const DEFAULT_EMBED_ALLOWLIST: EmbedAllowlistEntry[] | null;
21
21
  /**
22
- * Page context for resolving links, images, etc.
22
+ * Contextual information about the wiki page being rendered.
23
+ *
24
+ * The renderer uses this to resolve relative links, local file paths,
25
+ * page-existence checks (adding a `"newpage"` CSS class to links
26
+ * targeting non-existent pages), and `[[iftags]]` evaluation.
27
+ *
28
+ * @group Render Options
23
29
  */
24
30
  interface PageContext {
25
- /** Current page's full name (e.g., "secret:test2") */
31
+ /** Full page name including category prefix (e.g. `"secret:test2"`) */
26
32
  pageName: string;
27
- /** Site slug (e.g., "scp-wiki") */
33
+ /** Site slug used to build inter-site URLs (e.g. `"scp-wiki"`) */
28
34
  site?: string;
29
- /** Site domain (e.g., "scp-wiki.wikidot.com") */
35
+ /** Site domain used for absolute URL generation (e.g. `"scp-wiki.wikidot.com"`) */
30
36
  domain?: string;
31
- /** Check if a page exists (for "newpage" class on links) */
37
+ /**
38
+ * Returns whether a page exists on the site.
39
+ * When a target page does not exist, the renderer adds `class="newpage"`
40
+ * to the link element — the standard Wikidot convention for red-links.
41
+ */
32
42
  pageExists?: (page: string) => boolean;
33
- /** Page tags for [[iftags]] conditional rendering */
43
+ /** Page tags used for client-side `[[iftags]]` evaluation during rendering */
34
44
  tags?: string[];
35
45
  }
36
46
  /**
37
- * Resolved user information for rendering
47
+ * User profile data returned by a user-resolver callback.
48
+ *
49
+ * Passed to the renderer to produce the Wikidot user-info markup
50
+ * (`[[user username]]`). When a field is omitted the corresponding
51
+ * UI element is simply not emitted.
52
+ *
53
+ * @group Render Options
38
54
  */
39
55
  interface ResolvedUser {
40
- /** User's display name (defaults to username if not provided) */
56
+ /** Display name shown in the rendered output (falls back to the raw username) */
41
57
  name?: string;
42
- /** User profile URL. If not provided, link becomes non-navigable */
58
+ /** Profile URL. When omitted the username is rendered as a non-navigable `<span>` */
43
59
  url?: string;
44
- /** Avatar image URL. If not provided, no avatar is rendered */
60
+ /** Avatar image URL. When omitted no avatar `<img>` is rendered */
45
61
  avatarUrl?: string;
46
- /** Karma image URL for avatar background (Wikidot-specific feature) */
62
+ /** Karma-badge image URL shown behind the avatar (Wikidot-specific feature) */
47
63
  karmaUrl?: string;
48
64
  }
49
65
  /**
50
- * Resolver functions for dynamic content
66
+ * Async/sync resolver callbacks for content that depends on external data.
67
+ *
68
+ * Unlike the parser's `DataProvider` (which fetches bulk data for
69
+ * module expansion), these resolvers are called per-element during the
70
+ * rendering pass.
71
+ *
72
+ * @group Render Options
51
73
  */
52
74
  interface RenderResolvers {
53
75
  /**
54
- * Resolve user information from username.
55
- * Returns user data for rendering, or null if user not found.
56
- * If not provided, user elements are rendered as plain text.
76
+ * Look up a user profile by username.
77
+ *
78
+ * @param username - The raw username from `[[user username]]`
79
+ * @returns Profile data for rendering, or `null` if the user is unknown.
80
+ * When `null` or when the resolver is omitted, the username is
81
+ * rendered as plain text.
57
82
  */
58
83
  user?: (username: string) => ResolvedUser | null;
59
84
  /**
60
- * Returns URL for htmlBlock iframe src.
61
- * Called with the index of the htmlBlock (0-based, matching tree["html-blocks"] order).
62
- * If not provided or returns empty string, uses default pattern: /{pageName}/html/{hash}-{nonce}
85
+ * Build an iframe `src` URL for an `[[html]]` block.
63
86
  *
64
- * SECURITY NOTE: The returned URL is used directly in iframe src attribute.
65
- * The application is responsible for validating the URL scheme (e.g., rejecting javascript:, data:).
87
+ * @param index - Zero-based index matching `SyntaxTree["html-blocks"]`
88
+ * @returns The URL string. When empty or when the resolver is omitted,
89
+ * the default pattern `/{pageName}/html/{hash}-{nonce}` is used.
90
+ *
91
+ * @security The returned URL is injected directly into the iframe `src`
92
+ * attribute. The caller must validate the scheme to reject `javascript:`,
93
+ * `data:`, and other dangerous protocols.
66
94
  */
67
95
  htmlBlockUrl?: (index: number) => string;
68
96
  }
69
97
  /**
70
- * Options for HTML rendering
98
+ * Full configuration for `renderToHtml()`.
99
+ *
100
+ * Every field is optional; defaults produce safe, standalone HTML output
101
+ * suitable for a full wiki page.
102
+ *
103
+ * @group Render Options
71
104
  */
72
105
  interface RenderOptions {
73
106
  /**
74
- * Base URL used to resolve protocol-relative URLs (e.g., "//example.com/path").
75
- * The protocol of this URL is inherited by protocol-relative references.
76
- * Example: "https://scp-wiki.wikidot.com" or "http://scp-jp.wikidot.com"
77
- * If not provided, protocol-relative URLs default to HTTPS.
107
+ * Base URL for resolving protocol-relative URLs (e.g. `"//example.com/path"`).
108
+ *
109
+ * The scheme of this URL (`http:` or `https:`) is prepended to
110
+ * protocol-relative references. When omitted, HTTPS is assumed.
111
+ *
112
+ * @example "https://scp-wiki.wikidot.com"
78
113
  */
79
114
  baseUrl?: string;
80
- /** Wikitext settings controlling rendering behavior */
115
+ /**
116
+ * Context-dependent feature flags.
117
+ * Defaults to page-mode settings when omitted.
118
+ */
81
119
  settings?: WikitextSettings;
82
- /** Page context for resolving file paths, links, etc. */
120
+ /** Page context for resolving relative links, local file paths, etc. */
83
121
  page?: PageContext;
84
- /** Pre-collected footnote elements from SyntaxTree.footnotes */
122
+ /**
123
+ * Pre-collected footnote element arrays from `SyntaxTree.footnotes`.
124
+ * Passed through so the renderer can emit footnote bodies in the
125
+ * `[[footnoteblock]]` section.
126
+ */
85
127
  footnotes?: Element[][];
86
- /** Resolver functions for dynamic content */
128
+ /** Callbacks for resolving users, HTML-block URLs, etc. */
87
129
  resolvers?: RenderResolvers;
88
130
  /**
89
131
  * Sandbox attribute value for htmlBlock iframes.
@@ -106,7 +148,18 @@ interface RenderOptions {
106
148
  embedAllowlist?: EmbedAllowlistEntry[] | null;
107
149
  }
108
150
  /**
109
- * Render a SyntaxTree to HTML string
151
+ * Render a {@link SyntaxTree} to an HTML string.
152
+ *
153
+ * This is the main entry point of `@wdprlib/render`. It walks the AST
154
+ * produced by `@wdprlib/parser`, serialises each element to HTML, and
155
+ * appends any collected `[[module CSS]]` styles at the end (when
156
+ * `WikitextSettings.allowStyleElements` is `true`).
157
+ *
158
+ * @param tree - Parsed AST (from `parse()` or `resolveModules()`)
159
+ * @param options - Rendering configuration
160
+ * @returns Complete HTML string
161
+ *
162
+ * @group Render
110
163
  */
111
164
  declare function renderToHtml(tree: SyntaxTree2, options?: RenderOptions): string;
112
165
  import { WikitextMode, WikitextSettings as WikitextSettings3 } from "@wdprlib/ast";
package/dist/index.js CHANGED
@@ -272,11 +272,17 @@ function isValidCssColor(color) {
272
272
  if (/^#[0-9a-f]{3}([0-9a-f])?$/.test(trimmed) || /^#[0-9a-f]{6}([0-9a-f]{2})?$/.test(trimmed)) {
273
273
  return true;
274
274
  }
275
- if (/^rgba?\(\s*\d{1,3}\s*,\s*\d{1,3}\s*,\s*\d{1,3}\s*(,\s*(0|1|0?\.\d+))?\s*\)$/.test(trimmed)) {
276
- return true;
277
- }
278
- if (/^hsla?\(\s*\d{1,3}\s*,\s*\d{1,3}%\s*,\s*\d{1,3}%\s*(,\s*(0|1|0?\.\d+))?\s*\)$/.test(trimmed)) {
279
- return true;
275
+ const fnMatch = trimmed.match(/^(rgba?|hsla?)\(([^)]*)\)$/);
276
+ if (fnMatch) {
277
+ const fn = fnMatch[1];
278
+ const args = fnMatch[2].split(",").map((s) => s.trim()).join(",");
279
+ if (fn.startsWith("rgb")) {
280
+ if (/^\d{1,3},\d{1,3},\d{1,3}(,(0|1|0?\.\d+))?$/.test(args))
281
+ return true;
282
+ } else {
283
+ if (/^\d{1,3},\d{1,3}%,\d{1,3}%(,(0|1|0?\.\d+))?$/.test(args))
284
+ return true;
285
+ }
280
286
  }
281
287
  return false;
282
288
  }
@@ -4065,7 +4071,7 @@ function renderJoin(ctx, data) {
4065
4071
  const buttonText = data["button-text"] ?? "Join";
4066
4072
  const attrs = data.attributes ?? {};
4067
4073
  const className = attrs.class ?? "join-box";
4068
- ctx.push(`<div class="${escapeHtml(className)}">`);
4074
+ ctx.push(`<div class="${escapeAttr(className)}">`);
4069
4075
  ctx.push(`<a href="javascript:;">${escapeHtml(buttonText)}</a>`);
4070
4076
  ctx.push("</div>");
4071
4077
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wdprlib/render",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "description": "HTML renderer for Wikidot markup",
5
5
  "keywords": [
6
6
  "html",