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

How to preprocess markdown? #85

Closed
GildedHonour opened this issue Jul 23, 2018 · 9 comments
Closed

How to preprocess markdown? #85

GildedHonour opened this issue Jul 23, 2018 · 9 comments

Comments

@GildedHonour
Copy link

Say, I have a tag audio, video, image or whatever and I want to dynamically adjust it's "source" before rendering it on an html page. Does comrak provide an api for an element to do so?

@kivikakk
Copy link
Owner

You can walk the returned AST until you find the InlineHtml or BlockHtml representing the tag in question. It doesn't parse HTML — that's a non-goal of CommonMark — so you'll still have quite a bit of work to do.

@GildedHonour
Copy link
Author

thx

does the library support <audio ... and <video markdown tags?

@kivikakk
Copy link
Owner

No problem!

And unfortunately not; there’s no syntax in CommonMark for those tags, nor any extension implemented.

@GildedHonour
Copy link
Author

GildedHonour commented Jul 26, 2018

If I want to extend it for myself, something like this:

[audio](/path/file.mp4)
which will be parsed into the real html audio tag , how will I add that tag? Is there a similar example in the library?

@brson
Copy link
Collaborator

brson commented Jul 26, 2018

@GildedHonour my downstream branch does something like that, commandeering the image syntax like ![video](<hashcode>) (we use hash identifiers instead of paths. I can't share the full code off hand, but here's a snippet:

https://gist.github.com/brson/b180839aa4a236bc78e7b86eca3f7877

I'm not saying this is the cleanest way to do it, but it's what we do - note that when we're in our custom mode, indicated by the 'rtjson' flag - we use a totally new AST node, NodeMeda, to indicate it's special. Elsewhere we have further validation that the text of that node is 'video', 'image', 'gif', etc. - Depending on your needs you might check for those 'keywords' right there to make the decision to use the custom node.

From there you just have to edit html.rs, which should be pretty obvious since it's a small repetitive file.

Hope that helps.

@GildedHonour
Copy link
Author

@brson Thanks. But I can't understand how that can be useful for my case because that's quite a small part of code. Can you share more?

Also, there's "}" missing in the very end, or somewhere else.

@brson
Copy link
Collaborator

brson commented Aug 21, 2018

@GildedHonour sorry for the lack of detail. I meant to give a hint of a reasonable place to reinterpret the link syntax. The rest I'd expect to fall out of that via compiler errors.

In close_bracket_match (the function to finish a successfully parsed link) you can use logic to, instead of creating a NodeLink, create your own custom node (like my NodeMedia), perhaps when url equals "audio". You'll need to add the new node in nodes.rs. From there you can extend the renderer in html.rs to emit the audio tag (and maybe the one in cm.rs). That's about all that's needed, afaicr offhand.

Another way to do it is to have an AST post-processing pass that walks the AST and looks for NodeLink and changes them as appropriate to NodeWhatever. Cleaner but slower.

@GildedHonour
Copy link
Author

@brson yes, I can. That's why I asked for more code.

@kivikakk
Copy link
Owner

I'll close this issue out now; thanks @brson for dropping in with pointers from your own work!

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