svelte-markdown icon indicating copy to clipboard operation
svelte-markdown copied to clipboard

Need Documentation on Markdown Custom Styling

Open SilasReinagel opened this issue 3 years ago • 4 comments

It's not clear how to provide custom styling for the generated markdown.

SilasReinagel avatar Jan 04 '22 04:01 SilasReinagel

My solution was to use global.

<!-- a source that includes a h1 -->
<SvelteMarkdown {source}></SvelteMarkdown>

<style>
  :global(h1) {
    color: red;
  }
</style>

magnetenstad avatar Jan 09 '22 20:01 magnetenstad

I'll add this to the README. But the approach is either what @magnetenstad mentioned, or using your own custom renderer for the element you want. E.g. if you want to style paragraphs you'd create your renderer:

<!-- Paragraph.svelte -->
<p><slot /></p>

<style>
  p {
    color: red;
  }
</style>

And then pass it to SvelteMarkdown using the renderers prop:

<SvelteMarkdown source={source} renderers={{ paragraph: Paragraph }} />

You can use the default renderers as reference.

pablo-abc avatar Jan 11 '22 00:01 pablo-abc

@pablo-abc Perfect! Thank you for the guidance on that.

SilasReinagel avatar Jan 11 '22 16:01 SilasReinagel

Adding here in-case someone stumbles upon this issue and wants a quick solution. It's exactly the same as solution by @magnetenstad but keeps the style local to current element.

// Required: id should be unique here
<div id="markdown-container">
	<SvelteMarkdown source={tokens} />
</div>

<style>
        // Similarly you can style other elements
	:global(#markdown-container  h1) {
		color: red;
	}
</style>

nkitsaini avatar Nov 18 '22 16:11 nkitsaini