Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/add-markdown-for-agents.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/markdown-for-agents': minor
---

Add markdown-for-agents middleware
85 changes: 85 additions & 0 deletions packages/markdown-for-agents/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# @hono/markdown-for-agents

Hono middleware for [markdown-for-agents](https://www.npmjs.com/package/markdown-for-agents) — converts HTML responses to clean, token-efficient Markdown for AI agents.

> `markdown-for-agents` is an ESM-only dependency.

When a client sends `Accept: text/markdown`, HTML responses are automatically converted to Markdown — typically saving 80–90% of tokens. Normal browser requests pass through untouched.

## Install

```bash
npm install @hono/markdown-for-agents
# yarn add @hono/markdown-for-agents
```

## Usage

```ts
import { Hono } from 'hono'
import { markdown } from '@hono/markdown-for-agents'

const app = new Hono()
app.use(markdown())

app.get('/', (c) => {
return c.html('<h1>Hello</h1>')
})

export default app
```

```bash
# Normal HTML response
curl http://localhost:3000

# Markdown response for AI agents
curl -H "Accept: text/markdown" http://localhost:3000
```

## How it works

The middleware uses content negotiation. When a client sends `Accept: text/markdown`, HTML responses are automatically converted to Markdown. The response includes:

- `Content-Type: text/markdown; charset=utf-8`
- `x-markdown-tokens` header with the token count
- `ETag` header with a content hash for cache validation
- `Vary: Accept` header so CDNs cache HTML and Markdown separately
- `content-signal` header with publisher consent signals (when configured)

## Options

Accepts all [`markdown-for-agents` options](https://www.npmjs.com/package/markdown-for-agents#options):

```ts
app.use(
markdown({
// Strip nav, ads, sidebars, cookie banners
extract: true,

// Resolve relative URLs
baseUrl: 'https://example.com',

// Remove duplicate content blocks
deduplicate: true,

// Custom token counter (e.g. tiktoken)
tokenCounter: (text) => ({
tokens: enc.encode(text).length,
characters: text.length,
words: text.split(/\s+/).filter(Boolean).length,
}),

// Publisher consent signal header
contentSignal: { aiTrain: true, search: true, aiInput: true },
})
)
```

## Author

Konstantin Konstantinov <https://github.com/KKonstantinov>

## License

MIT
16 changes: 16 additions & 0 deletions packages/markdown-for-agents/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "@hono/markdown-for-agents",
"version": "0.1.0",
"license": "MIT",
"exports": {
".": "./src/index.ts"
},
"imports": {
"hono": "jsr:@hono/hono@^4.8.3",
"markdown-for-agents": "npm:markdown-for-agents@^1.3.1"
},
"publish": {
"include": ["deno.json", "README.md", "src/**/*.ts"],
"exclude": ["src/**/*.test.ts"]
}
}
51 changes: 51 additions & 0 deletions packages/markdown-for-agents/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"name": "@hono/markdown-for-agents",
"version": "0.1.0",
"description": "Hono middleware that converts HTML responses to Markdown for AI agents via Accept: text/markdown",
"type": "module",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"build": "tsdown",
"format": "prettier --check . --ignore-path ../../.gitignore",
"lint": "eslint",
"typecheck": "tsc -b tsconfig.json",
"test": "vitest",
"version:jsr": "yarn version:set $npm_package_version"
},
"exports": {
".": {
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
},
"license": "MIT",
"publishConfig": {
"registry": "https://registry.npmjs.org",
"access": "public",
"provenance": true
},
"repository": {
"type": "git",
"url": "git+https://github.com/honojs/middleware.git",
"directory": "packages/markdown-for-agents"
},
"homepage": "https://github.com/honojs/middleware",
"dependencies": {
"markdown-for-agents": "^1.3.1"
},
"peerDependencies": {
"hono": ">=4.0.0"
},
"devDependencies": {
"hono": "^4.11.5",
"tsdown": "^0.15.9",
"typescript": "^5.9.3",
"vitest": "^4.1.0-beta.1"
}
}
Loading