rehype

repository·main·Indexed 25 days ago

https://github.com/rehypejs/rehype

An ecosystem of plugins for inspecting and transforming HTML using the Hypertext Abstract Syntax Tree (hast). It includes the rehype() processor shortcut, rehype-parse for transforming HTML strings into a syntax tree, rehype-stringify for serializing hast back to HTML, and rehype-cli for command-line HTML manipulation. Requires Node.js 16+ and is ESM only.

Tokens
6.5K
Snippets
13
Records
42
Agent score
77%

What's inside rehype

  1. Explore the rehype plugin ecosystem

    main

    The rehype ecosystem contains a wide variety of plugins for transforming HTML. You can find a curated list of high-quality projects in the awesome-rehype repository or search for the rehype-plugin topic on GitHub.

    Important Distinction:

    • rehype plugins work with HTML.
    • remark plugins work with markdown.

    If you are looking for markdown transformations, you should consult the remark plugin list instead.

  2. What is rehype and how does it work?

    main

    rehype is an ecosystem of plugins that transform HTML using an Abstract Syntax Tree (AST) called hast (HTML AST). Plugins inspect and modify this tree to change the structure or content of HTML. It is built on top of the unified project, which provides the core transformation engine.

    Key components:

    • rehype: Adds HTML support to the unified ecosystem.
    • hast: The specific HTML AST format used by rehype.
    • Plugins: Programs that inspect or change the hast tree.
  3. Naming conventions for plugins and utilities

    main

    When creating new tools for the ecosystem, use the following prefixing conventions to ensure users understand the scope of your tool:

    • rehype-: Use this prefix if the tool is a plugin that works with rehype().use().
    • hast-util-: Use this prefix if the tool is a utility that works specifically with the hast (HTML) syntax tree.
    • unist-util-: Use this prefix if the tool is a utility that works with any unist syntax tree.
    • vfile-: Use this prefix if the tool is a utility that works with virtual files (vfile).

    Warning: Do not use the rehype- prefix if your tool does not work with rehype().use(), as this will confuse users.

  4. Security considerations when using rehype

    main
    Since rehype processes HTML, improper use can lead to Cross-Site Scripting (XSS) attacks. To ensure the resulting HTML tree is safe, it is highly recommended to use rehype-sanitize as part of your processing pipeline. Additionally, always carefully assess the security risks of any third-party plugins you add to your processor.
  5. Install rehype-stringify

    main

    Install rehype-stringify via npm for Node.js (version 16+). Note that this package is ESM only.

    In Deno, use esm.sh:

    import rehypeStringify from 'https://esm.sh/rehype-stringify@10'

    In browsers, use esm.sh with a module script:

    <script type="module">
      import rehypeStringify from 'https://esm.sh/rehype-stringify@10?bundle'
    </script>
    npm install rehype-stringify
  6. Install rehype-parse

    main

    Install rehype-parse via npm for Node.js (version 16+). Note that this package is ESM only.

    In Deno, use esm.sh:

    import rehypeParse from 'https://esm.sh/rehype-parse@9'

    In browsers, use esm.sh with the ?bundle query:

    <script type="module">
      import rehypeParse from 'https://esm.sh/rehype-parse@9?bundle'
    </script>
    npm install rehype-parse