2.1.1 ā€¢ Published 1 month ago

pantsdown v2.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

Pantsdown

Pantsdown is a Markdown to HTML converter. It attempts to render markdown similar to how GitHub does it plus some features developed specifically for github-preview.nvim.

ā–¶ļø Demo

šŸ“¦ Installation

This package is distributed only as a TypeScript module. This means you'll need a bundler to handle transpilation. See below for usage examples.

# bun
bun install pantsdown
# npm
npm install pantsdown

šŸ’» Usage

šŸšØ Pantsdown does not sanitize the output HTML. If you are processing potentially unsafe strings, it's recommended you use a sanitization library like DOMPurify.

Styles

For styles to be properly applied, either the element containing the generated html or one of its parents must have the classes class="pantsdown light" or class="pantsdown dark" added. You can also add the class "high-contrast" to enable high-contrast themes class="pantsdown dark high-contrast" or class="pantsdown light high-contrast".

Bun

Take a look at how Pantsdown's demo is built for a very simple usage example with Bun.

Vite

Create a Vite Project & install dependencies:

bun create vite my-app --template react-swc-ts
cd my-app
bun install pantsdown

Remove CSS from my-app/src/main.tsx:

import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.tsx'
- import './index.css'

ReactDOM.createRoot(document.getElementById('root')!).render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
)

Replace content in my-app/src/App.tsx:

import { Pantsdown } from "pantsdown";
import "pantsdown/styles.css";
import { useEffect } from "react";

const pantsdown = new Pantsdown();

function App() {
  useEffect(() => {
    const container = document.getElementById("markdown-container");
    if (!container) return;

    const markdown = "# Hello world\n- [ ] Task 1\n- [x] Task 2";
    const { html, javascript } = pantsdown.parse(markdown);
    container.innerHTML = html;

    const newScript = document.createElement("script");
    newScript.text = javascript;
    container.appendChild(newScript);
  }, []);

  // āš ļø for styles to be applied, a parent element must have
  // the classes "pantsdown light" or "pantsdown dark" added
  return <div id="markdown-container" className="pantsdown light" />;
}

export default App;

āš™ļø Configuration

The Pantsdown constructor accepts an optional configuration object. If you

import { Pantsdown, type PartialPantsdownConfig } from "pantsdown";

// This is the default config object. If you provide
// a config object, it will be deeply merged into this.
const config: PartialPantsdownConfig = {
  renderer: {
    /**
     * Prefix to be added to relative image sources.
     * Must start and end with "/"
     *
     * @example
     * relativeImageUrlPrefix: "/__localimage__/"
     *
     * ![image](./wallpants-512.png)
     * relative src is updated and results in:
     * <img src="/__localimage__/wallpants-512.png" />
     *
     * ![image](https://avatars.githubusercontent.com/wallpants)
     * absolute src remains unchanged:
     * <img src="https://avatars.githubusercontent.com/wallpants" />
     */
    relativeImageUrlPrefix: "",

    /**
     * Whether to render <details> html tags with attribute open=""
     *
     * @default
     * false
     */
    detailsTagDefaultOpen: false,
  },
};

const pantsdown = new Pantsdown(config);
const html = pantsdown.parse(markdown);

console.log(html);

šŸ¤ Acknowledgements

Pantsdown is based on Marked. Without their hard work, Pantsdown would not exist.

2.1.1

1 month ago

2.1.0

2 months ago

2.0.8

2 months ago

2.0.7

2 months ago

2.0.6

3 months ago

2.0.5

4 months ago

2.0.4

4 months ago

2.0.3

5 months ago

2.0.2

5 months ago

2.0.1

5 months ago

2.0.0

5 months ago

1.10.4

5 months ago

1.10.3

5 months ago

1.10.2

6 months ago

1.10.1

6 months ago

1.10.0

6 months ago

1.9.0

6 months ago

1.8.3

6 months ago

1.8.2

6 months ago

1.8.1

6 months ago

1.8.0

6 months ago

1.7.2

6 months ago

1.7.1

6 months ago

1.6.1

6 months ago

1.6.0

6 months ago

1.5.2

6 months ago

1.5.1

6 months ago

1.5.0

6 months ago

1.4.5

6 months ago

1.4.4

6 months ago

1.4.3

6 months ago

1.4.2

6 months ago

1.4.1

7 months ago

1.4.0

7 months ago

1.3.1

7 months ago

1.3.0

7 months ago

1.2.0

7 months ago

1.1.3

7 months ago

1.1.2

7 months ago

1.1.1

7 months ago

1.1.0

7 months ago

1.0.1

7 months ago