Rendering Markdown to HTML with Syntax Highlighting

Markup

Why Server-Side Markdown Rendering?

Rendering Markdown in the browser is fine for simple cases, but server-side rendering has real advantages:

  • SEO — search engines index the rendered HTML, not raw Markdown
  • Security — XSS is sanitised centrally, not per-client
  • Consistency — the same output regardless of which Markdown library the client uses
  • Syntax highlighting — Pygments produces accurate, theme-able code blocks

Calling the API

curl -X POST https://api.toolkitapi.io/v1/convert/markdown-to-html \
  -H "X-API-Key: $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"markdown": "# Hello\n\nThis is **bold** text."}'

Response:

{
  "html": "<h1>Hello</h1>\n<p>This is <strong>bold</strong> text.</p>"
}

Supported Extensions

Extension What it enables
fenced_code Triple-backtick code blocks
tables GFM-style pipe tables
toc [TOC] placeholder generates a table of contents
footnotes [^1] footnote syntax
strikethrough ~~deleted~~ text

Pass the extensions you need in the request body:

{
  "markdown": "...",
  "extensions": ["tables", "fenced_code", "toc"]
}

Syntax Highlighting

When fenced_code is enabled, you can specify a language on the opening fence:

```python
def hello():
    return "world"
```

The API applies Pygments highlighting and returns a <div class="highlight"> block with inline CSS classes — ready to style with any Pygments CSS theme.

Storing Rendered HTML

If your content rarely changes, store the rendered HTML in your database alongside the raw Markdown. On read, serve the cached HTML. On write, call the API to re-render. This avoids repeated API calls and keeps rendering costs near zero.

Try it out

Browse Tools →

More from the Blog