Add ability to pass custom renderer to MarkdownFilter #318
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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!