Custom action on code fences but keep the normal behavior #1291
-
Hi. I want to validate code blocks in my MkDocs site (e.g. by giving them to an external process). The validation would basically have the ability to abort building the site. A further ability that I'd like to have is to modify the code snippet before passing it onwards. Other than that, there should be no changes to what code fences do, and normal syntax highlighting should continue to work. So I started looking, how do I get the content (and class) of code fences?
Oh wait, "custom fences" is the right feature!
Other options? Basically my big issue with the supported approach is that there's no good way to still use the default Or, well, going back to the overall problem, I basically just want a hook to be called on every fenced code block, without abandoning the highlighting behavior. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Yeah, you can use custom fences and call into the default formatter. I basically do that in this project: https://github.com/facelessuser/coloraide/blob/master/tools/colors.py#L210. Basically, the above code is allowing me to parse raw Python code and get it's results, then I format it as a ```color
Color("rebeccapurple").interpolate(
"lch(85% 100 85)",
space='lch'
)
``` Generally, it is assumed that if someone is overriding this, they don't want to do pygments parsing, or they are going to implement their own; otherwise, they would normally just use the default highlighter. But yeah, I ran into a situation where I wanted to do much more, but still, leverage the highlighter. Custom fence usage is one of those things that has kind of evolved over time. It's one of those things, that given time, I push a bit further. |
Beta Was this translation helpful? Give feedback.
Yeah, you can use custom fences and call into the default formatter. I basically do that in this project: https://github.com/facelessuser/coloraide/blob/master/tools/colors.py#L210.
Basically, the above code is allowing me to parse raw Python code and get it's results, then I format it as a
pycon
input/output, send it through the highlighter and also about color previews:Generally, it is assumed that if someone is overriding this, they don't want to do pygments parsing, or they are going to implement their own; otherwise, they would normally just use the default highlighter. But yeah, I ran into a …