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

rehype-katex-svelte

v1.2.0

Published

rehype plugin to transform .math-inline, .math-display with KaTeX to Svelte markup

Downloads

1,399

Readme

rehype-katex-svelte

rehype plugin to transform .math-inline and .math-display elements with KaTeX into Svelte-friendly markup nodes.

This plugin is functionally equivalent to rehype-katex but is specifically intended to be used with mdsvex (markdown format for Svelte).

The key issue it addresses is that LaTeX curly braces (e.g. {x}) conflict with Svelte's template syntax; as such, using rehype-katex to serialize a LaTeX expression such as \frac{x}{y} would result in "x is not defined" and "y is not defined" errors. This plugin fixes those errors by rendering KaTeX content via {@html "..."} instead of plain HTML nodes (which is what rehype-katex does), preventing curly-brace content from getting treated as Svelte template expressions.

Usage

To use rehype-katex-svelte with mdsvex, you need to import rehype-katex-svelte and remark-math and add both to mdsvex's config:

Note: mdsvex uses an old remark version so you need [email protected]

npm install -D [email protected]
import rehypeKatexSvelte from "rehype-katex-svelte";
import remarkMath from 'remark-math'

mdsvex({
  remarkPlugins: [
    remarkMath,
  ],
  rehypePlugins: [
    rehypeKatexSvelte,
    /* other rehype plugins... */
  ],
  /* other mdsvex config options... */
});

Options passed to the rehype-katex-svelte plugin are relayed directly to KaTeX:

mdsvex({
  rehypePlugins: [
    [
      rehypeKatexSvelte,
      {
        macros: {
          "\\CC": "\\mathbb{C}",
          "\\vec": "\\mathbf",
        },
      },
    ],
    /* etc. */
  ],
  /* etc. */
});

Then you start writing maths in your .svx files

<!-- blog-post.svx -->
<!-- inline -->
$f(x) = x^2$
<!-- block -->
$$
f(x) = x^2
$$

You might also want to add katex.css somewhere to style the maths properly

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" integrity="sha384-AfEj0r4/OFrOo5t7NnNe46zW/tFgW6x/bCJG8FqQCEo3+Aro6EYUG4+cU+KJWu/X" crossorigin="anonymous">

This plugin is not really intended to be used directly with the rehype API, but nothing stops you from doing so if that's what gets you going:

rehype().use(rehypeKatexSvelte[, katexOptions])

Caveats

  • I didn't bother implementing some of rehype-katex's other features, e.g. options.throwOnError, because I didn't personally find a need for it.

  • My code might not be following rehype "best practices"—this is literally my first attempt at a rehype plugin. If you'd like to help me improve my code, by all means go ahead! (Please open a PR.)

Security

I haven't given any thought to sanitizing inputs & protecting against XSS, so beware! Make sure you only use rehype-katex-svelte on inputs you trust, i.e. your own source code.

If you care about improving the security of this plugin, please open a PR, and I'd be happy to merge it!

Contributing

Feel free to open an issue, make a PR, email me, whatever. Code of conduct: don't be a jerk.

License

GPLv3 @ Kye Shi