# Website to Markdown for LLMs: why it cuts tokens and how to automate it

Why clean Markdown outperforms raw HTML in LLM prompts and RAG, what good HTML-to-Markdown conversion strips, and how to automate it via API or MCP.

Published: 2026-11-28
Author: Ajain Vivek

**TL;DR:** Raw HTML wastes most of an LLM's context window on navigation, scripts, and styling. Converting a website to clean Markdown preserves headings, lists, tables, and code fences while cutting token count dramatically. Automate it with Webcrawl's /v1/scrape API, MCP, or our free converter.

Feeding raw HTML to an LLM wastes context tokens on `<script>` tags, SVG paths, cookie banners, and nested layout `<div>` wrappers. Converting a **website to Markdown** strips that boilerplate while keeping the hierarchy models actually rely on: headings, links, tables, and fenced code blocks.

Try it interactively on the free [website to Markdown](/tools/website-to-markdown) and [HTML to Markdown](/tools/html-to-markdown) tools, or read the [URL to Markdown](/url-to-markdown) API overview.

## Why does Markdown work better than raw HTML or plain text?

Plain text (`innerText`) removes noise, but it also destroys structure: headings collapse into paragraphs, tables turn into unreadable columns, and code blocks lose language tags. Clean Markdown hits the sweet spot:

| Format | Preserves headings & tables | Strips nav & scripts | Token efficiency |
| --- | --- | --- | --- |
| Raw HTML | Yes (noisy) | No | Low (heavy tag overhead) |
| Plain text | No | Partially | High (but loses structure) |
| Clean Markdown | Yes | Yes | High |

Models trained on billions of lines of GitHub READMEs and technical docs parse `#` headings, `| table |` syntax, and ```` ```ts ```` fences natively.

## What should a good HTML-to-Markdown pipeline strip vs keep?

A reliable converter does more than run regex over tags:

1. **Extract main content first:** drop `<nav>`, `<footer>`, `<aside>`, cookie modals, and inline `<script>`/`<style>` blocks before converting.
2. **Resolve relative URLs:** turn `/docs/auth` into `https://example.com/docs/auth` so agents can follow citations without guessing the origin.
3. **Preserve tables and code blocks:** keep language hints (`language-ts` to ```` ```ts ````) and GFM pipe tables intact.
4. **Fallback to browser rendering only when needed:** fetch static HTML directly first, and render JavaScript only when the initial document is an empty app shell.
5. **Return metadata alongside Markdown:** title, description, canonical URL, and status code help downstream RAG filters.

## How do you convert a URL to Markdown via API, CLI, or MCP?

### REST (`POST /v1/scrape`)

```bash
curl -X POST https://webcrawl.app/v1/scrape \
  -H "Authorization: Bearer $WEBCRAWL_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"url":"https://webcrawl.app/docs/scrape","formats":["markdown"]}'
```

### TypeScript (`@webcrawl/client`)

```ts
import { createClient } from "@webcrawl/client";

const client = createClient({ apiKey: process.env.WEBCRAWL_API_KEY });
const res = await client.scrape({ url: "https://example.com/docs" });
console.log(res.markdown);
```

### CLI and MCP

```bash
npx webcrawl scrape https://example.com/docs
```

Inside Claude Code, Cursor, or Codex with Webcrawl MCP connected, the agent calls `web_read` directly and receives Markdown without any shell script.

## When should a site serve `.md` mirrors directly?

If you publish documentation or marketing pages, you do not have to force every agent to scrape HTML at all. Serving a `.md` mirror (like `https://webcrawl.app/docs/scrape.md`) with `<link rel="alternate" type="text/markdown">` lets agents fetch clean Markdown in one `GET` request. See [what is llms.txt](/blog/what-is-llms-txt) and [AEO for developer docs](/blog/aeo-for-developer-docs).

## FAQ

### Does converting HTML to Markdown break code examples?

Not when the parser reads `<pre><code class="language-...">` elements before collapsing whitespace. Good converters keep indentation and attach the language identifier to the fenced block.

### How do you handle pages that render content with JavaScript?

Start with an HTTP fetch for speed; if the response is a client-rendered shell with minimal text, fall back to a headless browser render before running main-content extraction.

### Is there a free way to test website-to-Markdown conversion?

Yes. Use the [website to Markdown](/tools/website-to-markdown) tool in your browser with no account, or sign up for 1,000 free monthly API credits on [webcrawl.app](https://webcrawl.app).

HTML: https://webcrawl.app/blog/website-to-markdown-for-llms
