valeriangalliat / markdown-it-anchor

A markdown-it plugin that adds an `id` attribute to headings and optionally permalinks.
The Unlicense
287 stars 71 forks source link

more icons for "symbol" #130

Open xarthurx opened 3 months ago

xarthurx commented 3 months ago

Hi, I'm using a lot of icons from Iconify and would also like to use a different icons for the symbol.

Usually when you use such an icon in a html, you can just define a class="i-xxxx-xxx" and with a correct setting of css framework, it will work.

You can check examples here: https://unocss.dev/presets/icons

However, when I try to do such things with markdown-it-anchor:

      permalink: mdAnchor.permalink.linkInsideHeader({
        symbol: ` 
            <div class="i-ph-anchor-simple-thin" />
            <div class="i-material-symbols-link" />
            @
        `,
        placement: "before",
      }),

it seems the code will keep adding nested DOM and doesn't render as an icons...

image image

Is there a way to make it work?

valeriangalliat commented 3 months ago

Hi! It seems that:

<div class="i-ph-anchor-simple-thin" />
<div class="i-material-symbols-link" />
@

Is equivalent do doing

<div class="i-ph-anchor-simple-thin">
<div class="i-material-symbols-link">
@
<!-- rest of the content -->
</div>
</div>

Which doesn't sounds like it is what you want

In a React JSX context, <div /> would do what you want, maybe the documentation you got that code from was targeted at JSX?

In the case of vanilla HTML you want an explicit closing tag:

<div class="i-ph-anchor-simple-thin"></div>
<div class="i-material-symbols-link"></div>
@

Cheers

xarthurx commented 3 months ago

Thx. The HTML grammar issue got resolved by your method.

There's another problem, however, is a bit interesting.

I guess the reason is when only replace the symbol part, the "replacing" process happens after the css engine parse the page and got triggered. So even the html code got replaced/injected, it actually doesn't trigger the css engine -- as a result, the code doesn't take effect...

valeriangalliat commented 3 months ago

That's gonna depend a lot on your build system but it sounds like you have a preprocessing tool that includes the CSS for the icons that it detects in your HTML, but it doesn't account for the ones in the Markdown rendered content

You're correct you would need to make it detect the icons after the Markdown is done processing for this to work 👍