mdit-py-plugins
mdit-py-plugins copied to clipboard
anchors_plugin callback
I want a list of the title, unique slug-id pairs for my generated anchors so I can create a navbar linking to the content. In MarkdownIt for JS a callback exists with this data:
"The callback option is a function that will be called at the end of rendering with the token and an info object. The info object has title and slug properties with the token content and the slug used for the identifier."
How can this be done with the current plugin?
I thought about a custom parsing function for heading_open token, but how to get inline after?
I considering passing a custom slug_func to anchors_plugin, but it doesn't have access to the slugs param so it can not retrieve the unique_slug id for the heading.
I tried parsing first, then rendering, but the plugin maintains state and so the unique_slug ids in the token stream don't match what is rendered.
Thanks for opening your first issue here! Engagement like this is essential for open source projects! :hugs:
If you haven't done so already, check out EBP's Code of Conduct. Also, please try to follow the issue template as it helps other community members to contribute more effectively.
If your issue is a feature request, others may react to it, to raise its prominence (see Feature Voting).
Welcome to the EBP community! :tada:
I don't know if this is the ideal way to do this, but my current solution is:
class MyRenderer(RendererHTML):
def __init__(self, *args, **kwargs):
self.fm = None
self.pairs = []
super().__init__(*args, **kwargs)
def front_matter(self, tokens, idx, options, env):
token = tokens[idx]
front_matter = token.content.splitlines()
self.fm = front_matter
return self.renderToken(tokens, idx, options, env)
def heading_open(self, tokens, idx, options, env):
token = tokens[idx]
if token.attrs:
_id = token.attrs[0][1]
token = tokens[idx + 1]
title = token.content
self.pairs.append((title, _id))
return self.renderToken(tokens, idx, options, env)
md = (
MarkdownIt("default", renderer_cls=MyRenderer)...
md.renderer.pairs
hey yeh thanks I'll try to have a look soon