If <Markdown> is awesome, it goes without saying that I’d also use it in frameworks! So I went ahead and built one for Svelte as well, since that’s the UI framework I use with Astro.
You can also build your own Markdown component easily if you use React, Vue, or other frameworks. I’ll show you how in this lesson.
Markdown component in Svelte
Using the Markdown component in Svelte is easy. You just have to import that component and write your markdown.
The only difference here is we can’t write the content in a slot. We have to use a content prop.
<script> import { Markdown } from '@splendidlabz/svelte'</script>
<Markdown content=`...` />This is a limitation because Svelte does not expose the contents of a slot to a component until they are rendered.
Multiline content
Like the Astro counterpart, you don’t have to worry about indentation because the Markdown component takes care of it.
<script> import { Markdown } from '@splendidlabz/svelte'</script>
<div> <div> <Markdown content=" First paragraph
Second paragraph " /> </div></div><div> <div> <p>First paragraph </p> <p>Second paragraph </p> </div></div>Inline content
The Svelte Markdown component also has an inline version — just add the inline property to use it.
<h2> <Markdown inline content="Ain't this cool?" /></h2><h2> Ain’t this cool?</h2>Building your own Markdown
Both the Astro and Svelte Markdown components use the markdown utility I’ve exposed in Splendid Utils.
So you can just grab that utility and put it into your framework of choice.
Here are some examples for React and Vue. Note that these examples are created by AI — I haven’t tested them out because I don’t use these frameworks.
React:
import { markdown } from '@splendidlabz/utils'import { useMemo } from 'react'
export function Markdown({ content, inline = false }) { const html = useMemo(() => markdown(content, { inline }), [content, inline]) return <div dangerouslySetInnerHTML={{ __html: html }} />}Vue:
<script setup> import { computed } from 'vue' import { markdown } from '@splendidlabz/utils'
const props = defineProps({ content: { type: String, required: true }, inline: { type: Boolean, default: false }, })
const html = computed(() => markdown(props.content, { inline: props.inline }))</script>
<template> <div v-html="html" /></template>Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson