microsoft-webui-press 0.0.24

High-performance Static Site Generator powered by WebUI Framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

//! Build orchestrator: content pipeline → protocol build → parallel render → output.

use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::atomic::AtomicU64;
use std::sync::{Arc, OnceLock};
use std::time::Instant;

use console::style;
use rayon::prelude::*;
use serde_json::{Map, Value};
use webui::BuildOptions;
use webui_handler::{Protocol, RenderOptions, ResponseWriter, WebUIHandler};
use webui_tokens::TokenFile;

use crate::bundler::{
    bundle_assets, collect_component_scripts_for_html, discover_component_scripts,
    extract_bundle_scripts, inject_module_script_tags, inject_script_tag, module_script_tag,
    page_bundle_signature, resolve_config_component_source, resolve_node_modules, BundleOptions,
    BundleResult, BundleThread, PageBundleEntry, RootBundleEntry, ScriptSource,
};
use crate::content::process_content_with_states;
use crate::error::{Error, Result};
use crate::markdown::Highlighter;
use crate::state::{load_render_states, merge_page_state};
use crate::types::{BuildStats, DocsConfig};

/// Persistent state held by the dev server across rebuilds. The dev
/// server always performs a full rebuild on every watcher tick — the
/// previous incremental machinery proved too complex for the marginal
/// time savings — so this struct exists solely to amortize startup
/// costs that don't depend on user-edited content.
///
/// Today that's just the syntect `Highlighter` (the syntax + theme
/// load is ~30-50ms). One-shot CLI builds construct a fresh
/// `BuildCache::default()` and discard it.
#[derive(Default)]
pub struct BuildCache {
    /// Syntect highlighter — kept alive across rebuilds so we don't pay
    /// the syntax/theme load cost on every keystroke.
    pub(crate) highlighter: Option<Highlighter>,
    /// Suppress per-step terminal output (`✔ Rendered 31 pages`, etc.).
    /// The dev server flips this on after the first build so subsequent
    /// rebuilds collapse into the rolling rebuild line.
    pub quiet: bool,
    /// Skip the `remove_dir_all(out_dir)` step. Files are overwritten
    /// in place. Dev-mode optimization — saves the macOS-ENOTEMPTY
    /// retry path and shaves disk I/O off every rebuild. Stale files
    /// from deleted source pages survive until the next process restart.
    pub skip_clean: bool,
    /// Dev-mode flag: when true, the bundler skips minification for
    /// faster rebuilds during `webui-press serve`.
    pub dev_mode: bool,
}

impl BuildCache {
    /// Create an empty cache with verbose output and clean-then-build
    /// semantics. Subsequent calls to [`build_docs_with_cache`] will
    /// populate it on first use.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Switch the cache into dev-server rebuild mode: suppresses
    /// per-step output banners and skips wiping the output directory.
    pub fn set_dev_rebuild(&mut self) {
        self.quiet = true;
        self.skip_clean = true;
        self.dev_mode = true;
    }
}

fn load_theme(theme: &str, config_dir: &Path) -> Result<TokenFile> {
    let config_relative = config_dir.join(theme);
    let resolved = if config_relative.exists() {
        config_relative
            .canonicalize()
            .map_err(|e| Error::Build(format!("Failed to canonicalize {theme}: {e}")))?
    } else {
        webui_tokens::resolve_theme_path(theme, config_dir)
            .map_err(|e| Error::Build(format!("Failed to resolve theme {theme}: {e}")))?
    };
    webui_tokens::load_token_file(&resolved)
        .map_err(|e| Error::Build(format!("Failed to load theme {}: {e}", resolved.display())))
}

fn inject_theme_tokens(
    state: &mut Value,
    token_file: &TokenFile,
    protocol_tokens: &[String],
) -> Result<()> {
    let resolved = webui_tokens::resolve_tokens(protocol_tokens, token_file)
        .map_err(|e| Error::Build(format!("Failed to resolve theme tokens: {e}")))?;
    webui_tokens::inject_into_state(state, &resolved);
    Ok(())
}

struct NotFoundStateInput<'a> {
    config: &'a DocsConfig,
    base_path: &'a str,
    nav: Value,
    footer: Value,
    content: &'a str,
    head_injection: &'a str,
    global_state: Option<&'a Value>,
}

fn build_not_found_state(input: NotFoundStateInput<'_>) -> Value {
    let state = json_obj([
        (
            "site",
            json_obj([
                ("title", Value::String(input.config.site.title.clone())),
                ("base", Value::String(input.base_path.to_string())),
            ]),
        ),
        ("navigation", input.nav),
        ("sidebar", json_obj([("sections", Value::Array(vec![]))])),
        (
            "page",
            json_obj([
                ("title", Value::String("Page Not Found".to_string())),
                (
                    "description",
                    Value::String("The page you're looking for doesn't exist.".to_string()),
                ),
                ("content", Value::String(input.content.to_string())),
                ("isHome", Value::Bool(false)),
                ("layout", Value::String("doc".to_string())),
            ]),
        ),
        ("hero", Value::Null),
        ("footer", input.footer),
        ("prev", Value::Null),
        ("next", Value::Null),
        ("pageData", Value::Null),
        ("headTags", Value::String(input.head_injection.to_string())),
        ("label", Value::String("Copy".to_string())),
        ("icon", Value::String("🌙".to_string())),
    ]);

    merge_page_state(state, input.global_state, None)
}

// ── Output helpers ──────────────────────────────────────────────
//
// Mirrors the styling vocabulary in `crates/webui-cli/src/utils/output.rs`
// so webui-press feels at home in a workspace where users may also see
// `webui build` / `webui serve` output.

/// Monotonic counter used to produce unique per-rebuild temp-dir names.
/// Combined with the process id and a fxhash of the page path so two
/// rebuilds running in the same process can never collide on the
/// per-page scratch dir.
static REBUILD_NONCE: AtomicU64 = AtomicU64::new(0);

fn print_header(cache: &BuildCache, title: &str) {
    if cache.quiet {
        return;
    }
    eprintln!(
        "\n  {} {}",
        style("⚡").cyan().bold(),
        style(title).cyan().bold()
    );
}

fn print_success(cache: &BuildCache, message: &str) {
    if cache.quiet {
        return;
    }
    eprintln!("  {} {message}", style("✔").green());
}

fn print_bundle_summary(cache: &BuildCache, result: &BundleResult) {
    print_success(
        cache,
        &format!(
            "Bundled {} component script{} into {} root script{} and {} page script group{} in {:.0}ms",
            result.component_count,
            if result.component_count == 1 { "" } else { "s" },
            usize::from(result.root_script.is_some()),
            if result.root_script.is_some() {
                ""
            } else {
                "s"
            },
            result.page_entry_count,
            if result.page_entry_count == 1 {
                ""
            } else {
                "s"
            },
            result.duration.as_secs_f64() * 1000.0
        ),
    );
}

fn print_finish(cache: &BuildCache, message: &str) {
    if cache.quiet {
        return;
    }
    eprintln!("\n  {} {message}\n", style("✨").green());
}

/// Build a JSON object from key-value pairs without using `json!` (which calls `unwrap`).
fn json_obj<const N: usize>(entries: [(&str, Value); N]) -> Value {
    let mut map = Map::with_capacity(N);
    for (k, v) in entries {
        map.insert(k.to_string(), v);
    }
    Value::Object(map)
}

/// A writer that collects rendered HTML into a String buffer.
struct StringWriter {
    buf: String,
}

impl StringWriter {
    fn with_capacity(cap: usize) -> Self {
        Self {
            buf: String::with_capacity(cap),
        }
    }
}

impl ResponseWriter for StringWriter {
    fn write(&mut self, content: &str) -> webui_handler::Result<()> {
        self.buf.push_str(content);
        Ok(())
    }

    fn end(&mut self) -> webui_handler::Result<()> {
        Ok(())
    }
}

/// Build a documentation site from the given configuration.
///
/// `config_dir` is the directory containing `config.json`. It is used to
/// resolve relative paths declared in the config (such as a custom page's
/// `state_file`).
///
/// One-shot variant for the CLI build command. The dev server uses
/// [`build_docs_with_cache`] so the syntect highlighter survives across
/// rebuilds.
pub fn build_docs(
    config: &DocsConfig,
    config_dir: &Path,
    template_dir: &Path,
) -> Result<BuildStats> {
    let mut cache = BuildCache::new();
    build_docs_with_cache(config, config_dir, template_dir, &mut cache)
}

/// Build a documentation site, reusing the supplied [`BuildCache`] across
/// invocations.
///
/// **Always performs a full rebuild.** The previous incremental machinery
/// (per-page state hashes, component-bundle signatures, descriptor cache,
/// search-index sig, etc.) was removed because the resulting invariants
/// were complex for a dev-server-only optimization. The rebuild is
/// already fast in practice and a from-scratch run keeps the output
/// guaranteed-consistent with the source.
///
/// The cache exists today purely to amortize startup costs that are
/// independent of user-edited content (the syntect highlighter load).
pub fn build_docs_with_cache(
    config: &DocsConfig,
    config_dir: &Path,
    template_dir: &Path,
    cache: &mut BuildCache,
) -> Result<BuildStats> {
    let start = Instant::now();
    let base_path = &config.base_path;
    let out_dir = Path::new(&config.out_dir);

    // Flat output: site files live at the root of `out_dir`. URLs in HTML
    // include `basePath` via `<base href>` and link rewriting; the dev server
    // (`webui-press serve`) maps URL `<basePath>/foo` → file `out_dir/foo`.
    // For deploys (e.g. GitHub Pages project sites), the host mounts `out_dir`
    // at `<basePath>`, so the same flat layout works.
    let site_dir = out_dir.to_path_buf();

    // Read custom CSS
    let custom_css = config
        .css
        .as_ref()
        .and_then(|p| fs::read_to_string(p).ok())
        .unwrap_or_default();
    let token_file = match &config.theme {
        Some(theme) => Some(load_theme(theme, config_dir)?),
        None => None,
    };

    print_header(cache, &config.site.title);
    eprintln!();

    // Compute the `<head>` injection string up-front, BEFORE any
    // filesystem mutations. Including it here makes it cheap to bake
    // into each page's state inside `process_content` (avoiding a
    // post-process_content mutation pass that would force-clone every
    // cached descriptor) and lets the content cache invalidate when
    // the link tags change. We don't write the CSS files yet — that
    // happens after content processing succeeds, so a content failure
    // can't corrupt the previous valid output.
    let base_css_src = template_dir.join("docs.css");
    let has_base_css = base_css_src.exists();
    let base_css_link = if has_base_css {
        format!("<link rel=\"stylesheet\" href=\"{base_path}docs.css\">")
    } else {
        String::new()
    };
    let has_theme_css = !custom_css.is_empty();
    let theme_css_link = if has_theme_css {
        format!("<link rel=\"stylesheet\" href=\"{base_path}theme.css\">")
    } else {
        String::new()
    };
    let head_injection = {
        let mut parts: Vec<String> = Vec::with_capacity(2 + config.head.len());
        if !base_css_link.is_empty() {
            parts.push(base_css_link.clone());
        }
        if !theme_css_link.is_empty() {
            parts.push(theme_css_link.clone());
        }
        for tag in &config.head {
            parts.push(tag.to_html());
        }
        parts.join("\n  ")
    };

    // Step 1: Process content. Highlighter is cached across rebuilds —
    // syntect's default syntax/theme load is ~30-50ms which we don't want
    // to pay every keystroke.
    let render_states = load_render_states(config, config_dir)?;
    let highlighter = cache.highlighter.take().unwrap_or_default();
    let pages = process_content_with_states(config, &highlighter, &head_injection, &render_states)?;

    // Restore the highlighter for the next rebuild.
    cache.highlighter = Some(highlighter);

    // Step 2: Resolve component sources for the per-page builds
    let mut component_sources: Vec<String> = Vec::new();
    let cwd = std::env::current_dir().unwrap_or_default();
    // Built-in component library (e.g. crates/webui-press/components/)
    let builtin_components = template_dir.parent().map(|p| p.join("components"));
    if let Some(ref bc) = builtin_components {
        if bc.exists() {
            component_sources.push(bc.to_string_lossy().to_string());
        }
    }
    // User component sources from config. Local paths are resolved against
    // the current project root; npm package names/scopes must stay bare so
    // webui-discovery resolves them from node_modules.
    if let Some(ref user_sources) = config.components {
        for source in user_sources {
            component_sources.push(resolve_config_component_source(source, &cwd));
        }
    }
    // Template-local components (e.g. docs-search, docs-theme-toggle living
    // beside the template's index.html).
    component_sources.push(template_dir.to_string_lossy().to_string());

    let template_html = fs::read_to_string(template_dir.join("index.html"))
        .map_err(|e| Error::Build(format!("Failed to read template: {e}")))?;
    let component_script_index = discover_component_scripts(&component_sources)?;

    // Step 3: Wipe the previous output and recreate the site root.
    // Always done — a from-scratch run guarantees the output matches
    // the current source with no possibility of stale per-page dirs,
    // stale CSS files, or orphaned assets surviving a config change.
    if out_dir.exists() && !cache.skip_clean {
        remove_dir_all_retry(out_dir)
            .map_err(|e| Error::Io(format!("Cannot clean output dir: {e}")))?;
    }
    fs::create_dir_all(&site_dir).map_err(|e| Error::Io(format!("Cannot create site dir: {e}")))?;

    // Materialize the CSS files referenced by the head injection. Done
    // after content processing succeeds so a content failure can't
    // corrupt the previous valid output (the wipe above still happens
    // because we clean before processing — but that order means a
    // failure leaves no output at all, never half-output).
    if has_base_css {
        fs::copy(&base_css_src, site_dir.join("docs.css"))
            .map_err(|e| Error::Io(format!("Cannot copy docs.css: {e}")))?;
    }
    if has_theme_css {
        fs::write(site_dir.join("theme.css"), &custom_css)
            .map_err(|e| Error::Io(format!("Cannot write theme.css: {e}")))?;
    }

    let handler = WebUIHandler::with_plugin(|| {
        Box::new(webui_handler::plugin::webui::WebUIHydrationPlugin::new())
    });

    // Pre-create per-page output directories sequentially (avoids races and
    // cheap fs::create_dir_all calls competing on the same parent paths).
    for page in &pages {
        let page_dir = site_dir.join(page.path.strip_prefix(base_path).unwrap_or(&page.path));
        fs::create_dir_all(&page_dir)
            .map_err(|e| Error::Io(format!("Cannot create dir {}: {e}", page_dir.display())))?;
    }

    // Step 2b: Extract <script bundle> tags from page content BEFORE
    // rendering. Page scripts become imports in the page's virtual esbuild
    // entry; the source HTML keeps plain, non-bundled scripts untouched.
    //
    // Also collect `scriptFile` entries from customPages config. These
    // produce per-page scripts without polluting the HTML string.
    let mut explicit_page_scripts: HashMap<String, Vec<ScriptSource>> = HashMap::new();
    let mut page_contents_with_scripts: HashMap<String, String> = HashMap::new();

    for page in &pages {
        let content = page.state["page"]["content"].as_str().unwrap_or("");
        if content.contains("<script") && content.contains("bundle") {
            let (modified, scripts) = extract_bundle_scripts(content);
            if !scripts.is_empty() {
                page_contents_with_scripts.insert(page.path.clone(), modified);
                explicit_page_scripts
                    .entry(page.path.clone())
                    .or_default()
                    .extend(scripts);
            }
        }
    }

    // Collect scriptFile from customPages config.
    for (link, custom_page) in &config.custom_pages {
        if let Some(script_path) = custom_page.script_file() {
            // Build the full page path matching how process_content constructs
            // URL paths: base_path + link (minus leading slash).
            let normalized = if link.ends_with('/') {
                link.clone()
            } else {
                format!("{link}/")
            };
            let page_path = format!("{}{}", base_path, &normalized[1..]);
            explicit_page_scripts
                .entry(page_path)
                .or_default()
                .push(ScriptSource::File(script_path.to_string()));
        }
    }

    let not_found_content = format!(
        "<h1>404 — Page Not Found</h1>\
         <p>The page you're looking for doesn't exist or has been moved.</p>\
         <p><a href=\"{base_path}\">← Back to Home</a></p>"
    );

    let template_script_path = template_dir.join("index.ts");
    let template_script = if template_script_path.exists() {
        Some(
            template_script_path
                .canonicalize()
                .unwrap_or(template_script_path),
        )
    } else {
        None
    };
    let root_component_scripts =
        collect_component_scripts_for_html(&[template_html.as_str()], &component_script_index)?;
    let root_bundle = if template_script.is_some() || !root_component_scripts.is_empty() {
        Some(RootBundleEntry {
            script_path: template_script,
            component_scripts: root_component_scripts,
        })
    } else {
        None
    };

    // Build virtual entries only for pages with content-specific scripts.
    // Template/chrome scripts are loaded once through the root entry, so pages
    // that only use template components do not get duplicate tiny page-N files.
    let mut page_bundles: Vec<PageBundleEntry> = Vec::new();
    let mut page_bundle_ids: HashMap<String, usize> = HashMap::with_capacity(pages.len());
    let mut page_bundle_signatures: HashMap<String, usize> = HashMap::new();
    for page in &pages {
        let content = page_contents_with_scripts
            .get(&page.path)
            .map(String::as_str)
            .unwrap_or_else(|| page.state["page"]["content"].as_str().unwrap_or(""));
        let mut component_scripts =
            collect_component_scripts_for_html(&[content], &component_script_index)?;
        if let Some(root) = &root_bundle {
            component_scripts.retain(|path| !root.component_scripts.contains(path));
        }
        let explicit_scripts = explicit_page_scripts.remove(&page.path).unwrap_or_default();
        if component_scripts.is_empty() && explicit_scripts.is_empty() {
            continue;
        }

        let signature = page_bundle_signature(&component_scripts, &explicit_scripts, config_dir);
        if let Some(&id) = page_bundle_signatures.get(&signature) {
            page_bundle_ids.insert(page.path.clone(), id);
            continue;
        }

        let id = page_bundles.len();
        page_bundle_signatures.insert(signature, id);
        page_bundle_ids.insert(page.path.clone(), id);
        page_bundles.push(PageBundleEntry {
            id,
            page_path: page.path.clone(),
            component_scripts,
            explicit_scripts,
        });
    }

    let not_found_component_scripts =
        collect_component_scripts_for_html(&[not_found_content.as_str()], &component_script_index)?;
    let not_found_bundle_id = if not_found_component_scripts.is_empty() {
        None
    } else {
        let signature = page_bundle_signature(&not_found_component_scripts, &[], config_dir);
        if let Some(&id) = page_bundle_signatures.get(&signature) {
            Some(id)
        } else {
            let id = page_bundles.len();
            page_bundle_signatures.insert(signature, id);
            page_bundles.push(PageBundleEntry {
                id,
                page_path: format!("{base_path}404/"),
                component_scripts: not_found_component_scripts,
                explicit_scripts: Vec::new(),
            });
            Some(id)
        }
    };

    // Start the one client bundle in parallel with per-page template parsing.
    // Each page blocks only when it reaches projection finalization.
    let node_modules = if root_bundle.is_some() || !page_bundles.is_empty() {
        Some(resolve_node_modules(config_dir)?)
    } else {
        None
    };
    let (projection_source, projection_completer) = webui::projection_manifest_barrier();
    let site_dir_for_bundle = site_dir.clone();
    let root_bundle_for_bundle = root_bundle.clone();
    let page_bundles_for_bundle = page_bundles.clone();
    let bundler_config = config.bundler.clone();
    let config_dir_for_bundle = config_dir.to_path_buf();
    let content_dir_for_bundle = PathBuf::from(&config.content_dir);
    let base_path_for_bundle = base_path.to_string();
    let dev_mode = cache.dev_mode;
    // Published before the projection completer wakes page builds. This keeps
    // parsing overlapped with esbuild while making exact generated URLs
    // available before each protocol is handed to the renderer.
    let generated_preloads = Arc::new(OnceLock::new());
    let generated_preloads_for_bundle = Arc::clone(&generated_preloads);
    let bundle_thread = BundleThread::spawn(move || {
        match bundle_assets(&BundleOptions {
            site_dir: &site_dir_for_bundle,
            base_path: &base_path_for_bundle,
            node_modules: node_modules.as_deref(),
            root_bundle: root_bundle_for_bundle.as_ref(),
            page_bundles: &page_bundles_for_bundle,
            bundler_config: bundler_config.as_ref(),
            dev_mode,
            config_dir: &config_dir_for_bundle,
            content_dir: &content_dir_for_bundle,
        }) {
            Ok(result) => {
                if generated_preloads_for_bundle
                    .set(result.preloads.clone())
                    .is_err()
                {
                    let message = "Generated preload metadata was published twice".to_string();
                    projection_completer.complete(Err(message.clone()));
                    return Err(Error::Build(message));
                }
                projection_completer.complete(Ok(result.projection.clone()));
                Ok(result)
            }
            Err(error) => {
                projection_completer.complete(Err(error.to_string()));
                Err(error)
            }
        }
    });

    // Per-page build + render + write in parallel.
    //
    // Each page is its own complete app: we substitute `{{{page.content}}}`
    // in the template with the page's actual HTML (including any custom
    // elements like <code-block>) and run `webui::build()` on the result.
    // This means the build pipeline naturally discovers the components used
    // on this page, expands their declarative shadow DOM, and emits their
    // template metadata to `window.__webui.templates` for client-side
    // hydration — no manual registration tricks required.
    let total_bytes = std::sync::atomic::AtomicUsize::new(0);
    let component_css: std::sync::Mutex<HashMap<String, String>> =
        std::sync::Mutex::new(HashMap::new());

    let page_start = Instant::now();
    pages.par_iter().try_for_each(|page| -> Result<()> {
        let page_dir = site_dir.join(page.path.strip_prefix(base_path).unwrap_or(&page.path));
        let target = page_dir.join("index.html");

        // Use modified content (with bundled script tags removed) if scripts were extracted.
        let content = page_contents_with_scripts
            .get(&page.path)
            .map(|s| s.as_str())
            .unwrap_or_else(|| page.state["page"]["content"].as_str().unwrap_or(""));

        // Protect <pre> blocks from HTML parser whitespace normalization.
        let (protected, pre_blocks) = protect_pre_blocks(content);

        // Substitute the raw signal in the template with the literal HTML.
        let page_html = template_html.replace("{{{page.content}}}", &protected);

        // Per-page temp dir holding only this page's index.html — components
        // come exclusively from `component_sources`, which already includes
        // the template dir (for docs-search/docs-theme-toggle) plus any
        // configured component libraries.
        //
        // Name includes pid + per-rebuild nonce so two parallel page
        // builds (and successive rebuilds) can never collide and wipe
        // each other's in-progress files.
        let nonce = REBUILD_NONCE.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        let page_tmp = std::env::temp_dir().join(format!(
            "webui-press-page-{}-{:x}-{nonce:x}",
            std::process::id(),
            fxhash(&page.path),
        ));
        if page_tmp.exists() {
            fs::remove_dir_all(&page_tmp).ok();
        }
        fs::create_dir_all(&page_tmp)
            .map_err(|e| Error::Io(format!("Cannot create page temp: {e}")))?;
        fs::write(page_tmp.join("index.html"), &page_html)
            .map_err(|e| Error::Io(format!("Cannot write page temp: {e}")))?;

        let mut build_result = webui::build(BuildOptions {
            app_dir: page_tmp.clone(),
            entry: "index.html".to_string(),
            plugin: Some(webui::Plugin::WebUI),
            components: component_sources.clone(),
            theme: token_file.clone(),
            projection_manifests: vec![projection_source.clone()],
            ..BuildOptions::default()
        })
        .map_err(|e| Error::Build(format!("{}: {e}", page.path)))?;
        let preloads = generated_preloads.get().ok_or_else(|| {
            Error::Build("Generated preload metadata was not published".to_string())
        })?;
        let (hrefs, mut preload_warnings) = preloads.resolve(
            &build_result.protocol.module_preloads,
            page_bundle_ids.get(&page.path).copied(),
        );
        build_result.protocol.module_preloads = hrefs;
        build_result.warnings.append(&mut preload_warnings);

        total_bytes.fetch_add(
            build_result.protocol_bytes.len(),
            std::sync::atomic::Ordering::Relaxed,
        );

        // Collect per-component CSS files (Link strategy emits one .css
        // per component used on the page). Multiple pages share components,
        // so we dedupe by filename in a shared map and write once after
        // the parallel pass completes.
        if !build_result.css_files.is_empty() {
            let mut css_map = component_css
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            for (name, content) in build_result.css_files {
                css_map.entry(name).or_insert(content);
            }
        }

        let mut themed_state;
        let render_state = if let Some(token_file) = token_file.as_ref() {
            themed_state = page.state.clone();
            inject_theme_tokens(&mut themed_state, token_file, &build_result.protocol.tokens)?;
            &themed_state
        } else {
            &page.state
        };

        let protocol = Protocol::new(build_result.protocol);
        let mut writer = StringWriter::with_capacity(8192);
        handler
            .render(
                &protocol,
                render_state,
                &RenderOptions::new("index.html", &page.path),
                &mut writer,
            )
            .map_err(|e| Error::Render(format!("{}: {e}", page.path)))?;

        // Restore the protected <pre> blocks via a single-pass scan.
        let html = restore_pre_blocks(&writer.buf, &pre_blocks);

        // Write directly inside the parallel closure.
        fs::write(&target, &html)
            .map_err(|e| Error::Io(format!("Cannot write {}: {e}", page.path)))?;

        fs::remove_dir_all(&page_tmp).ok();
        Ok(())
    })?;

    print_success(
        cache,
        &format!(
            "Rendered {} pages in {:.0}ms",
            pages.len(),
            page_start.elapsed().as_secs_f64() * 1000.0
        ),
    );
    let bundle_result = bundle_thread.join()?;
    print_bundle_summary(cache, &bundle_result);

    // Step 4: Search index. Strip rendered HTML to plain text and emit
    // an entry per non-home page. Done from-scratch every build.
    let search_path = site_dir.join("search-index.json");
    let search_entries: Vec<Value> = pages
        .iter()
        .filter(|p| !p.is_home)
        .map(|p| {
            let html = p.state["page"]["content"].as_str().unwrap_or("");
            let title = p.state["page"]["title"].as_str().unwrap_or("");
            build_search_entry(title, &p.path, html)
        })
        .collect();
    fs::write(
        &search_path,
        serde_json::to_string(&search_entries)
            .map_err(|e| Error::Build(format!("JSON error: {e}")))?,
    )
    .map_err(|e| Error::Io(e.to_string()))?;
    print_success(
        cache,
        &format!("Indexed {} pages for search", search_entries.len()),
    );

    // Step 5: Copy static public assets.
    let public_dir = Path::new(&config.public_dir);
    if public_dir.exists() {
        copy_dir(public_dir, &site_dir)?;
    }

    // Step 6: Generate 404 page
    let nav_val = pages
        .first()
        .map(|p| p.state["navigation"].clone())
        .unwrap_or_default();

    let footer_val = config
        .footer
        .as_ref()
        .map(|f| json_obj([("html", Value::String(f.html.clone()))]))
        .unwrap_or(Value::Null);

    let mut not_found_state = build_not_found_state(NotFoundStateInput {
        config,
        base_path,
        nav: nav_val,
        footer: footer_val,
        content: &not_found_content,
        head_injection: &head_injection,
        global_state: render_states.global(),
    });

    let not_found_html = template_html.replace("{{{page.content}}}", &not_found_content);
    let nf_tmp = std::env::temp_dir().join(format!(
        "webui-press-404-{}-{:x}",
        std::process::id(),
        REBUILD_NONCE.fetch_add(1, std::sync::atomic::Ordering::Relaxed),
    ));
    if nf_tmp.exists() {
        fs::remove_dir_all(&nf_tmp).ok();
    }
    fs::create_dir_all(&nf_tmp).map_err(|e| Error::Io(e.to_string()))?;
    fs::write(nf_tmp.join("index.html"), &not_found_html).map_err(|e| Error::Io(e.to_string()))?;

    let mut nf_build = webui::build(BuildOptions {
        app_dir: nf_tmp.clone(),
        entry: "index.html".to_string(),
        plugin: Some(webui::Plugin::WebUI),
        components: component_sources.clone(),
        theme: token_file.clone(),
        projection_manifests: vec![projection_source],
        ..BuildOptions::default()
    })
    .map_err(|e| Error::Build(format!("404 build failed: {e}")))?;
    let preloads = generated_preloads
        .get()
        .ok_or_else(|| Error::Build("Generated preload metadata was not published".to_string()))?;
    let (hrefs, mut preload_warnings) =
        preloads.resolve(&nf_build.protocol.module_preloads, not_found_bundle_id);
    nf_build.protocol.module_preloads = hrefs;
    nf_build.warnings.append(&mut preload_warnings);

    if let Some(token_file) = token_file.as_ref() {
        inject_theme_tokens(&mut not_found_state, token_file, &nf_build.protocol.tokens)?;
    }

    // Fold the 404 page's component CSS into the shared map, then write
    // all per-component stylesheets at site root in one pass. The handler
    // emits relative hrefs like `<link rel="stylesheet" href="code-block.css">`;
    // the template's <base href="{site.base}"> resolves them against site root.
    {
        let mut css_map = component_css
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        for (name, content) in &nf_build.css_files {
            css_map
                .entry(name.clone())
                .or_insert_with(|| content.clone());
        }
    }

    let nf_protocol = Protocol::new(nf_build.protocol);
    let mut writer_404 = StringWriter::with_capacity(4096);
    handler
        .render(
            &nf_protocol,
            &not_found_state,
            &RenderOptions::new("index.html", &format!("{base_path}404/")),
            &mut writer_404,
        )
        .map_err(|e| Error::Render(format!("404: {e}")))?;

    fs::write(site_dir.join("404.html"), writer_404.buf).map_err(|e| Error::Io(e.to_string()))?;
    fs::remove_dir_all(&nf_tmp).ok();
    print_success(cache, "Generated 404 page");

    // Step 7: Write per-component stylesheets collected during the
    // parallel page builds (and the 404 build above). Sort by name for
    // deterministic output.
    let css_map_local = component_css
        .into_inner()
        .unwrap_or_else(std::sync::PoisonError::into_inner);
    let mut css_names: Vec<&String> = css_map_local.keys().collect();
    css_names.sort();
    let css_count = css_names.len();
    for name in css_names {
        fs::write(site_dir.join(name), &css_map_local[name])
            .map_err(|e| Error::Io(format!("Cannot write {name}: {e}")))?;
    }
    if css_count > 0 {
        print_success(cache, &format!("Wrote {css_count} component stylesheets"));
    }

    // Step 8: Inject page script <script> tags into rendered pages.
    if bundle_result.root_script.is_some() || !bundle_result.script_map.is_empty() {
        let mut linked_count = 0usize;
        for page in &pages {
            let page_rel_paths = if let Some(bundle_id) = page_bundle_ids.get(&page.path) {
                let rel_paths = bundle_result.script_map.get(bundle_id).ok_or_else(|| {
                    Error::Build(format!(
                        "Missing bundled script for page {} entry {}",
                        page.path, bundle_id
                    ))
                })?;
                if rel_paths.is_empty() {
                    None
                } else {
                    Some(rel_paths)
                }
            } else {
                None
            };
            if bundle_result.root_script.is_none() && page_rel_paths.is_none() {
                continue;
            }
            let page_dir = site_dir.join(page.path.strip_prefix(base_path).unwrap_or(&page.path));
            let target = page_dir.join("index.html");
            let mut html = fs::read_to_string(&target)
                .map_err(|e| Error::Io(format!("Cannot read {}: {e}", target.display())))?;

            if let Some(rel_path) = bundle_result.root_script.as_ref() {
                let tag = module_script_tag(base_path, rel_path);
                inject_script_tag(&mut html, &tag);
                linked_count += 1;
            }
            if let Some(rel_paths) = page_rel_paths {
                linked_count += inject_module_script_tags(&mut html, base_path, rel_paths);
            }

            fs::write(&target, &html)
                .map_err(|e| Error::Io(format!("Cannot rewrite {}: {e}", page.path)))?;
        }
        if let Some(bundle_id) = not_found_bundle_id {
            let rel_paths = bundle_result.script_map.get(&bundle_id).ok_or_else(|| {
                Error::Build(format!(
                    "Missing bundled script for 404 page entry {bundle_id}"
                ))
            })?;
            let target = site_dir.join("404.html");
            let mut html = fs::read_to_string(&target)
                .map_err(|e| Error::Io(format!("Cannot read {}: {e}", target.display())))?;
            if let Some(rel_path) = bundle_result.root_script.as_ref() {
                let tag = module_script_tag(base_path, rel_path);
                inject_script_tag(&mut html, &tag);
                linked_count += 1;
            }
            linked_count += inject_module_script_tags(&mut html, base_path, rel_paths);
            fs::write(&target, &html)
                .map_err(|e| Error::Io(format!("Cannot rewrite 404.html: {e}")))?;
        } else if let Some(rel_path) = bundle_result.root_script.as_ref() {
            let target = site_dir.join("404.html");
            let mut html = fs::read_to_string(&target)
                .map_err(|e| Error::Io(format!("Cannot read {}: {e}", target.display())))?;
            let tag = module_script_tag(base_path, rel_path);
            inject_script_tag(&mut html, &tag);
            fs::write(&target, &html)
                .map_err(|e| Error::Io(format!("Cannot rewrite 404.html: {e}")))?;
            linked_count += 1;
        }
        if linked_count > 0 {
            print_success(
                cache,
                &format!(
                    "Linked {} script tag{}",
                    linked_count,
                    if linked_count == 1 { "" } else { "s" }
                ),
            );
        }
    }

    let elapsed = start.elapsed();
    let total_bytes = total_bytes.load(std::sync::atomic::Ordering::Relaxed);
    print_finish(cache, &format!("Built in {:.1}s", elapsed.as_secs_f64()));

    Ok(BuildStats {
        pages: pages.len(),
        protocol_bytes: total_bytes,
    })
}

/// Recursively remove a directory, retrying briefly on `ENOTEMPTY`.
///
/// On macOS (and occasionally Linux), `fs::remove_dir_all` can fail
/// with `Directory not empty` when something writes a file into a
/// subdirectory between the moment we walked it and the moment we try
/// to `rmdir` it. Likely culprits during a dev-server rebuild:
///
///  - Spotlight / `mds` writing index hints,
///  - Finder dropping a `.DS_Store`,
///  - the user's editor reindexing the project,
///  - a back-to-back save firing another rebuild before the first one
///    finished cleanup (the worker coalesces, but a stray write into
///    `out_dir` from a parallel `cargo` or another tool is still
///    possible).
///
/// We retry up to a handful of times with short backoff. After that the
/// error propagates so the user sees a real failure rather than an
/// infinite loop.
fn remove_dir_all_retry(path: &Path) -> std::io::Result<()> {
    const MAX_ATTEMPTS: u32 = 5;
    let mut delay = std::time::Duration::from_millis(20);
    for attempt in 1..=MAX_ATTEMPTS {
        match fs::remove_dir_all(path) {
            Ok(()) => return Ok(()),
            Err(e) if attempt < MAX_ATTEMPTS && is_dir_not_empty(&e) => {
                std::thread::sleep(delay);
                delay = delay.saturating_mul(2);
            }
            Err(e) => return Err(e),
        }
    }
    // Loop guarantees a return on each iteration; this is unreachable
    // but `for` returning `()` makes the type-checker happy without it.
    Ok(())
}

/// Returns true if the error is `ENOTEMPTY` / `EEXIST` style "directory
/// not empty". On macOS this is errno 66; on Linux it's 39. We also
/// match by `ErrorKind` once Rust exposes one (currently `Other`).
fn is_dir_not_empty(e: &std::io::Error) -> bool {
    match e.raw_os_error() {
        Some(66) => true,  // macOS / BSD: ENOTEMPTY
        Some(39) => true,  // Linux: ENOTEMPTY
        Some(145) => true, // Windows: ERROR_DIR_NOT_EMPTY
        _ => false,
    }
}

/// Iteratively copy a directory tree (BFS via stack — no recursion).
fn copy_dir(src: &Path, dest: &Path) -> Result<()> {
    if !src.exists() {
        return Ok(());
    }
    fs::create_dir_all(dest).map_err(|e| Error::Io(e.to_string()))?;
    let mut stack: Vec<(std::path::PathBuf, std::path::PathBuf)> =
        vec![(src.to_path_buf(), dest.to_path_buf())];
    while let Some((s, d)) = stack.pop() {
        for entry in fs::read_dir(&s).map_err(|e| Error::Io(e.to_string()))? {
            let entry = entry.map_err(|e| Error::Io(e.to_string()))?;
            let dest_path = d.join(entry.file_name());
            let ft = entry.file_type().map_err(|e| Error::Io(e.to_string()))?;
            if ft.is_dir() {
                fs::create_dir_all(&dest_path).map_err(|e| Error::Io(e.to_string()))?;
                stack.push((entry.path(), dest_path));
            } else {
                fs::copy(entry.path(), &dest_path).map_err(|e| Error::Io(e.to_string()))?;
            }
        }
    }
    Ok(())
}

/// Truncate a string to at most `max` bytes at a valid UTF-8 boundary.
pub(crate) fn truncate_utf8(s: &str, max: usize) -> &str {
    if s.len() <= max {
        return s;
    }
    let mut end = max;
    while end > 0 && !s.is_char_boundary(end) {
        end -= 1;
    }
    &s[..end]
}

#[derive(Default)]
struct SearchText {
    content: String,
    headings: Vec<SearchHeading>,
}

/// Heading metadata emitted into `search-index.json`.
///
/// The client search UI uses this to create separate, anchor-linked rows for
/// matching sections (`Page > Heading`) without re-parsing page HTML at runtime.
struct SearchHeading {
    text: String,
    anchor: String,
    level: u8,
}

/// Mutable heading capture state while scanning rendered HTML.
struct CurrentHeading {
    text: String,
    anchor: String,
    level: u8,
}

struct HtmlTag<'a> {
    name: &'a str,
    is_end: bool,
}

/// Build a single search-index entry for a page. Strips HTML tags,
/// decodes escaped text, captures headings for weighted ranking,
/// collapses whitespace, and truncates body content to 500 bytes.
pub(crate) fn build_search_entry(title: &str, path: &str, html: &str) -> Value {
    let search_text = extract_search_text(html);
    let content = normalize_search_text(&search_text.content);
    let truncated = truncate_utf8(&content, 500);
    let headings: Vec<Value> = search_text
        .headings
        .into_iter()
        .map(|heading| {
            json_obj([
                ("text", Value::String(heading.text)),
                ("anchor", Value::String(heading.anchor)),
                (
                    "level",
                    Value::Number(serde_json::Number::from(u64::from(heading.level))),
                ),
            ])
        })
        .collect();
    json_obj([
        ("title", Value::String(title.to_string())),
        ("path", Value::String(path.to_string())),
        ("content", Value::String(truncated.to_string())),
        ("headings", Value::Array(headings)),
    ])
}

fn extract_search_text(html: &str) -> SearchText {
    let mut text = SearchText {
        content: String::with_capacity(html.len() / 2),
        headings: Vec::with_capacity(8),
    };
    let mut current_heading: Option<CurrentHeading> = None;
    let mut skip_header_anchor = 0usize;
    let mut cursor = 0;

    while cursor < html.len() {
        let remaining = &html[cursor..];
        let Some(tag_offset) = remaining.find('<') else {
            push_search_text(
                &mut text,
                current_heading.as_mut(),
                skip_header_anchor,
                remaining,
            );
            break;
        };
        let text_end = cursor + tag_offset;
        push_search_text(
            &mut text,
            current_heading.as_mut(),
            skip_header_anchor,
            &html[cursor..text_end],
        );
        let tag_start = text_end + 1;
        let Some(tag_len) = html[tag_start..].find('>') else {
            push_search_text(
                &mut text,
                current_heading.as_mut(),
                skip_header_anchor,
                &html[text_end..],
            );
            break;
        };
        let tag_end = tag_start + tag_len;
        handle_search_tag(
            &html[tag_start..tag_end],
            &mut text,
            &mut current_heading,
            &mut skip_header_anchor,
        );
        cursor = tag_end + 1;
    }

    text
}

fn handle_search_tag(
    raw_tag: &str,
    text: &mut SearchText,
    current_heading: &mut Option<CurrentHeading>,
    skip_header_anchor: &mut usize,
) {
    if let Some(tag) = parse_html_tag(raw_tag) {
        if tag.is_end {
            handle_end_tag(tag.name, text, current_heading, skip_header_anchor);
        } else {
            handle_start_tag(tag.name, raw_tag, current_heading, skip_header_anchor);
        }
    }
    push_search_space(text, current_heading.as_mut(), *skip_header_anchor);
}

fn handle_start_tag(
    name: &str,
    raw_tag: &str,
    current_heading: &mut Option<CurrentHeading>,
    skip_header_anchor: &mut usize,
) {
    if let Some(level) = heading_level(name) {
        *current_heading = Some(CurrentHeading {
            text: String::with_capacity(64),
            anchor: attr_value(raw_tag, "id").unwrap_or("").to_string(),
            level,
        });
    } else if name.eq_ignore_ascii_case("a") && raw_tag.contains("header-anchor") {
        *skip_header_anchor += 1;
    }
}

fn handle_end_tag(
    name: &str,
    text: &mut SearchText,
    current_heading: &mut Option<CurrentHeading>,
    skip_header_anchor: &mut usize,
) {
    if name.eq_ignore_ascii_case("a") && *skip_header_anchor > 0 {
        *skip_header_anchor -= 1;
    }
    if heading_level(name).is_some() {
        if let Some(raw_heading) = current_heading.take() {
            let heading_text = normalize_search_text(&raw_heading.text);
            if !heading_text.is_empty() {
                text.headings.push(SearchHeading {
                    text: heading_text,
                    anchor: raw_heading.anchor,
                    level: raw_heading.level,
                });
            }
        }
    }
}

fn parse_html_tag(raw: &str) -> Option<HtmlTag<'_>> {
    let mut tag = raw.trim_start();
    if tag.starts_with('!') || tag.starts_with('?') {
        return None;
    }
    let is_end = tag.starts_with('/');
    if is_end {
        tag = tag[1..].trim_start();
    }

    let mut name_end = 0;
    for (idx, byte) in tag.bytes().enumerate() {
        if byte.is_ascii_alphanumeric() || byte == b'-' {
            name_end = idx + 1;
        } else {
            break;
        }
    }
    if name_end == 0 {
        return None;
    }
    Some(HtmlTag {
        name: &tag[..name_end],
        is_end,
    })
}

/// Return a heading level for `h1` through `h6`.
///
/// The scanner works on tag names from already-rendered markdown, so an ASCII
/// byte check is sufficient and avoids allocating a lowercased copy.
fn heading_level(name: &str) -> Option<u8> {
    let bytes = name.as_bytes();
    if bytes.len() == 2 && bytes[0].eq_ignore_ascii_case(&b'h') && (b'1'..=b'6').contains(&bytes[1])
    {
        Some(bytes[1] - b'0')
    } else {
        None
    }
}

/// Extract an HTML attribute value from a rendered start tag without a DOM
/// parser.
///
/// Search indexing runs once per page at build time, but it still avoids regex
/// and allocation-heavy parsing. This helper handles quoted and unquoted values
/// because heading IDs are generated as ordinary HTML attributes.
fn attr_value<'a>(raw_tag: &'a str, attr_name: &str) -> Option<&'a str> {
    let bytes = raw_tag.as_bytes();
    let mut cursor = 0;

    while cursor < bytes.len() {
        while bytes.get(cursor).is_some_and(u8::is_ascii_whitespace) {
            cursor += 1;
        }

        let name_start = cursor;
        while bytes.get(cursor).is_some_and(|b| is_attr_name_byte(*b)) {
            cursor += 1;
        }
        if name_start == cursor {
            cursor += 1;
            continue;
        }
        let name = &raw_tag[name_start..cursor];

        while bytes.get(cursor).is_some_and(u8::is_ascii_whitespace) {
            cursor += 1;
        }
        if bytes.get(cursor) != Some(&b'=') {
            continue;
        }
        cursor += 1;
        while bytes.get(cursor).is_some_and(u8::is_ascii_whitespace) {
            cursor += 1;
        }

        let (value_start, value_end) = attr_value_range(raw_tag, cursor);
        if name.eq_ignore_ascii_case(attr_name) {
            return Some(&raw_tag[value_start..value_end]);
        }
        cursor = value_end.saturating_add(1);
    }

    None
}

fn is_attr_name_byte(byte: u8) -> bool {
    byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b':')
}

fn attr_value_range(raw_tag: &str, cursor: usize) -> (usize, usize) {
    let bytes = raw_tag.as_bytes();
    match bytes.get(cursor).copied() {
        Some(b'"' | b'\'') => {
            let quote = bytes[cursor];
            let value_start = cursor + 1;
            let value_end = raw_tag[value_start..]
                .find(char::from(quote))
                .map_or(raw_tag.len(), |offset| value_start + offset);
            (value_start, value_end)
        }
        Some(_) => {
            let value_start = cursor;
            let value_end = raw_tag[value_start..]
                .find(char::is_whitespace)
                .map_or(raw_tag.len(), |offset| value_start + offset);
            (value_start, value_end)
        }
        None => (raw_tag.len(), raw_tag.len()),
    }
}

fn push_search_text(
    text: &mut SearchText,
    current_heading: Option<&mut CurrentHeading>,
    skip_header_anchor: usize,
    value: &str,
) {
    if skip_header_anchor > 0 {
        return;
    }
    text.content.push_str(value);
    if let Some(heading) = current_heading {
        heading.text.push_str(value);
    }
}

fn push_search_space(
    text: &mut SearchText,
    current_heading: Option<&mut CurrentHeading>,
    skip_header_anchor: usize,
) {
    if skip_header_anchor > 0 {
        return;
    }
    text.content.push(' ');
    if let Some(heading) = current_heading {
        heading.text.push(' ');
    }
}

fn normalize_search_text(raw: &str) -> String {
    let decoded = html_escape::decode_html_entities(raw);
    let mut normalized = String::with_capacity(decoded.len());
    let mut first = true;
    for token in decoded.split_whitespace() {
        if !first {
            normalized.push(' ');
        }
        normalized.push_str(token);
        first = false;
    }
    normalized
}

const PRE_BLOCK_MARKER_PREFIX: &str = "<span data-webui-press-pre-block=\"";
const PRE_BLOCK_MARKER_SUFFIX: &str = "\"></span>";

/// Replace `<pre …>…</pre>` blocks with placeholder elements so the WebUI
/// HTML parser does not normalize whitespace inside them. Returns the
/// modified string and the original blocks (in order) for restoration
/// after rendering.
fn protect_pre_blocks(content: &str) -> (String, Vec<String>) {
    use std::fmt::Write as _;
    let mut blocks: Vec<String> = Vec::new();
    let mut out = String::with_capacity(content.len());
    let mut cursor = 0;
    while let Some(rel_start) = find_pre_open(&content[cursor..]) {
        let start = cursor + rel_start;
        if let Some(rel_end) = content[start..].find("</pre>") {
            let end = start + rel_end + "</pre>".len();
            out.push_str(&content[cursor..start]);
            out.push_str(PRE_BLOCK_MARKER_PREFIX);
            // write! into existing buffer — avoids `format!` allocation per block.
            let _ = write!(&mut out, "{}", blocks.len());
            out.push_str(PRE_BLOCK_MARKER_SUFFIX);
            blocks.push(content[start..end].to_string());
            cursor = end;
        } else {
            break;
        }
    }
    out.push_str(&content[cursor..]);
    (out, blocks)
}

/// Find the next opening `<pre` tag where the next byte is one of `>`, ` `,
/// `\t`, `\n`, or `\r`. Avoids matching `<presentation`, `<pretend`, etc.
fn find_pre_open(s: &str) -> Option<usize> {
    let bytes = s.as_bytes();
    let mut i = 0;
    while let Some(rel) = s[i..].find("<pre") {
        let pos = i + rel;
        let after = pos + 4;
        match bytes.get(after) {
            Some(b'>' | b' ' | b'\t' | b'\n' | b'\r') => return Some(pos),
            _ => i = after,
        }
    }
    None
}

/// Single-pass restoration of pre-block placeholder elements to their
/// original content. Faster than calling `String::replace` once per block.
fn restore_pre_blocks(html: &str, blocks: &[String]) -> String {
    if blocks.is_empty() {
        return html.to_string();
    }
    let extra: usize = blocks.iter().map(|b| b.len()).sum();
    let mut out = String::with_capacity(html.len() + extra);
    let mut cursor = 0;
    while let Some(rel) = html[cursor..].find(PRE_BLOCK_MARKER_PREFIX) {
        let p = cursor + rel;
        out.push_str(&html[cursor..p]);
        let after = p + PRE_BLOCK_MARKER_PREFIX.len();
        if let Some(end_rel) = html[after..].find(PRE_BLOCK_MARKER_SUFFIX) {
            let num_str = &html[after..after + end_rel];
            if let Ok(idx) = num_str.parse::<usize>() {
                if let Some(block) = blocks.get(idx) {
                    out.push_str(block);
                    cursor = after + end_rel + PRE_BLOCK_MARKER_SUFFIX.len();
                    continue;
                }
            }
            // Unknown placeholder — keep verbatim.
            out.push_str(&html[p..after + end_rel + PRE_BLOCK_MARKER_SUFFIX.len()]);
            cursor = after + end_rel + PRE_BLOCK_MARKER_SUFFIX.len();
        } else {
            out.push_str(&html[p..]);
            return out;
        }
    }
    out.push_str(&html[cursor..]);
    out
}

/// Cheap deterministic hash for building unique temp directory names.
fn fxhash(s: &str) -> u64 {
    fxhash_bytes(s.as_bytes())
}

fn fxhash_bytes(bytes: &[u8]) -> u64 {
    let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
    for &b in bytes {
        hash ^= u64::from(b);
        hash = hash.wrapping_mul(0x100_0000_01b3);
    }
    hash
}

#[cfg(test)]
mod tests {
    use super::*;

    type TestResult<T = ()> = std::result::Result<T, Box<dyn std::error::Error>>;

    fn test_obj<const N: usize>(entries: [(&str, Value); N]) -> Value {
        let mut map = Map::with_capacity(N);
        for (key, value) in entries {
            map.insert(key.to_string(), value);
        }
        Value::Object(map)
    }

    fn string_value(value: &str) -> Value {
        Value::String(value.to_string())
    }

    fn docs_config() -> TestResult<DocsConfig> {
        let config = serde_json::from_str(
            r#"{
                "site": { "title": "Docs" },
                "basePath": "/",
                "contentDir": ".",
                "nav": [],
                "sidebar": []
            }"#,
        )?;
        Ok(config)
    }

    #[test]
    fn not_found_state_includes_global_state() -> TestResult {
        let config = docs_config()?;
        let global = test_obj([("release", test_obj([("version", string_value("v1.2.3"))]))]);

        let state = build_not_found_state(NotFoundStateInput {
            config: &config,
            base_path: "/",
            nav: Value::Array(vec![]),
            footer: Value::Null,
            content: "<h1>404</h1>",
            head_injection: "<meta name=\"docs\">",
            global_state: Some(&global),
        });

        assert_eq!(state["release"]["version"], "v1.2.3");
        assert_eq!(state["page"]["title"], "Page Not Found");
        assert_eq!(state["pageData"], Value::Null);
        assert_eq!(state["headTags"], "<meta name=\"docs\">");
        Ok(())
    }

    // --- truncate_utf8 ---------------------------------------------------

    #[test]
    fn truncate_utf8_short_string_unchanged() {
        assert_eq!(truncate_utf8("hello", 100), "hello");
    }

    #[test]
    fn truncate_utf8_ascii_at_exact_boundary() {
        assert_eq!(truncate_utf8("hello world", 5), "hello");
    }

    #[test]
    fn truncate_utf8_steps_back_off_multibyte_boundary() {
        // "é" is two bytes (0xC3 0xA9). Cutting at 1 byte must step back to 0.
        let s = "é-suffix";
        let out = truncate_utf8(s, 1);
        assert!(s.is_char_boundary(out.len()), "out.len()={}", out.len());
        assert_eq!(out, "");
    }

    #[test]
    fn truncate_utf8_keeps_multibyte_when_room_allows() {
        let s = "café";
        let out = truncate_utf8(s, 5); // "café" is 5 bytes total
        assert_eq!(out, "café");
    }

    // --- protect_pre_blocks / find_pre_open ------------------------------

    #[test]
    fn protect_pre_blocks_with_attrs() {
        let input = r#"<p>before</p><pre class="hljs">code</pre><p>after</p>"#;
        let (out, blocks) = protect_pre_blocks(input);
        assert_eq!(blocks.len(), 1);
        assert!(out.contains(r#"<span data-webui-press-pre-block="0"></span>"#));
        assert!(!out.contains("<pre"));
        assert_eq!(blocks[0], r#"<pre class="hljs">code</pre>"#);
    }

    #[test]
    fn protect_pre_blocks_bare_open_tag() {
        // Was previously missed because we only matched "<pre " (with space).
        let input = "<pre>code</pre>";
        let (out, blocks) = protect_pre_blocks(input);
        assert_eq!(blocks.len(), 1, "got out={out:?} blocks={blocks:?}");
        assert_eq!(blocks[0], "<pre>code</pre>");
    }

    #[test]
    fn protect_pre_blocks_does_not_match_presentation_tag() {
        // <presentation> must NOT be treated as a pre block.
        let input = "<presentation>x</presentation>";
        let (_out, blocks) = protect_pre_blocks(input);
        assert_eq!(blocks.len(), 0);
    }

    #[test]
    fn protect_then_restore_round_trip() {
        let input = "before <pre>a</pre> mid <pre class=\"x\">b</pre> end";
        let (protected, blocks) = protect_pre_blocks(input);
        let restored = restore_pre_blocks(&protected, &blocks);
        assert_eq!(restored, input);
    }

    #[test]
    fn restore_pre_blocks_no_blocks_returns_input() {
        assert_eq!(restore_pre_blocks("plain html", &[]), "plain html");
    }

    #[test]
    fn pre_block_placeholder_survives_webui_component_slot_render() -> TestResult {
        let input = "<code-block><pre><code>let x = 1;\n</code></pre></code-block>";
        let (protected, blocks) = protect_pre_blocks(input);
        let page_html = format!("<!DOCTYPE html><html><body>{protected}</body></html>");

        let tmp = std::env::temp_dir().join(format!(
            "webui-press-slot-test-{}-{:x}",
            std::process::id(),
            fxhash(input)
        ));
        if tmp.exists() {
            fs::remove_dir_all(&tmp)?;
        }
        fs::create_dir_all(&tmp)?;
        fs::write(tmp.join("index.html"), page_html)?;

        let components = vec![Path::new(env!("CARGO_MANIFEST_DIR"))
            .join("components")
            .to_string_lossy()
            .into_owned()];
        let build_result = webui::build(BuildOptions {
            app_dir: tmp.clone(),
            entry: "index.html".to_string(),
            plugin: Some(webui::Plugin::WebUI),
            components,
            ..BuildOptions::default()
        })?;

        let mut writer = StringWriter::with_capacity(4096);
        let handler = WebUIHandler::with_plugin(|| {
            Box::new(webui_handler::plugin::webui::WebUIHydrationPlugin::new())
        });
        let protocol = Protocol::new(build_result.protocol);
        handler.render(
            &protocol,
            &Value::Object(Map::new()),
            &RenderOptions::new("index.html", "/"),
            &mut writer,
        )?;

        fs::remove_dir_all(&tmp)?;

        let html = restore_pre_blocks(&writer.buf, &blocks);
        assert!(
            html.contains("<pre><code>let x = 1;\n</code></pre>"),
            "slotted pre block should be restored after WebUI render: {html}"
        );
        assert!(
            !html.contains(PRE_BLOCK_MARKER_PREFIX),
            "placeholder marker must not survive output: {html}"
        );

        Ok(())
    }

    #[test]
    fn build_search_entry_decodes_code_text_and_captures_headings() {
        let entry = build_search_entry(
            "`<for>` Loop Directive",
            "/guide/concepts/directives/for/",
            r##"<h1 id="for"><code>&lt;for&gt;</code> Loop Directive <a class="header-anchor" href="#for">#</a></h1><p>Use <code>&lt;for&gt;</code> loops.</p><h2 id="syntax">Syntax <a class="header-anchor" href="#syntax">#</a></h2><h3 id="nested">Nested Search <a class="header-anchor" href="#nested">#</a></h3>"##,
        );

        assert_eq!(entry["title"].as_str(), Some("`<for>` Loop Directive"));
        assert_eq!(
            entry["content"].as_str(),
            Some("<for> Loop Directive Use <for> loops. Syntax Nested Search")
        );
        let Some(headings) = entry["headings"].as_array() else {
            panic!("headings should be an array: {entry:?}");
        };
        assert_eq!(headings.len(), 3);
        assert_eq!(headings[0]["text"].as_str(), Some("<for> Loop Directive"));
        assert_eq!(headings[0]["anchor"].as_str(), Some("for"));
        assert_eq!(headings[0]["level"].as_u64(), Some(1));
        assert_eq!(headings[1]["text"].as_str(), Some("Syntax"));
        assert_eq!(headings[1]["anchor"].as_str(), Some("syntax"));
        assert_eq!(headings[1]["level"].as_u64(), Some(2));
        assert_eq!(headings[2]["text"].as_str(), Some("Nested Search"));
        assert_eq!(headings[2]["anchor"].as_str(), Some("nested"));
        assert_eq!(headings[2]["level"].as_u64(), Some(3));
    }

    // --- fxhash ----------------------------------------------------------

    #[test]
    fn fxhash_deterministic() {
        assert_eq!(fxhash("abc"), fxhash("abc"));
        assert_ne!(fxhash("abc"), fxhash("abd"));
    }
}