index : sparkle-git.git

ascending towards madness

use askama::Template;
use axum::{response::IntoResponse, Extension};
use comrak::Options;
use std::path::PathBuf;

use crate::{configuration::AppConfig, into_response, methods::filters, methods::repo::Result};

#[derive(Template)]
#[template(path = "about.html")]
pub struct View {
    about: String,
    config: AppConfig,
}

pub async fn handle(Extension(config): Extension<AppConfig>) -> Result<impl IntoResponse> {
    //TODO: refactor this for reuse with git.rs in `parse_and_transform_markdown`
    // enable gfm extensions
    // https://github.github.com/gfm/
    let mut options = Options::default();
    options.extension.autolink = true;
    options.extension.footnotes = true;
    options.extension.strikethrough = true;
    options.extension.table = true;
    options.extension.tagfilter = true;
    options.extension.tasklist = true;

    let about = comrak::markdown_to_html(
        &get_readme_file(PathBuf::from(&config.root_readme))
            .await
            .unwrap_or_else(|_| "`Error opening root_readme file.`".to_string()),
        &options,
    );

    Ok(into_response(View { about, config }))
}

async fn get_readme_file(path: PathBuf) -> Result<String, anyhow::Error> {
    let mut file = tokio::fs::File::open(path).await?;
    let mut contents = String::new();
    tokio::io::AsyncReadExt::read_to_string(&mut file, &mut contents).await?;
    Ok(contents)
}