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.
API Usage
Section titled “API Usage”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 HTMLconsole.log(toc); // Array of TOC items
Function Output
Section titled “Function Output”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.)}
Example of a TOC Item
Section titled “Example of a TOC Item”{ id: 'heading-1', // Generated ID for the heading title: 'Heading 1', // The heading text level: 1 // H1 corresponds to level 1}
Example Markdown and Output
Section titled “Example Markdown and Output”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 },];
Key Benefits
Section titled “Key Benefits”- 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.