moss-core 0.1.0

Pure-Rust content engine for moss: AST, render, resolve, validate, frontmatter, schema.
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
//! Typed shortcode AST nodes.
//!
//! Each shortcode is a closed enum variant with fully-typed arguments.
//! Variants land per-shortcode in Phase B (one variant per migration
//! commit) of the typed-AST migration.
//!
//! Migration order (Phase B): Subscribe, Buttons, Gallery, Hero, Grid, Recent.

use serde::{Deserialize, Serialize};

use super::node::Block;
use super::url::Url;

/// A typed shortcode block.
///
/// Variants:
/// - [`Shortcode::Subscribe`] — inline subscribe form (description + button)
/// - [`Shortcode::Buttons`] — list of action buttons with markdown links
/// - [`Shortcode::Gallery`] — image gallery with optional column count
/// - [`Shortcode::Hero`] — full-width hero section with media + overlay
/// - [`Shortcode::Grid`] — flexible multi-cell layout
/// - [`Shortcode::Recent`] — recent-posts query with fallback markdown
///
/// Phase B migrations add one variant per commit.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", tag = "kind")]
pub enum Shortcode {
    /// `:::subscribe` — inline newsletter signup form.
    ///
    /// Configuration is via attributes (`placeholder`, `button`); body
    /// must be empty under the unified grammar. Description text and
    /// any framing prose live in the surrounding markdown.
    Subscribe(SubscribeShortcode),
    /// `:::buttons {.classname}` — list of action buttons.
    ///
    /// Body is one markdown link per line (`[text](url)`). The first
    /// button gets the primary class; subsequent buttons get secondary.
    /// Optional `{.classname}` extra classes attach to the wrapping div.
    ///
    /// URLs flow through [`Url::Unresolved`] at parse time;
    /// [`crate::ast::visit::visit_urls_mut`] (or src-tauri's
    /// `apply_typed_shortcodes`) classifies them into [`Url::Resolved`]
    /// before rendering. The resolver-bypass class is closed by
    /// construction: `RenderHooks::render_shortcode` reads `Url::Resolved`,
    /// so a missing visitor is a debug-time crash.
    Buttons(ButtonsShortcode),
    /// `:::gallery N {.classname}` — image gallery with optional columns.
    ///
    /// `N` (positional integer) sets `--gallery-columns` CSS variable.
    /// Body is one image reference per line: `![alt](path)`, bare
    /// `path.jpg`, or `path|attrs` for media attributes (passed through
    /// to the renderer's inline style).
    Gallery(GalleryShortcode),
    /// `:::hero {image=path}` — full-width hero section with media + overlay.
    ///
    /// New grammar: `image` attribute carries the path. Backward-compat:
    /// when `image` is absent, the extractor scans the first non-empty
    /// body line for a media reference (`![[path]]`, `![alt](path)`, or
    /// bare media filename).
    ///
    /// The pipeline hoists the rendered hero HTML into the article
    /// template's hero slot — it does NOT render inline.
    Hero(HeroShortcode),
    /// `:::grid {cols=N}` or `:::grid N` — flexible multi-cell layout.
    ///
    /// Cells are split on `+++` (new grammar) or `---` (legacy moss-releases
    /// backward-compat — Step 3 of #613 rewrites these to `+++`). Each cell
    /// stores its raw markdown source; the renderer is responsible for any
    /// nested-shortcode extraction and markdown processing per cell.
    Grid(GridShortcode),
    /// `:::recent since=... last=... count=...` — list of recent posts
    /// scoped to the page's top-level folder (its scope).
    ///
    /// Body (between the opening and closing `:::`) is reserved for
    /// fallback content rendered when the query returns zero matches.
    /// Empty body means no fallback (the shortcode renders nothing).
    Recent(RecentShortcode),
}

/// Arguments for [`Shortcode::Subscribe`].
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct SubscribeShortcode {
    /// Optional override for the email input's placeholder text.
    pub placeholder: Option<String>,
    /// Optional override for the submit button label.
    pub button: Option<String>,
}

/// Arguments for [`Shortcode::Buttons`].
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct ButtonsShortcode {
    /// Extra CSS classes for the wrapping `<div>` (from `{.foo .bar}`).
    pub classes: String,
    /// Each button's text + URL. The first item renders as primary, the
    /// rest as secondary. Empty list = the shortcode renders nothing.
    pub items: Vec<ButtonItem>,
}

/// One button in a [`ButtonsShortcode`].
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ButtonItem {
    /// Display text inside the `<a>` tag.
    pub text: String,
    /// Click target. Author input as parsed; flows through
    /// [`crate::ast::visit::visit_urls_mut`] before reaching the renderer.
    pub url: Url,
}

/// Arguments for [`Shortcode::Gallery`].
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct GalleryShortcode {
    /// Optional column count for `--gallery-columns` CSS variable.
    pub columns: Option<u32>,
    /// Extra CSS classes for the wrapping `<div>` (from `{.foo .bar}`).
    pub classes: String,
    /// Each gallery image's src + alt + media attrs.
    pub items: Vec<GalleryItem>,
    /// Spec § P9 width attribute: `body | wide | page | screen` (with
    /// `full` aliased to `screen`). `None` means the author did not set
    /// a width — the emitter omits `data-width` so the HTML stays sparse.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub width: Option<String>,
}

/// One image in a [`GalleryShortcode`].
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct GalleryItem {
    /// Image source URL. Flows through resolver before rendering.
    pub src: Url,
    /// Alt text (from `![alt](...)` syntax). Empty if author used bare path.
    pub alt: String,
    /// Pipe-suffix media attributes verbatim (e.g. "cover top",
    /// "1.5:1 contain"). Empty if no pipe in the source.
    /// The renderer parses this via `moss_core::media::parse_media_attrs`.
    pub attrs: String,
}

/// Arguments for [`Shortcode::Grid`].
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct GridShortcode {
    /// Column count. Defaults to 1 when neither positional nor `cols=`
    /// attribute is provided.
    pub columns: u32,
    /// Optional ratio string like `"1:2"` or `"1:1:2"`. When present,
    /// the renderer emits `style="grid-template-columns:1fr 2fr"` etc.
    /// `cols=1:2:3` is equivalent to setting both `columns` (count = 3)
    /// and `ratio` to `"1:2:3"`.
    pub ratio: Option<String>,
    /// Extra CSS classes for the wrapping `<div>` (from `{.foo .bar}`).
    pub classes: String,
    /// Each cell's parsed block content. Phase 4 PR4.5 (2026-05-28)
    /// promoted this from `Vec<String>` (raw markdown source) to
    /// `Vec<Vec<Block>>` (fully-typed AST). Nested shortcodes inside a
    /// cell (`::::buttons` in `:::grid`) extract through
    /// [`crate::ast::parser::parse`] recursion in
    /// [`crate::ast::shortcode_extract::parse_grid`].
    ///
    /// Compound-link cells (the SoCiviC `[![[poster]] ### Title ...](/url)`
    /// pattern, where the entire cell is wrapped in a markdown link that
    /// spans block-level inner content) are represented as a single-element
    /// `vec![Block::LinkCard { url, children }]`. See
    /// [`Block::LinkCard`](crate::ast::Block::LinkCard) for the rationale.
    pub cells: Vec<Vec<Block>>,
    /// Spec § P9 width attribute: `body | wide | page | screen` (with
    /// `full` aliased to `screen`). `None` means the author did not set
    /// a width — the emitter omits `data-width` so the HTML stays sparse.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub width: Option<String>,
}

/// Arguments for [`Shortcode::Hero`].
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct HeroShortcode {
    /// Image source URL. `None` if neither the `image` attribute nor the
    /// first body line provided one — the renderer emits a section with
    /// no `<img>` in that case. Flows through resolver before rendering.
    pub image: Option<Url>,
    /// Pipe-suffix media attributes verbatim (e.g. "cover top",
    /// "1.5:1 contain"). Empty if no pipe in the source.
    pub attrs: String,
    /// Extra CSS classes for the wrapping `<section>` (from `{.foo .bar}`).
    pub classes: String,
    /// Parsed block content for the overlay. Phase 4 PR4.5 (2026-05-28)
    /// promoted this from `overlay_markdown: String` to `overlay: Vec<Block>`
    /// (fully-typed AST). Nested shortcodes inside the overlay
    /// (`::::buttons` inside `:::hero`) extract through
    /// [`crate::ast::parser::parse`] recursion in
    /// [`crate::ast::shortcode_extract::parse_hero`].
    pub overlay: Vec<Block>,
    /// Plain-text overlay source for downstream OG-fallback extraction.
    ///
    /// PR4.5 (2026-05-28): captured at parse time alongside the typed
    /// `overlay` because `crate::build::page::meta::extract_description`
    /// (the homepage-hero rung in the description chain) operates on
    /// markdown source — round-tripping `Vec<Block>` to markdown would
    /// invite drift. The renderer uses `overlay` for HTML; downstream
    /// consumers read `overlay_text` for description-chain extraction.
    ///
    /// Empty when the author wrote no overlay body.
    ///
    /// TODO(phase4-cleanup): replace with a Vec<Block>-walking
    /// `to_plain_text(blocks: &[Block]) -> String` helper in moss-core
    /// + consume `overlay` directly in `meta.rs::extract_description`,
    /// deleting this field. Carrying both `overlay: Vec<Block>` AND
    /// `overlay_text: String` makes the AST non-canonical (which is the
    /// source of truth?); per cross-SSG research, lossy or duplicate
    /// state is Gatsby's mistake. This is transitional — flag if it
    /// survives past PR7a. (Architecture review caveat 2026-05-28.)
    pub overlay_text: String,
    /// Spec § P9 width attribute: `body | wide | page | screen` (with
    /// `full` aliased to `screen`). `None` means the author did not set
    /// a width — the emitter omits `data-width` so the HTML stays sparse.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub width: Option<String>,
}

/// Arguments for [`Shortcode::Recent`].
///
/// Parameters parsed at shortcode-extract time; the query runs at render
/// time against the full post set. Renderer lives in
/// `src-tauri/src/build/markdown/recent.rs`.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct RecentShortcode {
    /// `since="YYYY-MM-DD"` — posts on or after this date. Stored as the
    /// raw string here; the rendering layer parses it into a DateTime.
    /// Mutually compatible with `last` — both set the cutoff, later wins.
    pub since: Option<String>,
    /// `last="week" | "month" | "Nd"` — relative window. The renderer
    /// converts this to a duration and subtracts from now.
    pub last: Option<String>,
    /// `count="N"` — cap at N most recent posts. The renderer applies a
    /// default of 10 when unset.
    pub count: Option<u32>,
    /// Body content rendered as fallback when zero posts match. Empty
    /// string means no fallback. Lives in the AST so the renderer doesn't
    /// need to re-read the source.
    pub fallback_markdown: String,
}

/// Identifier for a shortcode kind, used for AST queries (e.g.
/// `has_shortcode(&doc, ShortcodeKind::Subscribe)` to gate feature
/// detection without scanning source files).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ShortcodeKind {
    Subscribe,
    Buttons,
    Gallery,
    Hero,
    Grid,
    Recent,
}

impl Shortcode {
    /// Return the [`ShortcodeKind`] of this shortcode.
    pub fn kind(&self) -> ShortcodeKind {
        match self {
            Shortcode::Subscribe(_) => ShortcodeKind::Subscribe,
            Shortcode::Buttons(_) => ShortcodeKind::Buttons,
            Shortcode::Gallery(_) => ShortcodeKind::Gallery,
            Shortcode::Hero(_) => ShortcodeKind::Hero,
            Shortcode::Grid(_) => ShortcodeKind::Grid,
            Shortcode::Recent(_) => ShortcodeKind::Recent,
        }
    }
}

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

    #[test]
    fn shortcode_kind_variants_are_distinct() {
        let kinds = [
            ShortcodeKind::Subscribe,
            ShortcodeKind::Buttons,
            ShortcodeKind::Gallery,
            ShortcodeKind::Hero,
            ShortcodeKind::Grid,
            ShortcodeKind::Recent,
        ];
        let unique: std::collections::HashSet<_> = kinds.iter().collect();
        assert_eq!(unique.len(), kinds.len());
    }

    #[test]
    fn shortcode_kind_round_trips_through_serde() {
        for kind in [
            ShortcodeKind::Subscribe,
            ShortcodeKind::Buttons,
            ShortcodeKind::Gallery,
            ShortcodeKind::Hero,
            ShortcodeKind::Grid,
            ShortcodeKind::Recent,
        ] {
            let s = serde_json::to_string(&kind).expect("serialize");
            let back: ShortcodeKind = serde_json::from_str(&s).expect("deserialize");
            assert_eq!(kind, back);
        }
    }

    #[test]
    fn subscribe_kind_method_returns_subscribe() {
        let sc = Shortcode::Subscribe(SubscribeShortcode::default());
        assert_eq!(sc.kind(), ShortcodeKind::Subscribe);
    }

    #[test]
    fn subscribe_with_placeholder_and_button() {
        let sc = Shortcode::Subscribe(SubscribeShortcode {
            placeholder: Some("you@example.com".to_string()),
            button: Some("Subscribe".to_string()),
        });
        match &sc {
            Shortcode::Subscribe(args) => {
                assert_eq!(args.placeholder.as_deref(), Some("you@example.com"));
                assert_eq!(args.button.as_deref(), Some("Subscribe"));
            }
            other => panic!("expected Subscribe, got {other:?}"),
        }
    }

    #[test]
    fn subscribe_default_has_none_placeholder_and_button() {
        let args = SubscribeShortcode::default();
        assert!(args.placeholder.is_none());
        assert!(args.button.is_none());
    }

    #[test]
    fn subscribe_round_trips_through_serde() {
        let sc = Shortcode::Subscribe(SubscribeShortcode {
            placeholder: Some("p".to_string()),
            button: Some("b".to_string()),
        });
        let s = serde_json::to_string(&sc).expect("serialize");
        let back: Shortcode = serde_json::from_str(&s).expect("deserialize");
        assert_eq!(sc, back);
    }

    // ---- Buttons ----

    #[test]
    fn buttons_kind_method_returns_buttons() {
        let sc = Shortcode::Buttons(ButtonsShortcode::default());
        assert_eq!(sc.kind(), ShortcodeKind::Buttons);
    }

    #[test]
    fn buttons_items_carry_unresolved_urls() {
        let sc = Shortcode::Buttons(ButtonsShortcode {
            classes: String::new(),
            items: vec![
                ButtonItem {
                    text: "Docs".to_string(),
                    url: Url::unresolved("docs/"),
                },
                ButtonItem {
                    text: "GitHub".to_string(),
                    url: Url::unresolved("https://github.com"),
                },
            ],
        });
        match &sc {
            Shortcode::Buttons(args) => {
                assert_eq!(args.items.len(), 2);
                assert!(args.items[0].url.is_unresolved());
                assert!(args.items[1].url.is_unresolved());
            }
            _ => unreachable!(),
        }
    }

    #[test]
    fn buttons_default_has_no_items() {
        let args = ButtonsShortcode::default();
        assert!(args.items.is_empty());
        assert!(args.classes.is_empty());
    }

    #[test]
    fn buttons_round_trips_through_serde() {
        let sc = Shortcode::Buttons(ButtonsShortcode {
            classes: "primary".to_string(),
            items: vec![ButtonItem {
                text: "Go".to_string(),
                url: Url::unresolved("/x"),
            }],
        });
        let s = serde_json::to_string(&sc).expect("serialize");
        let back: Shortcode = serde_json::from_str(&s).expect("deserialize");
        assert_eq!(sc, back);
    }

    // ---- Gallery ----

    #[test]
    fn gallery_kind_method_returns_gallery() {
        let sc = Shortcode::Gallery(GalleryShortcode::default());
        assert_eq!(sc.kind(), ShortcodeKind::Gallery);
    }

    #[test]
    fn gallery_items_carry_unresolved_urls() {
        let sc = Shortcode::Gallery(GalleryShortcode {
            columns: Some(3),
            classes: String::new(),
            items: vec![
                GalleryItem {
                    src: Url::unresolved("a.jpg"),
                    alt: "A".to_string(),
                    attrs: String::new(),
                },
                GalleryItem {
                    src: Url::unresolved("b.jpg"),
                    alt: "B".to_string(),
                    attrs: "cover top".to_string(),
                },
            ],
            width: None,
        });
        match &sc {
            Shortcode::Gallery(args) => {
                assert_eq!(args.columns, Some(3));
                assert_eq!(args.items.len(), 2);
                assert!(args.items[0].src.is_unresolved());
                assert_eq!(args.items[1].attrs, "cover top");
            }
            _ => unreachable!(),
        }
    }

    #[test]
    fn gallery_default_no_columns_no_items() {
        let args = GalleryShortcode::default();
        assert!(args.columns.is_none());
        assert!(args.items.is_empty());
        assert!(args.classes.is_empty());
    }

    #[test]
    fn gallery_round_trips_through_serde() {
        let sc = Shortcode::Gallery(GalleryShortcode {
            columns: Some(4),
            classes: "showcase".to_string(),
            items: vec![GalleryItem {
                src: Url::unresolved("p.png"),
                alt: "Photo".to_string(),
                attrs: "1:1 contain".to_string(),
            }],
            width: None,
        });
        let s = serde_json::to_string(&sc).expect("serialize");
        let back: Shortcode = serde_json::from_str(&s).expect("deserialize");
        assert_eq!(sc, back);
    }

    // ---- Recent ----

    #[test]
    fn recent_kind_method_returns_recent() {
        let sc = Shortcode::Recent(RecentShortcode::default());
        assert_eq!(sc.kind(), ShortcodeKind::Recent);
    }

    #[test]
    fn recent_default_has_none_params_empty_fallback() {
        let args = RecentShortcode::default();
        assert!(args.since.is_none());
        assert!(args.last.is_none());
        assert!(args.count.is_none());
        assert!(args.fallback_markdown.is_empty());
    }

    #[test]
    fn recent_round_trips_through_serde() {
        let sc = Shortcode::Recent(RecentShortcode {
            since: Some("2026-04-01".to_string()),
            last: None,
            count: Some(5),
            fallback_markdown: "_No posts yet._".to_string(),
        });
        let s = serde_json::to_string(&sc).expect("serialize");
        let back: Shortcode = serde_json::from_str(&s).expect("deserialize");
        assert_eq!(sc, back);
    }
}