lepture/mistune

HTMLRenderer's heading cannot be customised when DirectiveToc is used

Closed this issue · 4 comments

Rhahi commented

using version 2.0.0a4,
HTMLRenderer's heading cannot be customised when DirectiveToc is used
for example, running the code below will result in two different behaviours.

import mistune
import mistune.directives import DirectiveToc

class Renderer(mistune.HTMLRenderer):
    def heading(self, text, level):
        return 'changed_heading'

test_a = mistune.create_markdown(
        renderer=Renderer(),
        plugins=[])
test_b = mistune.create_markdown(
        renderer=Renderer(),
        plugins=[DirectiveToc()])

print(test_a("# text")) #returns 'changed_heading'
print(test_b("# text")) #returns '<h1 id="toc_1">text</h1>'

This is because when DirectiveToc is used, the rendering process uses render_html_threading (from mistune.directives.toc.py) instead.
This is not necessarily an issue, since it's impossible to handle arbitrary changes in the renderer while trying to put header ids in the html. Maybe this behaviour should be documented so that people can find which method to override when using DirectiveToc.

Try to customize with:

def theading(self, text, level, tid):
    return 'changed toc heading'

@lepture add this to the documentation because I was about to change to another markdown engine before seeing this issue.

@mauro-balades thanks, I've added it to documentation.

I think the documentation might be slightly incorrect, as the function needs to be called theading rather than heading.