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

Multiple markdowns cause identifier problems #117

Open
yigitguler opened this issue Mar 19, 2020 · 1 comment
Open

Multiple markdowns cause identifier problems #117

yigitguler opened this issue Mar 19, 2020 · 1 comment

Comments

@yigitguler
Copy link

Hi,

We are displaying multiple markdown texts on a single page. Everything worked fine, except when we tried to use Codehilite's tabbed view.

Since the ids of the Codehilite collide with each other, the JS code doesn't work. For example, the id of the code tabs are the same for 2 markdown areas md-fenced-code-tabs

Is there a way to render elements with a prefix?

@some1ataplace
Copy link

some1ataplace commented Apr 5, 2023

Yes, you can render elements with a prefix to avoid id collisions. Here's an example of how you can modify the markdownify function in django-markdown-editor to add a prefix to the ids of Codehilite's tabbed view:

from markdownx.utils import markdownify as original_markdownify

def markdownify(markdown_text, prefix=''):
    html_text = original_markdownify(markdown_text)
    if prefix:
        html_text = html_text.replace('id="md-fenced-code-tabs', 'id="{}-md-fenced-code-tabs'.format(prefix))
        html_text = html_text.replace('href="#md-fenced-code-tabs', 'href="#{}-md-fenced-code-tabs'.format(prefix))
    return html_text

In the above code, we first import the original markdownify function from markdownx.utils. Then, we define a new markdownify function that takes an optional prefix argument.

Inside the function, we call the original markdownify function to convert the markdown text to HTML. Then, if a prefix is provided, we use string replacement to add the prefix to the ids and hrefs of Codehilite's tabbed view.

For example, if you call markdownify(markdown_text, prefix='my-prefix'), the ids and hrefs of Codehilite's tabbed view will be rendered as id="my-prefix-md-fenced-code-tabs" and href="#my-prefix-md-fenced-code-tabs", respectively.

You can use this modified markdownify function in your Django views to render multiple markdown texts with unique ids.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants