Effortlessly blend Svelte components into content with WordPress-inspired shortcodes. Simplify dynamic embedding with a familiar touch.
<slot>
mechanism. It also supports export let slot
if you need the slot content as a variable.# bun
bun install @elron/svelte-wp-shortcode@latest
# pnpm
pnpm install @elron/svelte-wp-shortcode@latest
# npm
npm install @elron/svelte-wp-shortcode@latest
# Yarn
yarn install @elron/svelte-wp-shortcode@latest
// Import Your Own Custom Svelte Components import TryButton from './TryButton.svelte'; import Note from './Note.svelte'; import Youtube from './Youtube.svelte'; import Strong from './Strong.svelte';
2. **Use the shortcodes in your content**:
```svelte
<WpShortcodes
markup={`
Simple embed: [try-button]
Text within: [note]Here's some inner text![/note]
With properties: [youtube id=EVP1NQAnpYk]
All combined: [strong color="red" class="text-xl"]Awesome, right?[/strong]
With HTML: <i>Still works!<i>
`}
components={{
'try-button': TryButton,
'note': Note,
'youtube': Youtube,
'strong': Strong
}}
/>
Voilà! The <TryButton>
, <Note>
, <Youtube>
, and <Strong>
Svelte components are rendered based on their respective shortcode types. Obviously, you can replace them with your own components.
[example message="hey"]
or no quotes at all [example message=hey]
. Single quotes won't work.slot
is reserved so you can use export let slot
if needed (<slot />
is supported as well). Example: If this is the shortcode [shortcode slot="Initial Content"]Overridden Content[/shortcode]
then export let slot
the slot
value will become Overriden Content
.There is a known svelte issue - {@html} tag is broken during hydration #8213 - that causes the @html code to render badly (duplicates itself).
If you encounter an issue, there are two known workarounds for now.
{#key mounted}
wrapperThis workaround still supports SSR (good for SEO).
<script lang="ts">
import WpShortcodes from '@elron/svelte-wp-shortcode';
import TryButton from './WpShortcodes/TryButton.svelte';
export let markup: string;
let components = { 'try-button': TryButton };
onMount(() => {
mounted = true;
});
</script>
{#key mounted}
<WpShortcodes {components} {markup} />
{/key}
+page.ts
file:This will disable SSR (bad for SEO).
export const csr = false;
If you've found another workaround or a fix, please share in the repo, would love your contribution!
Your input is valued! Share your feedback, report bugs, or make pull requests on our GitHub repository.
This project is licensed under the MIT License - see the LICENSE.md file for details.