Skip to content

Instantly share code, notes, and snippets.

@simonw
Created September 21, 2024 02:29
Show Gist options
  • Save simonw/a6c23ba1c95613d41b98f432f273dd85 to your computer and use it in GitHub Desktop.
Save simonw/a6c23ba1c95613d41b98f432f273dd85 to your computer and use it in GitHub Desktop.

Client-side JavaScript Markdown Libraries with Math Rendering

human (Sep 20, 2024, 07:20 PM)

Are there any client side JavaScript markdown libraries that can also handle inline math and render it?

assistant (Sep 20, 2024, 07:20 PM)

Yes, there are several client-side JavaScript libraries that can handle both Markdown and inline math rendering. Here are a few popular options:

  1. Marked + KaTeX:

    • Marked is a fast Markdown parser and compiler.
    • KaTeX is a fast math typesetting library for the web.
    • You can use these together to render both Markdown and math.
  2. MathJax + a Markdown library:

    • MathJax is a powerful and flexible math rendering library.
    • It can be combined with various Markdown libraries like Marked or Showdown.
  3. markdown-it + markdown-it-katex:

    • markdown-it is a Markdown parser with plugin support.
    • markdown-it-katex is a plugin that adds KaTeX math rendering to markdown-it.
  4. remark + rehype-katex:

    • remark is a Markdown processor powered by plugins.
    • rehype-katex is a plugin to render math with KaTeX.
  5. Unified.js ecosystem:

    • A set of tools for parsing, transforming, and rendering content, including Markdown and math.
  6. HyperMD:

    • An editor library that supports both Markdown and LaTeX math rendering.

Would you like more details on any of these options or an example of how to use one?

human (Sep 20, 2024, 07:21 PM)

Build an artifact that demonstrates Marked plus KaTeX - it should include a text area I can enter markdown in (repopulated with a good example) and live update the rendered version below. No react.

assistant (Sep 20, 2024, 07:21 PM)

Certainly! I'll create an HTML artifact that demonstrates the use of Marked and KaTeX to render Markdown and math in real-time. This will include a textarea for input and a live-updating rendered version below.

Markdown and Math Live Renderer

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Markdown and Math Live Renderer</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/marked/4.0.2/marked.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.15.3/katex.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.15.3/contrib/auto-render.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.15.3/katex.min.css">
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        textarea {
            width: 100%;
            height: 200px;
            margin-bottom: 20px;
        }
        #output {
            border: 1px solid #ddd;
            padding: 10px;
            background-color: #f9f9f9;
        }
    </style>
</head>
<body>
    <h1>Markdown and Math Live Renderer</h1>
    <textarea id="input" placeholder="Enter your Markdown and LaTeX here...">
# Welcome to the Markdown and Math Renderer!

This example demonstrates how to use **Markdown** with inline and block *math* equations.

## Inline Math

The quadratic formula is $ax^2 + bx + c = 0$. Its solution is given by:

## Block Math

$$x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$$

## List with Math

1. First item
2. Second item with inline math: $E = mc^2$
3. Third item

## Table with Math

| Operation | Symbol | Example |
|-----------|--------|---------|
| Addition  | $+$    | $2 + 2 = 4$ |
| Subtraction | $-$  | $5 - 3 = 2$ |
| Multiplication | $\times$ | $3 \times 4 = 12$ |

Enjoy rendering your Markdown and Math!</textarea>
    <div id="output"></div>

    <script>
        const input = document.getElementById('input');
        const output = document.getElementById('output');

        function renderContent() {
            const markdown = input.value;
            const html = marked.parse(markdown);
            output.innerHTML = html;
            renderMathInElement(output, {
                delimiters: [
                    {left: "$$", right: "$$", display: true},
                    {left: "$", right: "$", display: false}
                ],
                throwOnError: false
            });
        }

        input.addEventListener('input', renderContent);
        renderContent();
    </script>
</body>
</html>

I've created an HTML artifact that demonstrates the use of Marked for Markdown rendering and KaTeX for math rendering. Here's a breakdown of the key components:

  1. The page includes a textarea for input and a div for output.
  2. It uses Marked.js for Markdown parsing and KaTeX for math rendering.
  3. The input textarea is pre-populated with a comprehensive example that showcases various Markdown features and both inline and block math.
  4. The renderContent function is called on input changes and initial load. It:
    • Parses the Markdown using Marked
    • Renders the resulting HTML
    • Uses KaTeX to render any math expressions in the rendered HTML

You can modify the content in the textarea, and the rendered version will update in real-time below.

This example doesn't use React as requested, instead relying on vanilla JavaScript for simplicity and direct DOM manipulation.

Feel free to try it out and let me know if you'd like any modifications or have any questions!

human (Sep 20, 2024, 07:28 PM)

Add a text area below the preview that lets you copy out the HTML - with a copy button below it

assistant (Sep 20, 2024, 07:28 PM)

Certainly! I'll modify the artifact to include a textarea below the preview that displays the generated HTML, along with a copy button to easily copy the HTML content.

Markdown and Math Live Renderer with HTML Output

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Markdown and Math Live Renderer</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/marked/4.0.2/marked.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.15.3/katex.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.15.3/contrib/auto-render.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.15.3/katex.min.css">
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        textarea {
            width: 100%;
            height: 200px;
            margin-bottom: 20px;
        }
        #output, #html-output {
            border: 1px solid #ddd;
            padding: 10px;
            background-color: #f9f9f9;
            margin-bottom: 20px;
        }
        #html-output {
            height: 200px;
            overflow-y: auto;
        }
        button {
            padding: 10px 20px;
            background-color: #4CAF50;
            color: white;
            border: none;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>
    <h1>Markdown and Math Live Renderer</h1>
    <textarea id="input" placeholder="Enter your Markdown and LaTeX here...">
# Welcome to the Markdown and Math Renderer!

This example demonstrates how to use **Markdown** with inline and block *math* equations.

## Inline Math

The quadratic formula is $ax^2 + bx + c = 0$. Its solution is given by:

## Block Math

$$x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$$

## List with Math

1. First item
2. Second item with inline math: $E = mc^2$
3. Third item

## Table with Math

| Operation | Symbol | Example |
|-----------|--------|---------|
| Addition  | $+$    | $2 + 2 = 4$ |
| Subtraction | $-$  | $5 - 3 = 2$ |
| Multiplication | $\times$ | $3 \times 4 = 12$ |

Enjoy rendering your Markdown and Math!</textarea>
    <h2>Preview:</h2>
    <div id="output"></div>
    <h2>Generated HTML:</h2>
    <textarea id="html-output" readonly></textarea>
    <button id="copy-button">Copy HTML</button>

    <script>
        const input = document.getElementById('input');
        const output = document.getElementById('output');
        const htmlOutput = document.getElementById('html-output');
        const copyButton = document.getElementById('copy-button');

        function renderContent() {
            const markdown = input.value;
            const html = marked.parse(markdown);
            output.innerHTML = html;
            htmlOutput.value = output.innerHTML;
            renderMathInElement(output, {
                delimiters: [
                    {left: "$$", right: "$$", display: true},
                    {left: "$", right: "$", display: false}
                ],
                throwOnError: false
            });
        }

        input.addEventListener('input', renderContent);

        copyButton.addEventListener('click', () => {
            htmlOutput.select();
            document.execCommand('copy');
            alert('HTML copied to clipboard!');
        });

        renderContent();
    </script>
</body>
</html>

I've updated the artifact with the following changes:

  1. Added a new textarea with the id "html-output" below the preview area. This textarea displays the generated HTML.

  2. Included a "Copy HTML" button below the HTML output textarea.

  3. Modified the renderContent function to update the HTML output textarea with the generated HTML content.

  4. Added an event listener to the copy button that selects the HTML content, copies it to the clipboard, and shows an alert to confirm the action.

  5. Styled the new elements to match the existing design.

Now, when you enter or modify the Markdown content, you'll see:

  1. The rendered preview at the top
  2. The generated HTML in a textarea below the preview
  3. A button to easily copy the generated HTML

This enhancement allows users to not only preview their Markdown and math content but also grab the generated HTML for use elsewhere.

Feel free to try it out! Let me know if you want any further modifications or have any questions about the implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment