npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@elron/svelte-wp-shortcode

v1.0.6

Published

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

Downloads

32

Readme

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.