index : gitrs
A lightweight web interface for browsing Git repositories, implemented in Rust
about summary refs log tree commit diff

Diffstat (limited to 'src/markdown.rs')

-rw-r--r-- src/markdown.rs 18
 1 file changed, 18 insertions(+)

diff --git a/src/markdown.rs b/src/markdown.rs
index 8e17298..ed0c6ce 100644
--- a/src/markdown.rs
+++ b/src/markdown.rs
@@ -174,7 +174,23 @@
        || (url.starts_with("https://github.com/") && url.contains("/assets/"))
}

fn escape_attr(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for c in s.chars() {
        match c {
            '&' => out.push_str("&"),
            '"' => out.push_str("""),
            '\'' => out.push_str("'"),
            '<' => out.push_str("&lt;"),
            '>' => out.push_str("&gt;"),
            _ => out.push(c),
        }
    }
    out
}

fn video_html(url: &str, fallback_to_img: bool) -> String {
    let url = escape_attr(url);
    if fallback_to_img {
        format!(
            r#"<video class="readme-video" controls playsinline preload="metadata" src="{url}" onerror="const img=document.createElement('img');img.src=this.src;img.className='readme-img';this.replaceWith(img);"></video>"#
@@ -187,6 +203,8 @@
}

fn github_asset_img_html(url: &str, alt: &str) -> String {
    let url = escape_attr(url);
    let alt = escape_attr(alt);
    format!(
        r#"<img class="readme-img" src="{url}" alt="{alt}" onerror="const v=document.createElement('video');v.src=this.src;v.controls=true;v.playsInline=true;v.preload='metadata';v.className='readme-video';this.replaceWith(v);">"#
    )