svelte-exmarkdown

repository·main·Indexed 43 days ago

https://github.com/ssssota/svelte-exmarkdown

A dynamic and extensible Svelte component for runtime markdown rendering. Built on the unified ecosystem, it supports a pluggable architecture with remark and rehype plugins, allowing for custom component mapping, GFM support via gfmPlugin, and advanced integrations like Shiki, KaTeX, and rehype-highlight. Version 5.0.3.

Tokens
4.3K
Snippets
27
Records
30
Agent score
54%

What's inside svelte-exmarkdown

  1. How the rendering flow works in svelte-exmarkdown

    main

    The library is built on top of the unified ecosystem. The rendering process follows this pipeline:

    1. Markdown String is parsed by remark-parse into an mdast (Markdown Abstract Syntax Tree).
    2. remarkPlugins process the mdast.
    3. remark-rehype converts the mdast into a hast (Hypertext Abstract Syntax Tree).
    4. rehypePlugins process the hast.
    5. hast-component-mapping maps the hast nodes to specific SvelteComponents for final rendering.

    Because it uses unified, you can use any standard remark or rehype plugins.

  2. Customize Markdown rendering via the renderer object

    main
    You can control how specific Markdown elements are rendered by providing a renderer object. If you map a Markdown element (represented by its HTML tag name) to a specific string value in the renderer object, the component will render that exact HTML tag name instead of its default behavior.
  3. Control element rendering with allowlist and denylist

    main

    You can control which Markdown elements are permitted to render using the allowlist and denylist props.

    • denylist: A list of element names that should be explicitly excluded from rendering.
    • allowlist: A list of element names that are permitted to render; any element not in this list will be skipped.
  4. Customize the renderer with snippets

    main

    You can customize how the markdown is rendered by providing snippets. This allows you to override default rendering behavior for specific elements. Additionally, you can apply custom class names and styles to the rendered output.

    <ExMarkdown>
      <svelte:fragment slot="snippet">
        <!-- Custom snippet implementation here -->
      </svelte:fragment>
    </ExMarkdown>
  5. Use the Transparent component to render without a wrapper element

    main

    The Transparent component allows you to render content without adding an extra HTML wrapper element to the DOM. This is particularly useful when you need to disable specific Markdown syntax or when you want to maintain a specific DOM structure that doesn't allow for additional nesting.

    :transparent
  6. Use Shiki for code highlighting

    main

    You can use the Shiki syntax highlighter within svelte-exmarkdown by using specific shorthand directives.

    By default, Shiki is used asynchronously. If your environment requires synchronous execution, use the -sync variant.

    :shiki
    
    Or for synchronous usage:
    
    :shiki-sync
  7. Skip rendering an element using the renderer prop

    main

    To prevent a specific Markdown element from being rendered in the DOM, pass null to the renderer prop of the component. This is useful when you want to intercept the rendering process or completely suppress certain elements.

    <ExMarkdownRenderer {markdown} renderer={{ someElement: null }} />
  8. Render math formulas using remark-math and rehype-katex

    main

    To render mathematical formulas in your markdown content using svelte-exmarkdown, you need to integrate the remark-math and rehype-katex plugins. remark-math handles the parsing of math syntax, while rehype-katex converts that syntax into HTML using KaTeX. Alternatively, you can use rehype-mathjax as a substitute for rehype-katex if you prefer MathJax rendering.

    # You will need to install these plugins
    npm install remark-math rehype-katex
    # Or alternatively
    npm install remark-math rehype-mathjax
  9. Enable raw HTML support using rehype-raw

    main

    By default, svelte-exmarkdown does not allow raw HTML in markdown input to prevent security vulnerabilities. If you trust your input source and need to render raw HTML, you can enable this functionality by using the rehype-raw plugin in your configuration.

    // Example configuration concept
    // Note: Actual implementation depends on how you pass plugins to svelte-exmarkdown
    import rehypeRaw from 'rehype-raw';
    
    // Use rehypeRaw in your plugin array to allow raw HTML
  10. Basic usage of svelte-exmarkdown

    main

    Use the Markdown component to render markdown content dynamically. It accepts an md prop containing the markdown string. This component is designed for dynamic rendering scenarios where the markdown content might change at runtime (e.g., via a textarea binding).

    <script>
    	import Markdown from 'svelte-exmarkdown';
    	let md = $state('# Hello world!');
    </script>
    
    <textarea bind:value={md}></textarea>
    <Markdown {md} />