This PR implements a feature request from Heroku. ✨
Commonmarker allows you to pass a custom renderer. This can be useful in instances where you want to halt the traditional Markdown to HTML conversion and do something else with that information. However, the MarkdownFilter here would not accept a custom renderer.
This PR allows one to provide a custom renderer. A user would need to write their own custom renderer class, and then pass it into the filter as an option:
In the provided test case, CustomRenderer looks like this:
class CustomRenderer < CommonMarker::HtmlRenderer
def header(node)
block do
text = node.first_child.string_content
level = node.header_level
out("{level: #{level}, text: #{text}}")
super
end
end
end
Unfortunately, writing a custom renderer is not very well documented. In essence, every Markdown "type"--links, headers, images, and so forth--can have a method matching that name. The custom renderer will call that method before continuing the regular Markdown-> HTML conversion. For example, here, we:
Define a method called header, which only acts on the headers found in the Markdown document
Takes the contents of that header (node.first_child.string_content)
Takes the header level (node.header_level)
Writes back some information back out to the Markdown string ({level: #{level}, text: #{text}})
This PR implements a feature request from Heroku. ✨
Commonmarker allows you to pass a custom renderer. This can be useful in instances where you want to halt the traditional Markdown to HTML conversion and do something else with that information. However, the MarkdownFilter here would not accept a custom renderer.
This PR allows one to provide a custom renderer. A user would need to write their own custom renderer class, and then pass it into the filter as an option:
In the provided test case,
CustomRenderer
looks like this:Unfortunately, writing a custom renderer is not very well documented. In essence, every Markdown "type"--links, headers, images, and so forth--can have a method matching that name. The custom renderer will call that method before continuing the regular Markdown-> HTML conversion. For example, here, we:
header
, which only acts on the headers found in the Markdown documentnode.first_child.string_content
)node.header_level
){level: #{level}, text: #{text}}
)super
)For a file that looks like this:
The following HTML would be produced:
/cc @hannesfostie do let me know if this suffices!