@vojtaholik/static-kit-core 2.1.0 → 2.1.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vojtaholik/static-kit-core",
3
- "version": "2.1.0",
3
+ "version": "2.1.2",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -3,6 +3,7 @@ import type { LayoutProps } from "./layout.ts";
3
3
  import { layoutPropsSchema } from "./layout.ts";
4
4
  import { blockRegistry, type RenderContext } from "./block-registry.ts";
5
5
  import type { SchemaAddress } from "./schema-address.ts";
6
+ import { vlnaHtml } from "./vlna.ts";
6
7
 
7
8
  /**
8
9
  * Augmentable block props map — users register their block types via:
@@ -14,7 +15,7 @@ import type { SchemaAddress } from "./schema-address.ts";
14
15
  * }
15
16
  * }
16
17
  */
17
- export interface BlockPropsMap {}
18
+ export interface BlockPropsMap { }
18
19
 
19
20
  type TypedBlockInstance = {
20
21
  [K in keyof BlockPropsMap & string]: {
@@ -120,9 +121,10 @@ export async function renderPage(
120
121
  // Update <html> attributes
121
122
  if (node.nodeName === "html") {
122
123
  setAttr(node, "data-page-id", page.id);
123
- // if (page.density) {
124
- // setAttr(node, "data-density", page.density);
125
- // }
124
+ }
125
+
126
+ if (node.nodeName === 'main' && page.density) {
127
+ setAttr(node, "data-density", page.density);
126
128
  }
127
129
 
128
130
  // Process regions - inject a marker that we'll replace after serialization
@@ -160,6 +162,9 @@ export async function renderPage(
160
162
  html = html.replace(marker, content);
161
163
  }
162
164
 
165
+ // Czech typography: non-breaking spaces after single-char prepositions
166
+ html = vlnaHtml(html);
167
+
163
168
  // Inject dev overlay if in dev mode
164
169
  if (options.isDev) {
165
170
  html = injectDevOverlay(html);
package/src/index.ts CHANGED
@@ -54,5 +54,8 @@ export {
54
54
  type CompileOptions,
55
55
  } from "./template-compiler.ts";
56
56
 
57
+ // Czech typography (vlna)
58
+ export { vlna, vlnaHtml, preventWidow } from "./vlna.ts";
59
+
57
60
  // Configuration
58
61
  export { configSchema, defineConfig, type StaticKitConfig } from "./config.ts";
package/src/vlna.ts ADDED
@@ -0,0 +1,112 @@
1
+ /**
2
+ * Czech typography transform (vlna)
3
+ *
4
+ * Inserts non-breaking spaces (\u00A0) after single-character Czech
5
+ * prepositions and conjunctions (k, s, v, z, o, u, a, i) so they
6
+ * never end up alone at the end of a line.
7
+ *
8
+ * Also prevents widows — a single short word orphaned on the last
9
+ * line of a paragraph.
10
+ *
11
+ * Runs at build time. The util itself is never shipped to browsers;
12
+ * only the resulting \u00A0 characters are baked into the HTML output.
13
+ *
14
+ * Based on ČSN 01 6910 and Petr Olšák's vlna for TeX.
15
+ */
16
+
17
+ /**
18
+ * Single-char Czech prepositions/conjunctions: k s v z o u a i
19
+ * Uses lookbehind so the leading whitespace isn't consumed — handles
20
+ * adjacent prepositions like "v s tím" in a single pass.
21
+ * Only replaces regular spaces (not existing \u00A0).
22
+ */
23
+ const SINGLE_CHAR_RE = /(?<=\s|^)([ksvzouaiKSVZOUAI]) /g;
24
+
25
+ /**
26
+ * Replace space after single-char Czech prepositions/conjunctions with \u00A0.
27
+ * Plain text only — no HTML awareness.
28
+ */
29
+ export function vlna(text: string): string {
30
+ return text.replace(SINGLE_CHAR_RE, "$1\u00A0");
31
+ }
32
+
33
+ /**
34
+ * Prevent widows — replace the last space in a text with \u00A0
35
+ * so the final word doesn't sit alone on a line.
36
+ * Only acts when the last word is ≤15 chars (avoids gluing long URLs etc).
37
+ */
38
+ export function preventWidow(text: string): string {
39
+ return text.replace(/\s(\S{1,15})\s*$/, "\u00A0$1");
40
+ }
41
+
42
+ /** Tags whose text content should NOT be transformed */
43
+ const SKIP_TAGS = new Set([
44
+ "script",
45
+ "style",
46
+ "code",
47
+ "pre",
48
+ "textarea",
49
+ "kbd",
50
+ "var",
51
+ "samp",
52
+ ]);
53
+
54
+ /**
55
+ * Apply Czech typography transforms to all text nodes in an HTML string.
56
+ * Skips content inside <script>, <style>, <code>, <pre>, <textarea>, etc.
57
+ * Does not touch HTML tags or attributes — only text between > and <.
58
+ */
59
+ export function vlnaHtml(html: string): string {
60
+ // Track which tags we're inside to know when to skip
61
+ const tagStack: string[] = [];
62
+ let result = "";
63
+ let i = 0;
64
+
65
+ while (i < html.length) {
66
+ if (html[i] === "<") {
67
+ // Find end of tag
68
+ const tagEnd = html.indexOf(">", i);
69
+ if (tagEnd === -1) {
70
+ // Malformed HTML — just append rest
71
+ result += html.slice(i);
72
+ break;
73
+ }
74
+
75
+ const tag = html.slice(i, tagEnd + 1);
76
+ result += tag;
77
+
78
+ // Parse tag name
79
+ const tagMatch = tag.match(/^<\/?([a-zA-Z][a-zA-Z0-9-]*)/);
80
+ if (tagMatch) {
81
+ const tagName = tagMatch[1]!.toLowerCase();
82
+ if (tag[1] === "/") {
83
+ // Closing tag — pop from stack
84
+ const idx = tagStack.lastIndexOf(tagName);
85
+ if (idx !== -1) tagStack.splice(idx, 1);
86
+ } else if (!tag.endsWith("/>")) {
87
+ // Opening tag (not self-closing)
88
+ tagStack.push(tagName);
89
+ }
90
+ }
91
+
92
+ i = tagEnd + 1;
93
+ } else {
94
+ // Text node — find the next tag
95
+ const nextTag = html.indexOf("<", i);
96
+ const textEnd = nextTag === -1 ? html.length : nextTag;
97
+ const text = html.slice(i, textEnd);
98
+
99
+ // Only transform if we're not inside a skip tag
100
+ const inSkipTag = tagStack.some((t) => SKIP_TAGS.has(t));
101
+ if (inSkipTag) {
102
+ result += text;
103
+ } else {
104
+ result += vlna(text);
105
+ }
106
+
107
+ i = textEnd;
108
+ }
109
+ }
110
+
111
+ return result;
112
+ }