index : sparkle-git.git

ascending towards madness

author holly sparkles <sparkles@holly.sh> 2024-01-05 19:21:46.0 +00:00:00
committer holly sparkles <sparkles@holly.sh> 2024-05-14 6:40:47.0 +00:00:00
commit
3f6854712825d1b6972a82aa7ae749fdcca2e145 [patch]
tree
07d9f145deae3f4d84946b979d943f87bbf963fe
parent
9ffa4d8b585543993d0c4736c8bcd0578dd6a371
download
3f6854712825d1b6972a82aa7ae749fdcca2e145.tar.gz

refactor: github flavoured markdown options to be reusable.

- also fixes repo readmes not rendering tables

Diff

 src/configuration/mod.rs | 19 +++++++++++++++++++
 src/git.rs               |  4 ++--
 src/methods/about.rs     | 24 +++++++++---------------
 3 files changed, 30 insertions(+), 17 deletions(-)

diff --git a/src/configuration/mod.rs b/src/configuration/mod.rs
index 6ce7fc0..17b84cc 100644
--- a/src/configuration/mod.rs
+++ b/src/configuration/mod.rs
@@ -1,5 +1,6 @@
use std::{path::Path, vec};

use comrak::Options;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -111,3 +112,21 @@ impl AppConfig {
        Path::new(&self.root_readme).try_exists().unwrap_or(false)
    }
}

pub struct ReadmeConfig;

impl ReadmeConfig {
    pub fn gfm() -> Options {
        // 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;

        options
    }
}
diff --git a/src/git.rs b/src/git.rs
index 7f40cf3..91a5e8d 100644
--- a/src/git.rs
+++ b/src/git.rs
@@ -25,7 +25,7 @@ use syntect::{
use time::OffsetDateTime;
use tracing::{error, instrument, warn};

use crate::syntax_highlight::ComrakSyntectAdapter;
use crate::{configuration::ReadmeConfig, syntax_highlight::ComrakSyntectAdapter};

type ReadmeCacheKey = (PathBuf, Option<Arc<str>>);

@@ -446,7 +446,7 @@ fn parse_and_transform_markdown(s: &str, syntax_set: &SyntaxSet) -> String {
    options.extension.tagfilter = true;
    options.extension.tasklist = true;

    comrak::markdown_to_html_with_plugins(s, &options, &plugins)
    comrak::markdown_to_html_with_plugins(s, &ReadmeConfig::gfm(), &plugins)
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
diff --git a/src/methods/about.rs b/src/methods/about.rs
index 42ea972..f11c2f4 100644
--- a/src/methods/about.rs
+++ b/src/methods/about.rs
@@ -1,9 +1,14 @@
use std::path::PathBuf;

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};
use crate::{
    configuration::{AppConfig, ReadmeConfig},
    into_response,
    methods::filters,
    methods::repo::Result,
};

#[derive(Template)]
#[template(path = "about.html")]
@@ -13,22 +18,11 @@ pub struct View {
}

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,
        &ReadmeConfig::gfm(),
    );

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