Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make reference maps extensible #17

Merged
merged 1 commit into from
May 13, 2023
Merged

make reference maps extensible #17

merged 1 commit into from
May 13, 2023

Conversation

rlidwka
Copy link
Collaborator

@rlidwka rlidwka commented May 7, 2023

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
Copy link

Codecov Report

Patch coverage: 93.75% and project coverage change: -0.06 ⚠️

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

📣 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
@@            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 Coverage Δ
src/plugins/cmark/block/reference.rs 94.89% <91.30%> (-1.40%) ⬇️
src/generics/inline/full_link.rs 98.42% <100.00%> (-0.03%) ⬇️

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

@rlidwka rlidwka merged commit a80c8ce into master May 13, 2023
7 checks passed
@rlidwka rlidwka deleted the reference-maps branch June 6, 2023 08:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants