marmite 0.4.0

Marmite is the easiest static site generator.
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
use super::*;
use crate::config::Marmite;
use crate::content::{Content, GroupedContent, Kind};
use crate::site::Data;
use std::collections::HashMap;

/// Convert markdown to html using comrak,
///
/// Uses the default parser options and no syntax highlighting.
pub fn get_html(markdown: &str) -> String {
    get_html_with_options(markdown, &ParserOptions::default(), None)
}

#[test]
fn test_fix_internal_links_with_md_extension() {
    let html = r#"<a href="test.md">test.md</a>"#;
    let expected = r#"<a href="test.html">test</a>"#;
    assert_eq!(fix_internal_links(html), expected);
}

#[test]
fn test_fix_internal_links_with_html_extension() {
    let html = r#"<a href="test.html">test.html</a>"#;
    let expected = r#"<a href="test.html">test</a>"#;
    assert_eq!(fix_internal_links(html), expected);
}

#[test]
fn test_fix_internal_links_without_extension() {
    let html = r#"<a href="test">test</a>"#;
    let expected = r#"<a href="test.html">test</a>"#;
    assert_eq!(fix_internal_links(html), expected);
}

#[test]
fn test_fix_internal_links_external_link() {
    let html = r#"<a href="http://example.com">example</a>"#;
    let expected = r#"<a href="http://example.com">example</a>"#;
    assert_eq!(fix_internal_links(html), expected);
}

#[test]
fn test_fix_internal_links_mixed_content() {
    let html = r#"<a href="test.md">test.md</a> and <a href="http://example.com">example</a>"#;
    let expected = r#"<a href="test.html">test</a> and <a href="http://example.com">example</a>"#;
    assert_eq!(fix_internal_links(html), expected);
}

#[test]
fn test_get_links_to_with_internal_links() {
    let html = r#"<a href="./test1.html">test1</a> <a href="./test2.html">test2</a>"#;
    let expected = Some(vec!["test1".to_string(), "test2".to_string()]);
    assert_eq!(get_links_to(html), expected);
}

#[test]
fn test_get_links_to_with_internal_links_no_slash() {
    let html = r#"<a href="test1.html">test1</a> <a href="test2.html">test2</a>"#;
    let expected = Some(vec!["test1".to_string(), "test2".to_string()]);
    assert_eq!(get_links_to(html), expected);
}

#[test]
fn test_get_links_to_with_no_internal_links() {
    let html = r#"<a href="http://example.com">example</a>"#;
    let expected: Option<Vec<String>> = None;
    assert_eq!(get_links_to(html), expected);
}

#[test]
fn test_get_links_to_with_mixed_links() {
    let html = r#"<a href="./test1.html">test1</a> <a href="test2.html">test2</a> <a href="http://example.com">example</a>"#;
    let expected = Some(vec!["test1".to_string(), "test2".to_string()]);
    assert_eq!(get_links_to(html), expected);
}

#[test]
fn test_get_links_to_with_no_links() {
    let html = r"<p>No links here</p>";
    let expected: Option<Vec<String>> = None;
    assert_eq!(get_links_to(html), expected);
}

#[test]
fn test_get_links_to_with_empty_string() {
    let html = "";
    let expected: Option<Vec<String>> = None;
    assert_eq!(get_links_to(html), expected);
}

#[test]
fn test_get_links_to_with_internal_link_with_heading() {
    let html = r#"<a href="test1.html#heading">test</a>"#;
    let expected = Some(vec!["test1#heading".to_string()]);

    assert_eq!(get_links_to(html), expected);
}

#[test]
fn test_get_html_basic_markdown() {
    let markdown = "# Title\n\nThis is a paragraph.";
    let expected = "<h1><a href=\"#title\" aria-hidden=\"true\" class=\"anchor\" id=\"title\"></a>Title</h1>\n<p>This is a paragraph.</p>\n";
    assert_eq!(get_html(markdown), expected);
}

#[test]
fn test_get_html_with_links() {
    let markdown = "[example](http://example.com)";
    let expected = "<p><a href=\"http://example.com\">example</a></p>\n";
    assert_eq!(get_html(markdown), expected);
}

#[test]
fn test_get_html_with_internal_relative_links() {
    let markdown = "[internal](./test.md)";
    let expected = "<p><a href=\"./test.md\">internal</a></p>\n";
    assert_eq!(get_html(markdown), expected);
}

#[test]
fn test_get_html_with_internal_links_no_slash() {
    let markdown = "[internal](test.md)";
    let expected = "<p><a href=\"test.html\">internal</a></p>\n";
    assert_eq!(get_html(markdown), expected);
}

#[test]
fn test_get_html_with_images() {
    let markdown = "![alt text](media/image.jpg)";
    let expected = "<p><figure><img src=\"media/image.jpg\" alt=\"alt text\" /></figure></p>\n";
    assert_eq!(get_html(markdown), expected);
}

#[test]
fn test_blockquotes_without_space() {
    let markdown = ">testing blockquote";
    let expected = "<blockquote>\n<p>testing blockquote</p>\n</blockquote>\n";
    assert_eq!(get_html(markdown), expected);
}

#[test]
fn test_blockquotes_with_space() {
    let markdown = "> testing blockquote";
    let expected = "<blockquote>\n<p>testing blockquote</p>\n</blockquote>\n";
    assert_eq!(get_html(markdown), expected);
}

#[test]
fn test_blockquotes_with_multiline() {
    let markdown = "> testing blockquote\n> line 2";
    let expected = "<blockquote>\n<p>testing blockquote\nline 2</p>\n</blockquote>\n";
    assert_eq!(get_html(markdown), expected);
}

#[test]
fn test_blockquotes_with_triple() {
    let markdown = ">>>\ntesting blockquote\n line 2\n>>>";
    let expected = "<blockquote>\n<p>testing blockquote\nline 2</p>\n</blockquote>\n";
    assert_eq!(get_html(markdown), expected);
}

#[test]
fn test_fix_internal_links_with_media_files() {
    let html = r#"<a href="media/image.jpg">View Image</a>"#;
    let expected = r#"<a href="media/image.jpg">View Image</a>"#;
    assert_eq!(fix_internal_links(html), expected);
}

#[test]
fn test_fix_internal_links_with_media_files_webp() {
    let html = r#"<a href="/media/homecloudpart1-mikrotik.webp">View Image</a>"#;
    let expected = r#"<a href="/media/homecloudpart1-mikrotik.webp">View Image</a>"#;
    assert_eq!(fix_internal_links(html), expected);
}

#[test]
fn test_fix_internal_links_with_various_media_extensions() {
    let test_cases = vec![
        ("media/image.jpg", "media/image.jpg"),
        ("media/image.jpeg", "media/image.jpeg"),
        ("media/image.png", "media/image.png"),
        ("media/image.gif", "media/image.gif"),
        ("media/image.webp", "media/image.webp"),
        ("media/image.svg", "media/image.svg"),
        ("media/image.avif", "media/image.avif"),
        ("media/video.mp4", "media/video.mp4"),
        ("media/audio.mp3", "media/audio.mp3"),
        ("media/document.pdf", "media/document.pdf"),
    ];

    for (input, expected) in test_cases {
        let html = format!(r#"<a href="{input}">Link</a>"#);
        let expected_html = format!(r#"<a href="{expected}">Link</a>"#);
        assert_eq!(fix_internal_links(&html), expected_html);
    }
}

#[test]
fn test_get_html_with_code_block() {
    let markdown = "```\nlet x = 1;\n```";
    let expected = "<pre><code>let x = 1;\n</code></pre>\n";
    assert_eq!(get_html(markdown), expected);
}

#[test]
fn test_get_html_with_task_list() {
    let markdown = "- [x] Task 1\n- [ ] Task 2";
    let expected = "<ul>\n<li><input type=\"checkbox\" checked=\"\" disabled=\"\" /> Task 1</li>\n<li><input type=\"checkbox\" disabled=\"\" /> Task 2</li>\n</ul>\n";
    assert_eq!(get_html(markdown), expected);
}

#[test]
fn test_get_html_with_table() {
    let markdown = "| Header1 | Header2 |\n| ------- | ------- |\n| Cell1   | Cell2   |";
    let expected = "<table>\n<thead>\n<tr>\n<th>Header1</th>\n<th>Header2</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>Cell1</td>\n<td>Cell2</td>\n</tr>\n</tbody>\n</table>\n";
    assert_eq!(get_html(markdown), expected);
}

#[test]
fn test_get_table_of_contents_from_html_with_single_header() {
    let html = r##"<h1><a href="#header1"></a>Header 1</h1>"##;
    let expected = "<ul>\n<li><a href=\"#header1\">Header 1</a></li>\n</ul>\n";
    assert_eq!(get_table_of_contents_from_html(html), expected);
}

#[test]
fn test_get_table_of_contents_from_html_with_multiple_headers() {
    let html = r##"
        <h1><a href="#header1"></a>Header 1</h1>
        <h2><a href="#header2"></a>Header 2</h2>
        <h3><a href="#header3"></a>Header 3</h3>
    "##;
    let expected = "<ul>\n<li><a href=\"#header1\">Header 1</a></li>\n<ul>\n<li><a href=\"#header2\">Header 2</a></li>\n<ul>\n<li><a href=\"#header3\">Header 3</a></li>\n</ul>\n</ul>\n</ul>\n";
    assert_eq!(get_table_of_contents_from_html(html), expected);
}

#[test]
fn test_get_table_of_contents_from_html_with_nested_headers() {
    let html = r##"
        <h1><a href="#header1"></a>Header 1</h1>
        <h2><a href="#header2"></a>Header 2</h2>
        <h1><a href="#header3"></a>Header 3</h1>
    "##;
    let expected = "<ul>\n<li><a href=\"#header1\">Header 1</a></li>\n<ul>\n<li><a href=\"#header2\">Header 2</a></li>\n</ul>\n<li><a href=\"#header3\">Header 3</a></li>\n</ul>\n";
    assert_eq!(get_table_of_contents_from_html(html), expected);
}

#[test]
fn test_get_table_of_contents_from_html_with_no_headers() {
    let html = r"<p>No headers here</p>";
    let expected = "";
    assert_eq!(get_table_of_contents_from_html(html), expected);
}

#[test]
fn test_get_table_of_contents_from_html_with_mixed_content() {
    let html = r##"
        <h1><a href="#header1"></a>Header 1</h1>
        <p>Some content</p>
        <h2><a href="#header2"></a>Header 2</h2>
        <p>More content</p>
    "##;
    let expected = "<ul>\n<li><a href=\"#header1\">Header 1</a></li>\n<ul>\n<li><a href=\"#header2\">Header 2</a></li>\n</ul>\n</ul>\n";
    assert_eq!(get_table_of_contents_from_html(html), expected);
}

// Helper function to create test site data
fn create_test_site_data() -> Data {
    let posts = vec![
        Content {
            title: "TWSBI Eco Indigo Blue & de Atramentis Document Brown".to_string(),
            slug: "twsbi-eco-indigo-blue-de-atramentis-document-brown".to_string(),
            ..Default::default()
        },
        Content {
            title: "Another Post".to_string(),
            slug: "another-post".to_string(),
            ..Default::default()
        },
    ];

    let pages = vec![Content {
        title: "About Page".to_string(),
        slug: "about".to_string(),
        ..Default::default()
    }];

    Data {
        site: Marmite::default(),
        posts,
        pages,
        tag: GroupedContent::new(Kind::Tag),
        archive: GroupedContent::new(Kind::Archive),
        author: GroupedContent::new(Kind::Author),
        stream: GroupedContent::new(Kind::Stream),
        series: GroupedContent::new(Kind::Series),
        language: GroupedContent::new(Kind::Language),
        latest_timestamp: None,
        config_path: String::new(),
        force_render: false,
        generated_urls: crate::site::UrlCollection::default(),
        galleries: HashMap::new(),
    }
}

#[test]
fn test_fix_wikilinks_exact_title_match() {
    let site_data = create_test_site_data();
    let html = r#"<a href="twsbi-eco-indigo-blue-amp-de-atramentis-document-brown.html" data-wikilink="true">TWSBI Eco Indigo Blue &amp; de Atramentis Document Brown</a>"#;
    let expected = r#"<a href="twsbi-eco-indigo-blue-de-atramentis-document-brown.html" data-wikilink="true">TWSBI Eco Indigo Blue &amp; de Atramentis Document Brown</a>"#;

    assert_eq!(fix_wikilinks(html, &site_data), expected);
}

#[test]
fn test_fix_wikilinks_case_insensitive() {
    let site_data = create_test_site_data();
    let html = r#"<a href="another-post-wrong.html" data-wikilink="true">another post</a>"#;
    let expected = r#"<a href="another-post.html" data-wikilink="true">another post</a>"#;

    assert_eq!(fix_wikilinks(html, &site_data), expected);
}

#[test]
fn test_fix_wikilinks_no_match_unchanged() {
    let site_data = create_test_site_data();
    let html = r#"<a href="nonexistent-post.html" data-wikilink="true">Nonexistent Post</a>"#;
    let expected = r#"<a href="nonexistent-post.html" data-wikilink="true">Nonexistent Post</a>"#;

    assert_eq!(fix_wikilinks(html, &site_data), expected);
}

#[test]
fn test_fix_wikilinks_multiple_links() {
    let site_data = create_test_site_data();
    let html = r#"<p>Check out <a href="another-post-wrong.html" data-wikilink="true">Another Post</a> and <a href="about-wrong.html" data-wikilink="true">About Page</a>.</p>"#;
    let expected = r#"<p>Check out <a href="another-post.html" data-wikilink="true">Another Post</a> and <a href="about.html" data-wikilink="true">About Page</a>.</p>"#;

    assert_eq!(fix_wikilinks(html, &site_data), expected);
}

#[test]
fn test_fix_wikilinks_html_entities() {
    let site_data = create_test_site_data();
    let html = r#"<a href="twsbi-wrong.html" data-wikilink="true">TWSBI Eco Indigo Blue &amp; de Atramentis Document Brown</a>"#;
    let expected = r#"<a href="twsbi-eco-indigo-blue-de-atramentis-document-brown.html" data-wikilink="true">TWSBI Eco Indigo Blue &amp; de Atramentis Document Brown</a>"#;

    assert_eq!(fix_wikilinks(html, &site_data), expected);
}

#[test]
fn test_fix_wikilinks_ignores_regular_links() {
    let site_data = create_test_site_data();
    let html = r#"<a href="another-post-wrong.html">Another Post</a> and <a href="external.com">External</a>"#;
    let expected = r#"<a href="another-post-wrong.html">Another Post</a> and <a href="external.com">External</a>"#;

    assert_eq!(fix_wikilinks(html, &site_data), expected);
}

#[test]
fn test_fix_wikilinks_mixed_content() {
    let site_data = create_test_site_data();
    let html = r#"<p>Regular <a href="external.com">external link</a> and <a href="wrong.html" data-wikilink="true">About Page</a> wikilink.</p>"#;
    let expected = r#"<p>Regular <a href="external.com">external link</a> and <a href="about.html" data-wikilink="true">About Page</a> wikilink.</p>"#;

    assert_eq!(fix_wikilinks(html, &site_data), expected);
}

#[test]
fn test_fix_internal_links_with_percent_encoded_spaces() {
    let html = r#"<a href="Markdown%20Format" data-wikilink="true">Markdown Format</a>"#;
    let expected = r#"<a href="markdown-format.html" data-wikilink="true">Markdown Format</a>"#;
    assert_eq!(fix_internal_links(html), expected);
}

#[test]
fn test_fix_internal_links_with_percent_encoded_spaces_and_fragment() {
    let html =
        r#"<a href="Markdown%20Format#Obsidian%20Links" data-wikilink="true">Markdown Format</a>"#;
    let expected =
        r#"<a href="markdown-format.html#obsidian-links" data-wikilink="true">Markdown Format</a>"#;
    assert_eq!(fix_internal_links(html), expected);
}

#[test]
fn test_decode_html_entities() {
    assert_eq!(decode_html_entities("Test &amp; Example"), "Test & Example");
    assert_eq!(decode_html_entities("&lt;tag&gt;"), "<tag>");
    assert_eq!(decode_html_entities("&quot;quoted&quot;"), "\"quoted\"");
    assert_eq!(decode_html_entities("It&#39;s working"), "It's working");
    assert_eq!(
        decode_html_entities("Mixed &amp; &#39;entities&#39; &lt;here&gt;"),
        "Mixed & 'entities' <here>"
    );
}

#[test]
fn test_render_native_mermaid_basic() {
    let html = r#"<p>Before</p><pre class="marmite-code"><code class="marmite-code-inner language-mermaid">graph LR
  A --&gt; B</code></pre><p>After</p>"#;
    let result = render_native_mermaid(html, "test-slug", None);
    assert!(result.contains("<div class=\"mermaid-diagram\">"));
    assert!(result.contains("<svg"));
    assert!(result.contains("<p>Before</p>"));
    assert!(result.contains("<p>After</p>"));
    assert!(!result.contains("language-mermaid"));
}

#[test]
fn test_render_native_mermaid_preserves_non_mermaid_blocks() {
    let html = r#"<pre class="marmite-code"><code class="marmite-code-inner language-rust">fn main() {}</code></pre>"#;
    let result = render_native_mermaid(html, "test-slug", None);
    assert_eq!(result, html);
}

#[test]
fn test_render_native_mermaid_no_mermaid_blocks() {
    let html = "<p>No code blocks here</p>";
    let result = render_native_mermaid(html, "test-slug", None);
    assert_eq!(result, html);
}

#[test]
fn test_render_native_mermaid_with_dark_theme() {
    let config: serde_yaml::Value = serde_yaml::from_str("theme: dark").unwrap();
    let html = r#"<pre class="marmite-code"><code class="marmite-code-inner language-mermaid">graph LR
  A --&gt; B</code></pre>"#;
    let result = render_native_mermaid(html, "test-slug", Some(&config));
    assert!(result.contains("<div class=\"mermaid-diagram\">"));
    assert!(result.contains("<svg"));
}

#[test]
fn test_render_native_mermaid_with_spacing_config() {
    let config: serde_yaml::Value = serde_yaml::from_str(
        r#"
flowchart:
  nodeSpacing: 80
  rankSpacing: 60
"#,
    )
    .unwrap();
    let html = r#"<pre class="marmite-code"><code class="marmite-code-inner language-mermaid">graph LR
  A --&gt; B</code></pre>"#;
    let result = render_native_mermaid(html, "test-slug", Some(&config));
    assert!(result.contains("<div class=\"mermaid-diagram\">"));
    assert!(result.contains("<svg"));
}

#[test]
fn test_render_native_mermaid_invalid_config_falls_back() {
    let config: serde_yaml::Value = serde_yaml::from_str("theme: nonexistent_theme").unwrap();
    let html = r#"<pre class="marmite-code"><code class="marmite-code-inner language-mermaid">graph LR
  A --&gt; B</code></pre>"#;
    let result = render_native_mermaid(html, "test-slug", Some(&config));
    // Falls back to defaults on invalid theme
    assert!(result.contains("<svg") || result.contains("language-mermaid"));
}