Skip to content

Using the Node.js API

The markdom.parse(markdown) function is the core method for converting Markdown to HTML and generating a Table of Contents (TOC) based on the headers in your Markdown document.

To get started, import the markdom module and use the parse() function to process a Markdown string:

import { markdom } from '@tenedev/markdom';
const md = '# Heading 1\n## Heading 2\n### Heading 3';
const { html, toc } = markdom.parse(md);
console.log(html); // Rendered HTML
console.log(toc); // Array of TOC items

The markdom.parse() function returns an object containing two properties:

  • html: The rendered HTML string generated from the provided Markdown.
  • toc: An array of Table of Contents (TOC) entries, which corresponds to the headings in the Markdown document.

Each item in the toc array follows this structure:

interface TOCItem {
id: string; // Unique ID for the heading, usually based on the text of the heading
title: string; // The heading text
level: number; // The level of the heading (e.g., 1 for H1, 2 for H2, etc.)
}
{
id: 'heading-1', // Generated ID for the heading
title: 'Heading 1', // The heading text
level: 1 // H1 corresponds to level 1
}

Markdown:

# Main Heading
## Subheading 1
### Sub-subheading 1.1

Output (HTML):

<h1 id="main-heading">Main Heading</h1>
<h2 id="subheading-1">Subheading 1</h2>
<h3 id="sub-subheading-1-1">Sub-subheading 1.1</h3>

Output (TOC):

[
{ id: 'main-heading', title: 'Main Heading', level: 1 },
{ id: 'subheading-1', title: 'Subheading 1', level: 2 },
{ id: 'sub-subheading-1-1', title: 'Sub-subheading 1.1', level: 3 },
];
  • Seamless Markdown-to-HTML conversion.
  • Automatic Table of Contents generation for easier navigation.
  • Flexible heading support, with the ability to customize how TOC items are rendered.