about summary refs log tree commit diff stats
path: root/src/web/generate/templates.rs
blob: 693b8628bb988f99a63c64050b7ba6f946490e03 (plain) (blame)
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
use vy::{DOCTYPE, IntoHtml, PreEscaped, body, div, head, html, link, meta, title};

pub(crate) fn make_page(
    content: impl IntoHtml,
    description: &str,
    title: Option<&str>,
) -> impl IntoHtml {
    (
        DOCTYPE,
        html!(
            lang = "en",
            head!(
                title!(title.unwrap_or("Back")),
                link!(rel = "icon", href = "/favicon.ico"),
                link!(rel = "stylesheet", "type" = "text/css", href = "/style.css"),
                meta!(charset = "UTF-8"),
                meta!(
                    name = "viewport",
                    content = "width=device-width,initial-scale=1"
                ),
                meta!(name = "description", content = description),
            ),
            body!(div!(class = "content", content))
        ),
    )
}

pub(super) fn to_markdown(input: &str, is_title: bool) -> PreEscaped<String> {
    let markdown = markdown::to_html(input.trim());

    // If the markdown contains only one line line, assuming that it is a title is okay.
    if input.lines().count() == 1 && markdown.starts_with("<p>") && is_title {
        PreEscaped(
            markdown
                .strip_prefix("<p>")
                .expect("We checked")
                .strip_suffix("</p>")
                .expect("markdown crate produces no invalid html")
                .to_owned(),
        )
    } else {
        PreEscaped(markdown)
    }
}