@elron/svelte-wp-shortcode
TypeScript icon, indicating that this package has built-in type declarations

1.0.6 • Public • Published

Svelte WP Shortcodes

Effortlessly blend Svelte components into content with WordPress-inspired shortcodes. Simplify dynamic embedding with a familiar touch.

Demo

🚀 Features

  • 📌 Dynamic Embeds: Seamlessly integrate Svelte components into static content.
  • 🔄 Props Transfer: Directly pass shortcode attributes as Svelte props.
  • ✒️ Versatile Syntax: Supports both self-closing ([component]) and pair tags ([component]...[/component]), allowing you to write shortcodes with or without quotes around attribute values ([youtube id="123"] or [youtube id=123]).
  • 🎟️ Slot Support: Easily pass content between opening and closing shortcodes, which gets rendered using Svelte's <slot> mechanism. It also supports export let slot if you need the slot content as a variable.
  • 🧱 HTML Compatibility: Seamless integration with HTML content! the library won't interfere with your HTML structure.
  • 🌐 SSR Compatibility: Ready for SvelteKit's server-side rendering.
  • 🛠️ Custom Components: Fully adaptable with your custom Svelte components for personalized designs.
  • 📦 Unlimited Shortcodes: Embed any number of Svelte components or shortcodes without restrictions.
  • 📜 Multi-Line Support: Captures and processes shortcodes with content that spans multiple lines, ensuring a flexible and forgiving user experience.

📦 Installation

# pnpm
pnpm install @elron/svelte-wp-shortcode@latest

# npm
npm install @elron/svelte-wp-shortcode@latest

# Yarn
yarn install @elron/svelte-wp-shortcode@latest

🛠️ Quick Start

  1. Import the Shortcodes component and your custom Svelte components:
import WpShortcodes from '@elron/svelte-wp-shortcode';

// 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';
  1. Use the shortcodes in your content:
<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.

Notes

  • For the shortcode attributes, you must use double quotes [example message="hey"] or no quotes at all [example message=hey]. Single quotes won't work.
  • The library does not support for nested shortcodes. It's optimized for simpler use cases, but deeper nesting or intricate scenarios might require special attention.
  • The attribute 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.

⚠️ Warning!

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.

Workaround #1 is to use {#key mounted} wrapper

This 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}

Workaround #2 is to disable CSR in SvelteKit, in the +page.ts file:

This will disable SSR (bad for SEO).

export const csr = false;

Found a solution? Share it.

If you've found another workaround or a fix, please share in the repo, would love your contribution!

❤️ Contributing

Your input is valued! Share your feedback, report bugs, or make pull requests on our GitHub repository.

📜 License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.0.611latest

Version History

VersionDownloads (Last 7 Days)Published
1.0.611
1.0.50
1.0.40
1.0.30
1.0.20
1.0.10
1.0.00
0.1.60
0.1.50
0.1.40
0.1.30
0.1.20
0.1.10
0.1.00
0.0.90
0.0.80
0.0.70
0.0.60
0.0.51
0.0.30
0.0.10

Package Sidebar

Install

npm i @elron/svelte-wp-shortcode

Weekly Downloads

12

Version

1.0.6

License

MIT

Unpacked Size

9.08 kB

Total Files

6

Last publish

Collaborators

  • elron