markdown-it-rust / markdown-it

markdown-it js library rewritten in rust
Other
79 stars 9 forks source link

make reference maps extensible #17

Closed rlidwka closed 1 year ago

rlidwka commented 1 year ago

This commit makes it possible to add custom handling for unknown reference labels:

use markdown_it::parser::block::builtin::BlockParserRule;
use markdown_it::parser::core::{CoreRule, Root};
use markdown_it::plugins::cmark::block::reference::{ReferenceMap, DefaultReferenceMap, CustomReferenceMap};
use markdown_it::{MarkdownIt, Node};

let md = &mut MarkdownIt::new();
markdown_it::plugins::cmark::add(md);

#[derive(Debug, Default)]
struct RefMapOverride(DefaultReferenceMap);
impl CustomReferenceMap for RefMapOverride {
    fn get(&self, label: &str) -> Option<(&str, Option<&str>)> {
        // override a specific link
        if label == "rust" {
            return Some((
                "https://www.rust-lang.org/",
                Some("The Rust Language"),
            ));
        }

        self.0.get(label)
    }

    fn insert(&mut self, label: String, destination: String, title: Option<String>) -> bool {
        self.0.insert(label, destination, title)
    }
}

struct AddCustomReferences;
impl CoreRule for AddCustomReferences {
    fn run(root: &mut Node, _: &MarkdownIt) {
        let data = root.cast_mut::<Root>().unwrap();
        data.ext.insert(ReferenceMap::new(RefMapOverride::default()));
    }
}

md.add_rule::<AddCustomReferences>()
    .before::<BlockParserRule>();

let html = md.parse("[rust]").render();
assert_eq!(
    html.trim(),
    r#"<p><a href="https://www.rust-lang.org/" title="The Rust Language">rust</a></p>"#
);
codecov-commenter commented 1 year ago

Codecov Report

Patch coverage: 93.75% and project coverage change: -0.06 :warning:

Comparison is base (e781c6e) 94.60% compared to head (d0e4314) 94.55%.

:mega: This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #17 +/- ## ========================================== - Coverage 94.60% 94.55% -0.06% ========================================== Files 56 56 Lines 2411 2425 +14 ========================================== + Hits 2281 2293 +12 - Misses 130 132 +2 ``` | [Impacted Files](https://app.codecov.io/gh/rlidwka/markdown-it.rs/pull/17?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Alex+Kocharin) | Coverage Δ | | |---|---|---| | [src/plugins/cmark/block/reference.rs](https://app.codecov.io/gh/rlidwka/markdown-it.rs/pull/17?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Alex+Kocharin#diff-c3JjL3BsdWdpbnMvY21hcmsvYmxvY2svcmVmZXJlbmNlLnJz) | `94.89% <91.30%> (-1.40%)` | :arrow_down: | | [src/generics/inline/full\_link.rs](https://app.codecov.io/gh/rlidwka/markdown-it.rs/pull/17?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Alex+Kocharin#diff-c3JjL2dlbmVyaWNzL2lubGluZS9mdWxsX2xpbmsucnM=) | `98.42% <100.00%> (-0.03%)` | :arrow_down: |

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.