@markdown-for-agents/hono 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +79 -0
- package/dist/index.mjs +4 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# @markdown-for-agents/hono
|
|
2
|
+
|
|
3
|
+
Hono middleware for [markdown-for-agents](https://www.npmjs.com/package/markdown-for-agents) — a runtime-agnostic HTML to Markdown converter built for AI agents.
|
|
4
|
+
|
|
5
|
+
Add one line to your Hono app and AI agents get clean, token-efficient Markdown instead of HTML. Normal browser requests pass through untouched.
|
|
6
|
+
|
|
7
|
+
## How it works
|
|
8
|
+
|
|
9
|
+
The middleware uses content negotiation. When a client sends `Accept: text/markdown`, HTML responses are automatically converted to Markdown. The response includes:
|
|
10
|
+
|
|
11
|
+
- `Content-Type: text/markdown; charset=utf-8`
|
|
12
|
+
- `x-markdown-tokens` header with the token count
|
|
13
|
+
- `ETag` header with a content hash for cache validation
|
|
14
|
+
- `Vary: Accept` header so CDNs cache HTML and Markdown separately
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install @markdown-for-agents/hono markdown-for-agents
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { Hono } from 'hono';
|
|
26
|
+
import { markdown } from '@markdown-for-agents/hono';
|
|
27
|
+
|
|
28
|
+
const app = new Hono();
|
|
29
|
+
app.use(markdown({ extract: true }));
|
|
30
|
+
|
|
31
|
+
app.get('/', c => {
|
|
32
|
+
return c.html('<h1>Hello</h1>');
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
export default app;
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
# Normal HTML response
|
|
40
|
+
curl http://localhost:3000
|
|
41
|
+
|
|
42
|
+
# Markdown response for AI agents
|
|
43
|
+
curl -H "Accept: text/markdown" http://localhost:3000
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Options
|
|
47
|
+
|
|
48
|
+
Accepts all [`markdown-for-agents` options](https://www.npmjs.com/package/markdown-for-agents#options):
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
app.use(
|
|
52
|
+
markdown({
|
|
53
|
+
// Strip nav, ads, sidebars, cookie banners
|
|
54
|
+
extract: true,
|
|
55
|
+
|
|
56
|
+
// Resolve relative URLs
|
|
57
|
+
baseUrl: 'https://example.com',
|
|
58
|
+
|
|
59
|
+
// Remove duplicate content blocks
|
|
60
|
+
deduplicate: true,
|
|
61
|
+
|
|
62
|
+
// Custom token counter (e.g. tiktoken)
|
|
63
|
+
tokenCounter: text => ({ tokens: enc.encode(text).length, characters: text.length, words: text.split(/\s+/).filter(Boolean).length })
|
|
64
|
+
})
|
|
65
|
+
);
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Other frameworks
|
|
69
|
+
|
|
70
|
+
| Package | Framework |
|
|
71
|
+
| -------------------------------------------------------------------------------------------- | -------------------------------------------- |
|
|
72
|
+
| [`@markdown-for-agents/express`](https://www.npmjs.com/package/@markdown-for-agents/express) | Express |
|
|
73
|
+
| [`@markdown-for-agents/fastify`](https://www.npmjs.com/package/@markdown-for-agents/fastify) | Fastify |
|
|
74
|
+
| [`@markdown-for-agents/nextjs`](https://www.npmjs.com/package/@markdown-for-agents/nextjs) | Next.js |
|
|
75
|
+
| [`@markdown-for-agents/web`](https://www.npmjs.com/package/@markdown-for-agents/web) | Web Standard (Cloudflare Workers, Deno, Bun) |
|
|
76
|
+
|
|
77
|
+
## License
|
|
78
|
+
|
|
79
|
+
MIT
|
package/dist/index.mjs
CHANGED
|
@@ -20,16 +20,18 @@ import { convert } from "markdown-for-agents";
|
|
|
20
20
|
function markdown(options) {
|
|
21
21
|
const tokenHeader = options?.tokenHeader ?? "x-markdown-tokens";
|
|
22
22
|
return async (c, next) => {
|
|
23
|
-
if (!(c.req.header("accept") ?? "").includes("text/markdown")) return next();
|
|
24
23
|
await next();
|
|
24
|
+
c.res.headers.append("vary", "Accept");
|
|
25
|
+
if (!(c.req.header("accept") ?? "").includes("text/markdown")) return;
|
|
25
26
|
if (!(c.res.headers.get("content-type") ?? "").includes("text/html")) return;
|
|
26
|
-
const { markdown: md, tokenEstimate } = convert(await c.res.text(), options);
|
|
27
|
+
const { markdown: md, tokenEstimate, contentHash } = convert(await c.res.text(), options);
|
|
27
28
|
c.res = new Response(md, {
|
|
28
29
|
status: c.res.status,
|
|
29
30
|
headers: c.res.headers
|
|
30
31
|
});
|
|
31
32
|
c.res.headers.set("content-type", "text/markdown; charset=utf-8");
|
|
32
33
|
c.res.headers.set(tokenHeader, String(tokenEstimate.tokens));
|
|
34
|
+
c.res.headers.set("etag", `"${contentHash}"`);
|
|
33
35
|
};
|
|
34
36
|
}
|
|
35
37
|
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["import type { MiddlewareHandler } from 'hono';\nimport { convert } from 'markdown-for-agents';\nimport type { MiddlewareOptions } from 'markdown-for-agents';\n\nexport type { MiddlewareOptions } from 'markdown-for-agents';\n\n/**\n * Hono middleware that converts HTML responses to markdown\n * when the client sends an `Accept: text/markdown` header.\n *\n * @param options - Conversion and middleware options.\n * @returns A Hono middleware handler.\n *\n * @example\n * ```ts\n * import { Hono } from \"hono\";\n * import { markdown } from \"@markdown-for-agents/hono\";\n *\n * const app = new Hono();\n * app.use(\"*\", markdown());\n * ```\n */\nexport function markdown(options?: MiddlewareOptions): MiddlewareHandler {\n const tokenHeader = options?.tokenHeader ?? 'x-markdown-tokens';\n\n return async (c, next) => {\n const accept = c.req.header('accept') ?? '';\n if (!accept.includes('text/markdown'))
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["import type { MiddlewareHandler } from 'hono';\nimport { convert } from 'markdown-for-agents';\nimport type { MiddlewareOptions } from 'markdown-for-agents';\n\nexport type { MiddlewareOptions } from 'markdown-for-agents';\n\n/**\n * Hono middleware that converts HTML responses to markdown\n * when the client sends an `Accept: text/markdown` header.\n *\n * @param options - Conversion and middleware options.\n * @returns A Hono middleware handler.\n *\n * @example\n * ```ts\n * import { Hono } from \"hono\";\n * import { markdown } from \"@markdown-for-agents/hono\";\n *\n * const app = new Hono();\n * app.use(\"*\", markdown());\n * ```\n */\nexport function markdown(options?: MiddlewareOptions): MiddlewareHandler {\n const tokenHeader = options?.tokenHeader ?? 'x-markdown-tokens';\n\n return async (c, next) => {\n await next();\n\n // Always signal that responses vary by Accept so caches store\n // separate entries for HTML and Markdown representations.\n c.res.headers.append('vary', 'Accept');\n\n const accept = c.req.header('accept') ?? '';\n if (!accept.includes('text/markdown')) return;\n\n const contentType = c.res.headers.get('content-type') ?? '';\n if (!contentType.includes('text/html')) return;\n\n const html = await c.res.text();\n const { markdown: md, tokenEstimate, contentHash } = convert(html, options);\n\n c.res = new Response(md, {\n status: c.res.status,\n headers: c.res.headers\n });\n c.res.headers.set('content-type', 'text/markdown; charset=utf-8');\n c.res.headers.set(tokenHeader, String(tokenEstimate.tokens));\n c.res.headers.set('etag', `\"${contentHash}\"`);\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAsBA,SAAgB,SAAS,SAAgD;CACrE,MAAM,cAAc,SAAS,eAAe;AAE5C,QAAO,OAAO,GAAG,SAAS;AACtB,QAAM,MAAM;AAIZ,IAAE,IAAI,QAAQ,OAAO,QAAQ,SAAS;AAGtC,MAAI,EADW,EAAE,IAAI,OAAO,SAAS,IAAI,IAC7B,SAAS,gBAAgB,CAAE;AAGvC,MAAI,EADgB,EAAE,IAAI,QAAQ,IAAI,eAAe,IAAI,IACxC,SAAS,YAAY,CAAE;EAGxC,MAAM,EAAE,UAAU,IAAI,eAAe,gBAAgB,QADxC,MAAM,EAAE,IAAI,MAAM,EACoC,QAAQ;AAE3E,IAAE,MAAM,IAAI,SAAS,IAAI;GACrB,QAAQ,EAAE,IAAI;GACd,SAAS,EAAE,IAAI;GAClB,CAAC;AACF,IAAE,IAAI,QAAQ,IAAI,gBAAgB,+BAA+B;AACjE,IAAE,IAAI,QAAQ,IAAI,aAAa,OAAO,cAAc,OAAO,CAAC;AAC5D,IAAE,IAAI,QAAQ,IAAI,QAAQ,IAAI,YAAY,GAAG"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@markdown-for-agents/hono",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Hono middleware for markdown-for-agents",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"main": "./dist/index.mjs",
|
|
21
21
|
"types": "./dist/index.d.mts",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"markdown-for-agents": "0.1.
|
|
23
|
+
"markdown-for-agents": "0.1.3"
|
|
24
24
|
},
|
|
25
25
|
"peerDependencies": {
|
|
26
26
|
"hono": ">=4.0.0"
|