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

Expose to_html::compile #115

Closed
mousetail opened this issue May 1, 2024 · 8 comments
Closed

Expose to_html::compile #115

mousetail opened this issue May 1, 2024 · 8 comments

Comments

@mousetail
Copy link

mousetail commented May 1, 2024

Currently, the to_mdast method is exposed, to create a syntax tree. However, after making the necessary modifications to this syntax tree it seems there is no exposed way to turn the result into HTML.

pub mod to_html should fix it though I'm not sure what other undesired things that would expose. Something to think about.

My use case is I want to replace all code blocks with a certain language with an image based on the content of the code block.

@wooorm
Copy link
Owner

wooorm commented May 1, 2024

Hey!

should fix it

No it doesn’t. That doesn’t take an AST.


What you are looking for is discussed in several open issues. GH-27. GH-32.

@wooorm wooorm closed this as not planned Won't fix, can't repro, duplicate, stale May 1, 2024
@mousetail
Copy link
Author

Hmm in that case I'd need a way to access and modify the event list as they are being streamed

@ChristianMurphy
Copy link
Collaborator

No, the tickets @wooorm reference are about supporting working with the syntax tree.
The API you want to make public doesn't fix the problem you want to solve, the issues above do.

@mousetail
Copy link
Author

I don't need a full plugin system, I just need to get between these two lines:

pub fn to_html_with_options(value: &str, options: &Options) -> Result<String, message::Message> {
    let (events, parse_state) = parser::parse(value, &options.parse)?;
    
    // do something with events
    Ok(to_html::compile(
        &events,
        parse_state.bytes,
        &options.compile,
    ))
}

In fact working with events is easier than working with the syntax tree since I can just modify the events I care about without needing to recursively nest a heterogeneous tree (which would either be very vebose or require a lot of macros)

A full plugin system would also solve it but since all the elements I need are already natively supported and I don't need any custom HTML it may not be necessary.

@mousetail
Copy link
Author

I fully understand that just exposing the to_html API won't be enough now, just thinking about what the new simplest way to make it work is

@wooorm
Copy link
Owner

wooorm commented May 1, 2024

I don't think what you want with events is easy.

You really should use an AST. The simplest is to use JavaScript.

@mousetail
Copy link
Author

mousetail commented May 1, 2024

Something like this should work: (Psuedocode)

let mut text = parse_state.bytes.to_vec();
let mut new_events = vec![];

let mut event_iter = events.into_iter();
while let Some(event) = event_iter.next() {
    if let Name::CodeFenced = event.name { // should also check if event.kind == EventKind::Start
       if let Some(Name::CodeFencedFence) = event_iter.next().map(|t|t.name) {
           // loop until you get a code fenced end event
           // check if the name matches your filter
           // if so, push the extra strings necessary to text
       }
    }
    // if anything doesn't match, push all the original events to new_events
}

Ok(to_html::compile(
        &new_events,
        text,
        &options.compile,
    ))

(I'm just arguing for the sake of arguing now, I'll probably just use a different library. Just want to prove it's possible)

@wooorm
Copy link
Owner

wooorm commented May 1, 2024

Everything is possible but many things are bad. The internal events are not meant to be touched. Either make the plugin system as discussed in the mentioned issues. Or use the JS ecosystem. Or even use client side JS. Or use rust HTML rewriter crates.

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

No branches or pull requests

3 participants