ssssota / svelte-exmarkdown

Svelte component to render markdown.
https://ssssota.github.io/svelte-exmarkdown
MIT License
199 stars 8 forks source link

Adding custom components #36

Closed heyngra closed 1 year ago

heyngra commented 1 year ago

Hey, I have a question, because I tried to implement it, but I can't figure it out. Is there any way, to add custom components to the svelte-exmarkdown?

Lets imagine we have h1 text called "Test". You would type it normally as

# Test

But is there a way to f.e add a button below, that acts like a svelte component, from the text itself? For example

# Test
<CustomButton/>

(So the user can type this and server can import these components if necessary)

ssssota commented 1 year ago

Thanks for your question!

You can implement like this: https://stackblitz.com/edit/svelte-exmarkdown-custom-component?file=src%2FApp.svelte

  1. use rehype-raw module as rehypePlugin.
  2. create your custom component.
  3. create bridge component.

Warning There are a few things to keep in mind.

  • When setting up a renderer for a plugin, the keys must be all lowercase
  • Component properties must be lowercased as well
  • You cannot use self-closing syntax like <CustomButton/>
heyngra commented 1 year ago

Will check later on if it works fine, if so, then I'll close this issue! Thanks a lot!