Skip to content

Table of contents

One call that takes a Markdown document and returns a table of contents as a nested Markdown list, plus every heading with its depth, text, anchor slug and line.

  • Your agent’s cue: a README or a long document needs a table of contents at the top, or the agent needs to link to a section by its anchor.
  • You’ll reach for it when the headings keep changing and the hand-maintained list at the top is always one edit behind.

You are building a documentation portal in Lovable that imports READMEs from your repositories. The H1 of a README often carries badges, and the headings often carry inline code. A backend function sends each README to MarkdownScribe and shows the table of contents it gets back; its anchors are the same ones GitHub generates for that file, so a link copied from the portal works on GitHub too.

Your coding agent has to add “see the section on calling nvm use automatically” to a document, as a link. It calls the operation, finds the heading in headings, and uses its slug, calling-nvm-use-automatically, the same slug GitHub gives that heading, backticks and all. No guessing at how punctuation turns into an anchor.

A build step regenerates the table of contents of every long document before publishing. Documents written by many hands skip heading levels: an #### right under a ##. The list comes back nested under the real parent, so it renders as a list and not as text glued to the item above, and headings that contain code keep their backticks in the link text.

  • Anchors that match GitHub’s. Badges, inline code, punctuation and repeated headings produce the same slugs GitHub renders, so links built from the response work there.
  • The structure, not just the list. headings gives depth, text, slug and line for each heading; the list in toc is one way to use it, linking by slug is another.
  • Only real headings. Lines that look like headings inside code blocks are ignored; a text search would count them.
Terminal window
curl https://api.markdownscribe.com/v1/md/toc \
-H "X-API-Key: $MDSCRIBE_API_KEY" \
-H "Content-Type: application/json" \
-d '{"markdown":"# Node Version Manager [![CI](https://img.shields.io/badge/ci-passing-green.svg)](https://example.org/ci)\n\n## Installing\n\n### Install with `curl`\n\n## Usage\n\n#### Calling `nvm use` automatically\n\n## Troubleshooting\n"}'
import { MarkdownScribeClient } from "@markdownscribe/sdk";
const client = new MarkdownScribeClient({ apiKey: process.env.MDSCRIBE_API_KEY! });
const result = await client.toc({
markdown: "# Node Version Manager [![CI](https://img.shields.io/badge/ci-passing-green.svg)](https://example.org/ci)\n\n## Installing\n\n### Install with `curl`\n\n## Usage\n\n#### Calling `nvm use` automatically\n\n## Troubleshooting\n",
});

Save the markdown value above as README.md, then:

Terminal window
mdscribe toc README.md

Captured from the API on 2026-10-23.

{
"toc": "- [Node Version Manager](#node-version-manager-)\n - [Installing](#installing)\n - [Install with `curl`](#install-with-curl)\n - [Usage](#usage)\n - [Calling `nvm use` automatically](#calling-nvm-use-automatically)\n - [Troubleshooting](#troubleshooting)",
"headings": [
{
"depth": 1,
"text": "Node Version Manager",
"slug": "node-version-manager-",
"line": 1
},
{
"depth": 2,
"text": "Installing",
"slug": "installing",
"line": 3
},
{
"depth": 3,
"text": "Install with curl",
"slug": "install-with-curl",
"line": 5
},
{
"depth": 2,
"text": "Usage",
"slug": "usage",
"line": 7
},
{
"depth": 4,
"text": "Calling nvm use automatically",
"slug": "calling-nvm-use-automatically",
"line": 9
},
{
"depth": 2,
"text": "Troubleshooting",
"slug": "troubleshooting",
"line": 11
}
],
"meta": {
"request_id": "01M5MY6MM48EFQX12MXMN7YD3G",
"duration_ms": 24,
"credits": 1
}
}

1 credit per call. Each response says what it cost in the X-Credits-Charged header. Error responses are never charged.

  • Options: options.min_depth and options.max_depth (1 to 6) limit which heading levels appear. The defaults include every level.
  • Input: markdown, up to 1,048,576 characters. ATX headings (# to ######) and setext headings (underlined with === or ---) are both read.
Error When
validation markdown is missing, or a depth option is outside 1 to 6.
payload_too_large The document is longer than 1,048,576 characters.
unauthorized, insufficient_credits, rate_limited The key, the balance or the per-minute limit, as on every operation.

Alternatives, and when MarkdownScribe is worth it

Section titled “Alternatives, and when MarkdownScribe is worth it”

Checked on 2026-10-23 against the tool’s own documentation.

  • markdown-toc. Add a <!-- toc --> comment to the file and run npx markdown-toc -i README.md in a terminal to insert or refresh the list in place (github.com/jonschlinkert/markdown-toc). Use MarkdownScribe when your agent runs where there is no terminal (the Lovable or Base44 editor), when you want the headings with their slugs and lines as JSON, or when you want format, lint, table of contents and frontmatter from one key with one error format and one bill.