cmark icon indicating copy to clipboard operation
cmark copied to clipboard

Feature: Rendering hooks

Open sciurius opened this issue 4 years ago • 3 comments

GoldMark has a nice feature: you can define your own renderers for specific items, such as links and headings.

See e.g. how Hugo uses this: https://gist.github.com/killbus/f47efbd13d96b437b9d218334508bffb

It would be nice if cmark could do something similar.

sciurius avatar Jul 16 '20 11:07 sciurius

IF you're using cmark as a library, you can do this by walking the AST and converting CMARK_NODE_HEADING blocks to CMARK_CUSTOM blocks. A custom block has an "on enter" and "on exit" text which you can construct from the heading AST element; this will be rendered verbatim in HTML output.

If you're using the command line program, then you might try my lcmark, a small lua wrapper which adds 'filters' that can do what I've just described.

jgm avatar Jul 16 '20 16:07 jgm

I'm using the Perl bindings (CPAN module CommonMark). I can process the parsed document using the iterator. Until now I haven't been able to modify (replace) the NODE_LINK node by a NODE_CUSTOM_BLOCK.

It's all very new...

UPDATE: Got it working! Thanks for the crucial pointers.

sciurius avatar Jul 16 '20 19:07 sciurius

FYI: https://github.com/sciurius/perl-CommonMark-Massage

sciurius avatar Jul 17 '20 08:07 sciurius