microsoft-webui-press 0.0.25

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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

//! Shared state loading and merge policy for WebUI Press builds.

use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};

use serde_json::Value;

use crate::error::{Error, Result};
use crate::types::DocsConfig;

/// Loaded render state inputs shared by all generated pages.
#[derive(Debug, Default)]
pub(crate) struct LoadedStates {
    global: Option<Value>,
    custom_pages: HashMap<String, Value>,
}

impl LoadedStates {
    pub(crate) fn global(&self) -> Option<&Value> {
        self.global.as_ref()
    }

    pub(crate) fn custom_page_state(&self, logical_path: &str) -> Option<&Value> {
        self.custom_pages
            .get(logical_path)
            .or_else(|| self.custom_pages.get(logical_path.trim_end_matches('/')))
            .or_else(|| {
                if logical_path.ends_with('/') {
                    None
                } else {
                    let mut with_slash = String::with_capacity(logical_path.len() + 1);
                    with_slash.push_str(logical_path);
                    with_slash.push('/');
                    self.custom_pages.get(with_slash.as_str())
                }
            })
    }
}

/// Top-level state keys reserved by the docs renderer. Global and custom-page
/// state objects whose top-level fields are flattened onto the page state must
/// not shadow these names so the canonical docs state always wins.
const RESERVED_STATE_KEYS: &[&str] = &[
    "site",
    "navigation",
    "sidebar",
    "page",
    "hero",
    "footer",
    "prev",
    "next",
    "pageData",
    "headTags",
    "tokens",
    "label",
    "icon",
];

struct StateLoader {
    cache: HashMap<PathBuf, Value>,
}

impl StateLoader {
    fn new() -> Self {
        Self {
            cache: HashMap::new(),
        }
    }

    fn load_state_value(
        &mut self,
        label: &str,
        inline: Option<&Value>,
        path: Option<&str>,
        config_dir: &Path,
    ) -> Result<Option<Value>> {
        if inline.is_some() && path.is_some() {
            return Err(Error::Build(format!(
                "{label}: 'state' and 'stateFile' are mutually exclusive - pick one."
            )));
        }

        if let Some(value) = inline {
            return Ok(Some(value.clone()));
        }

        let Some(rel) = path else {
            return Ok(None);
        };

        let rel_path = Path::new(rel);
        if rel_path.is_absolute() {
            return Err(Error::Build(format!(
                "{label}: stateFile must be a path relative to config.json, got absolute path {}",
                rel_path.display()
            )));
        }

        let abs = config_dir.join(rel_path);
        let key = fs::canonicalize(&abs).unwrap_or_else(|_| abs.clone());
        if let Some(cached) = self.cache.get(&key) {
            return Ok(Some(cached.clone()));
        }

        let raw = fs::read_to_string(&abs).map_err(|e| {
            Error::Build(format!(
                "{label}: cannot read stateFile {}: {e}",
                abs.display()
            ))
        })?;
        let parsed: Value = serde_json::from_str(&raw).map_err(|e| {
            Error::Build(format!(
                "{label}: stateFile {} is not valid JSON: {e}",
                abs.display()
            ))
        })?;
        self.cache.insert(key, parsed.clone());
        Ok(Some(parsed))
    }

    fn load_global_state(
        &mut self,
        config: &DocsConfig,
        config_dir: &Path,
    ) -> Result<Option<Value>> {
        let Some(value) = self.load_state_value(
            "Global state",
            config.state.as_ref(),
            config.state_file.as_deref(),
            config_dir,
        )?
        else {
            return Ok(None);
        };

        if !value.is_object() {
            return Err(Error::Build(
                "Global state: state/stateFile must be a JSON object.".to_string(),
            ));
        }

        Ok(Some(value))
    }

    fn load_custom_page_states(
        &mut self,
        config: &DocsConfig,
        config_dir: &Path,
    ) -> Result<HashMap<String, Value>> {
        let mut out: HashMap<String, Value> = HashMap::with_capacity(config.custom_pages.len());

        for (link, page) in &config.custom_pages {
            if let Some(value) = self.load_state_value(
                &format!("Custom page {link}"),
                page.inline_state(),
                page.state_file(),
                config_dir,
            )? {
                out.insert(link.clone(), value);
            }
        }

        Ok(out)
    }
}

pub(crate) fn load_render_states(config: &DocsConfig, config_dir: &Path) -> Result<LoadedStates> {
    let mut loader = StateLoader::new();
    let global = loader.load_global_state(config, config_dir)?;
    let custom_pages = loader.load_custom_page_states(config, config_dir)?;
    Ok(LoadedStates {
        global,
        custom_pages,
    })
}

/// Merge docs state with global state first and custom-page state second.
///
/// Global state fills missing non-reserved top-level keys. Custom-page state is
/// applied afterward and may replace global keys, but reserved docs keys are
/// never replaced.
pub(crate) fn merge_page_state(
    mut state: Value,
    global: Option<&Value>,
    custom_page: Option<&Value>,
) -> Value {
    merge_top_level_state(&mut state, global, false);
    merge_top_level_state(&mut state, custom_page, true);
    state
}

fn merge_top_level_state(state: &mut Value, extra: Option<&Value>, overwrite_existing: bool) {
    let Some(extra) = extra.and_then(Value::as_object) else {
        return;
    };

    let Value::Object(map) = state else {
        return;
    };

    for (key, value) in extra {
        if RESERVED_STATE_KEYS.contains(&key.as_str()) {
            continue;
        }
        if overwrite_existing || !map.contains_key(key) {
            map.insert(key.clone(), value.clone());
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::{CustomPage, SiteConfig};
    use serde_json::Map;
    use std::sync::atomic::{AtomicU64, Ordering};

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

    static TEMP_COUNTER: AtomicU64 = AtomicU64::new(0);

    fn empty_config() -> DocsConfig {
        DocsConfig {
            site: SiteConfig {
                title: "Docs".to_string(),
                description: String::new(),
            },
            base_path: "/".to_string(),
            content_dir: ".".to_string(),
            out_dir: "./dist".to_string(),
            public_dir: "./public".to_string(),
            css: None,
            theme: None,
            components: None,
            head: Vec::new(),
            nav: Vec::new(),
            sidebar: Vec::new(),
            sidebar_groups: std::collections::BTreeMap::new(),
            custom_pages: HashMap::new(),
            state: None,
            state_file: None,
            hero: None,
            footer: None,
            bundler: None,
        }
    }

    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 temp_config_dir(name: &str) -> TestResult<PathBuf> {
        let id = TEMP_COUNTER.fetch_add(1, Ordering::Relaxed);
        let dir =
            std::env::temp_dir().join(format!("webui-press-{name}-{}-{id}", std::process::id()));
        fs::create_dir_all(&dir)?;
        Ok(dir)
    }

    #[test]
    fn load_render_states_reads_global_state_file_object() -> TestResult {
        let dir = temp_config_dir("global-state-file")?;
        fs::create_dir_all(dir.join("state"))?;
        fs::write(
            dir.join("state/site.json"),
            r#"{ "release": { "version": "v1.2.3" } }"#,
        )?;

        let mut config = empty_config();
        config.state_file = Some("./state/site.json".to_string());

        let states = load_render_states(&config, &dir)?;
        let state = states.global().ok_or_else(|| {
            std::io::Error::new(std::io::ErrorKind::NotFound, "missing global state")
        })?;
        assert_eq!(state["release"]["version"], "v1.2.3");

        fs::remove_dir_all(dir).ok();
        Ok(())
    }

    #[test]
    fn load_render_states_rejects_global_state_and_state_file_together() {
        let mut config = empty_config();
        config.state = Some(test_obj([(
            "release",
            test_obj([("version", string_value("v1.2.3"))]),
        )]));
        config.state_file = Some("./state/site.json".to_string());

        let error = load_render_states(&config, Path::new(".")).err();
        let Some(error) = error else {
            panic!("expected mutually exclusive state error");
        };
        assert!(
            error
                .to_string()
                .contains("'state' and 'stateFile' are mutually exclusive"),
            "{error}"
        );
    }

    #[test]
    fn load_render_states_requires_global_json_object() {
        let mut config = empty_config();
        config.state = Some(Value::Array(vec![
            string_value("not"),
            string_value("object"),
        ]));

        let error = load_render_states(&config, Path::new(".")).err();
        let Some(error) = error else {
            panic!("expected object validation error");
        };
        assert!(
            error
                .to_string()
                .contains("state/stateFile must be a JSON object"),
            "{error}"
        );
    }

    #[test]
    fn load_render_states_reads_custom_page_state_file() -> TestResult {
        let dir = temp_config_dir("custom-state-file")?;
        fs::create_dir_all(dir.join("state"))?;
        fs::write(
            dir.join("state/playground.json"),
            r#"{ "files": ["a.rs"] }"#,
        )?;

        let mut config = empty_config();
        config.custom_pages.insert(
            "/playground/".to_string(),
            CustomPage::Full {
                html: "<docs-playground></docs-playground>".to_string(),
                layout: Some("full".to_string()),
                state: None,
                state_file: Some("./state/playground.json".to_string()),
                script_file: None,
            },
        );

        let states = load_render_states(&config, &dir)?;
        let state = states.custom_page_state("/playground").ok_or_else(|| {
            std::io::Error::new(std::io::ErrorKind::NotFound, "missing custom state")
        })?;
        assert_eq!(state["files"][0], "a.rs");

        fs::remove_dir_all(dir).ok();
        Ok(())
    }

    #[test]
    fn merge_page_state_applies_global_then_custom_page_state() {
        let base = test_obj([
            ("site", test_obj([("title", string_value("Docs"))])),
            ("headTags", string_value("<meta name=\"docs\">")),
        ]);
        let global = test_obj([
            ("release", test_obj([("version", string_value("global"))])),
            ("shared", string_value("global")),
        ]);
        let custom = test_obj([
            ("release", test_obj([("version", string_value("custom"))])),
            ("local", string_value("custom")),
        ]);

        let merged = merge_page_state(base, Some(&global), Some(&custom));
        assert_eq!(merged["release"]["version"], "custom");
        assert_eq!(merged["shared"], "global");
        assert_eq!(merged["local"], "custom");
    }

    #[test]
    fn merge_page_state_keeps_reserved_docs_keys() {
        let base = test_obj([
            ("site", test_obj([("title", string_value("Docs"))])),
            ("headTags", string_value("<meta name=\"docs\">")),
        ]);
        let custom = test_obj([
            ("site", test_obj([("title", string_value("Override"))])),
            ("headTags", string_value("<script>bad()</script>")),
            ("tokens", string_value("not-token-css")),
        ]);

        let merged = merge_page_state(base, None, Some(&custom));
        assert_eq!(merged["site"]["title"], "Docs");
        assert_eq!(merged["headTags"], "<meta name=\"docs\">");
        assert_eq!(merged.get("tokens"), None);
    }
}