Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

anchors_plugin callback #7

Open
tylerlozano opened this issue Dec 27, 2020 · 3 comments
Open

anchors_plugin callback #7

tylerlozano opened this issue Dec 27, 2020 · 3 comments
Labels
enhancement New feature or request

Comments

@tylerlozano
Copy link

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.

@tylerlozano tylerlozano added the enhancement New feature or request label Dec 27, 2020
@welcome
Copy link

welcome bot commented Dec 27, 2020

Thanks for opening your first issue here! Engagement like this is essential for open source projects! 🤗

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! 🎉

@tylerlozano
Copy link
Author

tylerlozano commented Dec 28, 2020

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

@chrisjsewell
Copy link
Member

hey yeh thanks I'll try to have a look soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants