Best way to override HtmlFormatter #454
Replies: 3 comments 3 replies
-
Rust has no inheritance, so we'd need to take a different approach. (I briefly started to think about this in #442 (comment).) The problem largely is in allowing some methods to be overridden while providing defaults for all, without pessimising the default case (and therefore Comrak for the large proportion of use cases). One way would be to separate each node type's formatting code into its own ( Another approach would be to construct a formatting function with a macro, allowing overrides to be provided for node types. This would shift the burden of custom renderers to compile-time. There's also the middleware approach I mentioned in that earlier discussion; that's perhaps simplest of all, though we'd need to see what any of these looked like in implementation to know for sure. |
Beta Was this translation helpful? Give feedback.
-
Not sure which of these would be better. I'm not quite understanding the middleware approach yet. If each node has it's own render function, I was naively thinking you could put a feature flag around each function such that if that flag is turned on when the crate is complied, the crate version doesn't get compiled and instead the one in the enclosing crate gets used. But even if that were possible, seems more like a hack than anything. |
Beta Was this translation helpful? Give feedback.
-
Ran across https://github.com/Shizcow/overrider-rs, which looks interesting and uses macros. But it hasn't been updated in 4 years. Kinda (I think) fits with your comment
Don't know if there is anything to reuse here or not. Edit: just throwing some ideas / resources out here as I keep investigating. |
Beta Was this translation helpful? Give feedback.
-
So I'm reaching a point where I want to override some aspects of the
HtmlFormatter
. For example, I plan on using thegemoji
option to handle shortcodes, rather than our own specific code. However one of the things we do is wrap any such emoji with<gl-emoji>
with additional attributes.I believe the
gemoji
crate can give us all that information, so in my head it makes sense to write it out during rendering rather modifying a parsed nokogiri tree later. Even if it doesn't, wrapping it in our tag would allow us to pluck them out of the tree more reliably.So my question is what would be the best way to replace just the handling of that node? I really don't want to duplicate that file.
What I think I want is to rewrite
format_node
such that for each node it calls a separate function. I actually handled theNodeValue::Math
node in that way, callingself.render_math_inline
.Then my goal would be to have a
GlHtmlFormatter
that inherits fromHtmlFormatter
and only overrides the methods I want.Knowing much more Ruby than Rust, would this be possible, and would you support such a rewrite of
format_node
?Beta Was this translation helpful? Give feedback.
All reactions