@vojtaholik/static-kit-core 2.1.1 → 2.1.3

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.1",
3
+ "version": "2.1.3",
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:
@@ -161,6 +162,9 @@ export async function renderPage(
161
162
  html = html.replace(marker, content);
162
163
  }
163
164
 
165
+ // Czech typography: non-breaking spaces after single-char prepositions
166
+ html = vlnaHtml(html);
167
+
164
168
  // Inject dev overlay if in dev mode
165
169
  if (options.isDev) {
166
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,123 @@
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
+ * Short (2–3 char) Czech prepositions that shouldn't end a line.
27
+ * Case-insensitive match at word boundary.
28
+ */
29
+ const SHORT_PREP_RE =
30
+ /(?<=\s|^)(bez|nad|pod|pro|při|přes|před|mezi|jako|ani|ale|nebo|Bez|Nad|Pod|Pro|Při|Přes|Před|Mezi|Jako|Ani|Ale|Nebo|BEZ|NAD|POD|PRO|PŘI|PŘES|PŘED|MEZI|JAKO|ANI|ALE|NEBO|do|ke|ku|na|od|po|se|ve|za|ze|Do|Ke|Ku|Na|Od|Po|Se|Ve|Za|Ze|DO|KE|KU|NA|OD|PO|SE|VE|ZA|ZE) /g;
31
+
32
+ /**
33
+ * Replace space after Czech prepositions/conjunctions with \u00A0.
34
+ * Handles single-char (k, s, v…) and short words (bez, nad, pro…).
35
+ * Plain text only — no HTML awareness.
36
+ */
37
+ export function vlna(text: string): string {
38
+ return text
39
+ .replace(SINGLE_CHAR_RE, "$1\u00A0")
40
+ .replace(SHORT_PREP_RE, "$1\u00A0");
41
+ }
42
+
43
+ /**
44
+ * Prevent widows — replace the last space in a text with \u00A0
45
+ * so the final word doesn't sit alone on a line.
46
+ * Only acts when the last word is ≤15 chars (avoids gluing long URLs etc).
47
+ */
48
+ export function preventWidow(text: string): string {
49
+ return text.replace(/\s(\S{1,15})\s*$/, "\u00A0$1");
50
+ }
51
+
52
+ /** Tags whose text content should NOT be transformed */
53
+ const SKIP_TAGS = new Set([
54
+ "script",
55
+ "style",
56
+ "code",
57
+ "pre",
58
+ "textarea",
59
+ "kbd",
60
+ "var",
61
+ "samp",
62
+ "title",
63
+ ]);
64
+
65
+ /**
66
+ * Apply Czech typography transforms to all text nodes in an HTML string.
67
+ * Skips content inside <script>, <style>, <code>, <pre>, <textarea>, etc.
68
+ * Does not touch HTML tags or attributes — only text between > and <.
69
+ */
70
+ export function vlnaHtml(html: string): string {
71
+ // Track which tags we're inside to know when to skip
72
+ const tagStack: string[] = [];
73
+ let result = "";
74
+ let i = 0;
75
+
76
+ while (i < html.length) {
77
+ if (html[i] === "<") {
78
+ // Find end of tag
79
+ const tagEnd = html.indexOf(">", i);
80
+ if (tagEnd === -1) {
81
+ // Malformed HTML — just append rest
82
+ result += html.slice(i);
83
+ break;
84
+ }
85
+
86
+ const tag = html.slice(i, tagEnd + 1);
87
+ result += tag;
88
+
89
+ // Parse tag name
90
+ const tagMatch = tag.match(/^<\/?([a-zA-Z][a-zA-Z0-9-]*)/);
91
+ if (tagMatch) {
92
+ const tagName = tagMatch[1]!.toLowerCase();
93
+ if (tag[1] === "/") {
94
+ // Closing tag — pop from stack
95
+ const idx = tagStack.lastIndexOf(tagName);
96
+ if (idx !== -1) tagStack.splice(idx, 1);
97
+ } else if (!tag.endsWith("/>")) {
98
+ // Opening tag (not self-closing)
99
+ tagStack.push(tagName);
100
+ }
101
+ }
102
+
103
+ i = tagEnd + 1;
104
+ } else {
105
+ // Text node — find the next tag
106
+ const nextTag = html.indexOf("<", i);
107
+ const textEnd = nextTag === -1 ? html.length : nextTag;
108
+ const text = html.slice(i, textEnd);
109
+
110
+ // Only transform if we're not inside a skip tag
111
+ const inSkipTag = tagStack.some((t) => SKIP_TAGS.has(t));
112
+ if (inSkipTag) {
113
+ result += text;
114
+ } else {
115
+ result += preventWidow(vlna(text));
116
+ }
117
+
118
+ i = textEnd;
119
+ }
120
+ }
121
+
122
+ return result;
123
+ }