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

@diplodoc/latex-extension

v1.3.2

Published

Extension for Diplodoc platform which adds support for Latex syntax using Katex

Downloads

5,777

Readme

Diplodoc Latex extension

NPM version

This is extension for Diplodoc platform which adds support for Latex syntax using katex library.

Extension contains some parts:

Quickstart

Attach plugin to transformer

import latex from '@diplodoc/latex-extension';
import transform from '@diplodoc/transform';

const {result} = await transform(`
### Inline latex

Some text $c = \pm\sqrt{a^2 + b^2}$

### Block latex

$$
c = \pm\sqrt{a^2 + b^2}
$$
`, {
    plugins: [
        latex.transform()
    ]
});

Add latex runtime to your final page

<html>
    <head>
        <link rel="stylesheet" href="_assets/latex-extension.css" />
        <!-- Read more about '_assets/latex-extension.js' in 'MarkdownIt transform plugin' section -->
        <script src="_assets/latex-extension.js" async />
        <script>
            // Read more about 'latexJsonp' in 'Prepared Katex runtime' section
            window.latexJsonp = window.latexJsonp || [];
            window.latexJsonp.push((latex) => {
                window.addEventListener('load', function() {
                    latex.run();
                });
            });
        </script>
    </head>
    <body style="background: #000">
        ${result.html}
    </body>
</html>

Prepared Katex runtime

Katex - is most popular and fast implementation of rendering tex/latex formulas.

The problem with Katex is that it has big bundle size. The most expected behavior is loading it asynchronously.

Prepared Katex runtime designed to solve this problem. We add latexJsonp global callback to handle Katex loading.

Also, we limit exposed Katex API by next methods:

Usage example:

window.latexJsonp = window.latexJsonp || [];

// This callback will be called when runtime is loaded
window.latexJsonp.push((latex) => {
    latex.run();
});

// You can configure more that one callback
window.latexJsonp.push((latex) => {
    console.log('Render diagrams');
});

Run options

  • querySelector - The query selector to use when finding elements to render. (Default: .yfm-latex)

  • nodes - The nodes to render. If this is set, querySelector will be ignored.

  • sanitize - The function to remove dangerous content from final html. (Default: identity)

Security

By default runtime uses Katex security recomendations. But there is extra option for extended sanitize. You feel free to use your own sanitizer:

import dompurify from 'dompurify';
import domready from 'domready';

window.latexJsonp = window.latexJsonp || [];
window.latexJsonp.push((latex) => {
    domready(() => latex.run({
        sanitize: dompurify.sanitize
    }));
});

MarkdownIt transform plugin

Plugin for @diplodoc/transform package.

Configuration:

  • runtime - name of runtime script which will be exposed in results script and style sections. If bundle option was disabled then can be plain string. Otherwise should be string record. Default:

    {
      "script": "_assets/latex-extension.js",
      "style": "_assets/latex-extension.css"
    }
  • bundle - boolean flag to enable/disable copying of bundled runtime to target directory. Where target directory is <transformer output option>/<plugin runtime option> Should be disabled for clientside usage Default: true

  • validate - boolean flag to enable/disable build validation. Useful for serverside prebuild tools like @diplodoc/cli Should be disabled for clientside usage Default: true

  • classes - additional classes which will be added to Latex formula container. (Example: my-own-class and-other-class)

  • katexOptions - katex rendering option

React hook and component for smart control of Katex

Simplifies Katex control with react

import React from 'react'
import { transform } from '@diplodoc/transform'
import latex from '@diplodoc/latex-extension/plugin'
import { LatexRuntime } from '@diplodoc/latex-extension/react'

const LATEX_RUNTIME = 'extension:latex';

const Doc: React.FC = ({ content }) => {
    const result = transform(content, {
      plugins: [
        // Initialize plugin for client/server rendering
        latex.transform({
          // Do not touch file system
          bundle: false,
          // Set custom runtime name for searching in result scripts
          runtime: LATEX_RUNTIME
        })
      ]
    })

    // Load katex only if one or more formulas should be rendered
    if (result.script.includes(LATEX_RUNTIME)) {
      import('@diplodoc/latex-extension/runtime')
    }

    if (result.style.includes(LATEX_RUNTIME)) {
      import('@diplodoc/latex-extension/runtime/styles')
    }

    return <div dangerouslySetInnerHTML={{ __html: result.html }} />
}

export const App: React.FC = ({ theme }) => {
    return <>
        <Doc content={`
            $$
            c = \pm\sqrt{a^2 + b^2}
            $$
        `}/>
        <KatexRuntime />
    </>
}