Skip to content

How can I write custom parser to convert to link with prefix? #385

Closed Answered by kivikakk
grindarius asked this question in Q&A
Discussion options

You must be logged in to vote

This will be relatively complicated to do with a post-processing step on the AST itself: you'd need to split the Text node into separate Text and Link nodes within the container, rather than just modifying the Text node itself. How about preprocessing the Markdown input instead?

use comrak::{format_html, parse_document, Arena, Options};
use once_cell::sync::Lazy;
use regex::Regex;
use std::error::Error;

pub static HASHTAG_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?m)#([\w\d_]+)").unwrap());

fn main() -> Result<(), Box<dyn Error>> {
    let arena = Arena::new();

    let doc = "# welcomeee\n\n#welcome\n\nmy guy";
    let tagged_doc = HASHTAG_REGEX.replace_all(&doc, "[#$1](/hashtags…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@grindarius
Comment options

Answer selected by grindarius
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants