@markdown-for-agents/web 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 ADDED
@@ -0,0 +1,116 @@
1
+ # @markdown-for-agents/web
2
+
3
+ Web Standard (Request/Response) 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
+ Works anywhere the Web Standard Request/Response API is available: Cloudflare Workers, Deno, Bun, and Node.js. 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/web markdown-for-agents
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ ### Cloudflare Workers
25
+
26
+ ```ts
27
+ import { markdownMiddleware } from '@markdown-for-agents/web';
28
+
29
+ const mw = markdownMiddleware({ extract: true });
30
+
31
+ export default {
32
+ async fetch(request: Request): Promise<Response> {
33
+ return mw(request, async () => {
34
+ const html = await renderPage();
35
+ return new Response(html, {
36
+ headers: { 'content-type': 'text/html' }
37
+ });
38
+ });
39
+ }
40
+ };
41
+ ```
42
+
43
+ ### Deno
44
+
45
+ ```ts
46
+ import { markdownMiddleware } from '@markdown-for-agents/web';
47
+
48
+ const mw = markdownMiddleware({ extract: true });
49
+
50
+ Deno.serve(async request => {
51
+ return mw(request, async () => {
52
+ return new Response('<h1>Hello</h1>', {
53
+ headers: { 'content-type': 'text/html' }
54
+ });
55
+ });
56
+ });
57
+ ```
58
+
59
+ ### Bun
60
+
61
+ ```ts
62
+ import { markdownMiddleware } from '@markdown-for-agents/web';
63
+
64
+ const mw = markdownMiddleware({ extract: true });
65
+
66
+ Bun.serve({
67
+ async fetch(request) {
68
+ return mw(request, async () => {
69
+ return new Response('<h1>Hello</h1>', {
70
+ headers: { 'content-type': 'text/html' }
71
+ });
72
+ });
73
+ }
74
+ });
75
+ ```
76
+
77
+ ```bash
78
+ # Normal HTML response
79
+ curl http://localhost:3000
80
+
81
+ # Markdown response for AI agents
82
+ curl -H "Accept: text/markdown" http://localhost:3000
83
+ ```
84
+
85
+ ## Options
86
+
87
+ Accepts all [`markdown-for-agents` options](https://www.npmjs.com/package/markdown-for-agents#options):
88
+
89
+ ```ts
90
+ const mw = markdownMiddleware({
91
+ // Strip nav, ads, sidebars, cookie banners
92
+ extract: true,
93
+
94
+ // Resolve relative URLs
95
+ baseUrl: 'https://example.com',
96
+
97
+ // Remove duplicate content blocks
98
+ deduplicate: true,
99
+
100
+ // Custom token counter (e.g. tiktoken)
101
+ tokenCounter: text => ({ tokens: enc.encode(text).length, characters: text.length, words: text.split(/\s+/).filter(Boolean).length })
102
+ });
103
+ ```
104
+
105
+ ## Other frameworks
106
+
107
+ | Package | Framework |
108
+ | -------------------------------------------------------------------------------------------- | --------- |
109
+ | [`@markdown-for-agents/express`](https://www.npmjs.com/package/@markdown-for-agents/express) | Express |
110
+ | [`@markdown-for-agents/fastify`](https://www.npmjs.com/package/@markdown-for-agents/fastify) | Fastify |
111
+ | [`@markdown-for-agents/hono`](https://www.npmjs.com/package/@markdown-for-agents/hono) | Hono |
112
+ | [`@markdown-for-agents/nextjs`](https://www.npmjs.com/package/@markdown-for-agents/nextjs) | Next.js |
113
+
114
+ ## License
115
+
116
+ MIT
package/dist/index.mjs CHANGED
@@ -25,13 +25,15 @@ function wantsMarkdown(request) {
25
25
  function markdownMiddleware(options) {
26
26
  const tokenHeader = options?.tokenHeader ?? "x-markdown-tokens";
27
27
  return async (request, next) => {
28
- if (!wantsMarkdown(request)) return next(request);
29
28
  const response = await next(request);
29
+ response.headers.append("vary", "Accept");
30
+ if (!wantsMarkdown(request)) return response;
30
31
  if (!(response.headers.get("content-type") ?? "").includes("text/html")) return response;
31
- const { markdown, tokenEstimate } = convert(await response.text(), options);
32
+ const { markdown, tokenEstimate, contentHash } = convert(await response.text(), options);
32
33
  const headers = new Headers(response.headers);
33
34
  headers.set("content-type", "text/markdown; charset=utf-8");
34
35
  headers.set(tokenHeader, String(tokenEstimate.tokens));
36
+ headers.set("etag", `"${contentHash}"`);
35
37
  return new Response(markdown, {
36
38
  status: response.status,
37
39
  statusText: response.statusText,
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["import { convert } from 'markdown-for-agents';\nimport type { MiddlewareOptions } from 'markdown-for-agents';\n\nexport type { MiddlewareOptions } from 'markdown-for-agents';\n\ntype Handler = (request: Request) => Response | Promise<Response>;\ntype Middleware = (request: Request, next: Handler) => Response | Promise<Response>;\n\nfunction wantsMarkdown(request: Request): boolean {\n const accept = request.headers.get('accept') ?? '';\n return accept.includes('text/markdown');\n}\n\n/**\n * Web-standard middleware that converts HTML responses to markdown\n * when the client sends an `Accept: text/markdown` header.\n *\n * Compatible with any runtime that supports the Fetch API\n * (Cloudflare Workers, Deno, Bun, etc.).\n *\n * @param options - Conversion and middleware options.\n * @returns A middleware function that wraps a {@link Handler}.\n *\n * @example\n * ```ts\n * import { markdownMiddleware } from \"@markdown-for-agents/web\";\n *\n * const md = markdownMiddleware({ extract: true });\n * const response = await md(request, handler);\n * ```\n */\nexport function markdownMiddleware(options?: MiddlewareOptions): Middleware {\n const tokenHeader = options?.tokenHeader ?? 'x-markdown-tokens';\n\n return async (request: Request, next: Handler): Promise<Response> => {\n if (!wantsMarkdown(request)) {\n return next(request);\n }\n\n const response = await next(request);\n const contentType = response.headers.get('content-type') ?? '';\n\n if (!contentType.includes('text/html')) {\n return response;\n }\n\n const html = await response.text();\n const { markdown, tokenEstimate } = convert(html, options);\n\n const headers = new Headers(response.headers);\n headers.set('content-type', 'text/markdown; charset=utf-8');\n headers.set(tokenHeader, String(tokenEstimate.tokens));\n\n return new Response(markdown, {\n status: response.status,\n statusText: response.statusText,\n headers\n });\n };\n}\n"],"mappings":";;;AAQA,SAAS,cAAc,SAA2B;AAE9C,SADe,QAAQ,QAAQ,IAAI,SAAS,IAAI,IAClC,SAAS,gBAAgB;;;;;;;;;;;;;;;;;;;;AAqB3C,SAAgB,mBAAmB,SAAyC;CACxE,MAAM,cAAc,SAAS,eAAe;AAE5C,QAAO,OAAO,SAAkB,SAAqC;AACjE,MAAI,CAAC,cAAc,QAAQ,CACvB,QAAO,KAAK,QAAQ;EAGxB,MAAM,WAAW,MAAM,KAAK,QAAQ;AAGpC,MAAI,EAFgB,SAAS,QAAQ,IAAI,eAAe,IAAI,IAE3C,SAAS,YAAY,CAClC,QAAO;EAIX,MAAM,EAAE,UAAU,kBAAkB,QADvB,MAAM,SAAS,MAAM,EACgB,QAAQ;EAE1D,MAAM,UAAU,IAAI,QAAQ,SAAS,QAAQ;AAC7C,UAAQ,IAAI,gBAAgB,+BAA+B;AAC3D,UAAQ,IAAI,aAAa,OAAO,cAAc,OAAO,CAAC;AAEtD,SAAO,IAAI,SAAS,UAAU;GAC1B,QAAQ,SAAS;GACjB,YAAY,SAAS;GACrB;GACH,CAAC"}
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["import { convert } from 'markdown-for-agents';\nimport type { MiddlewareOptions } from 'markdown-for-agents';\n\nexport type { MiddlewareOptions } from 'markdown-for-agents';\n\ntype Handler = (request: Request) => Response | Promise<Response>;\ntype Middleware = (request: Request, next: Handler) => Response | Promise<Response>;\n\nfunction wantsMarkdown(request: Request): boolean {\n const accept = request.headers.get('accept') ?? '';\n return accept.includes('text/markdown');\n}\n\n/**\n * Web-standard middleware that converts HTML responses to markdown\n * when the client sends an `Accept: text/markdown` header.\n *\n * Compatible with any runtime that supports the Fetch API\n * (Cloudflare Workers, Deno, Bun, etc.).\n *\n * @param options - Conversion and middleware options.\n * @returns A middleware function that wraps a {@link Handler}.\n *\n * @example\n * ```ts\n * import { markdownMiddleware } from \"@markdown-for-agents/web\";\n *\n * const md = markdownMiddleware({ extract: true });\n * const response = await md(request, handler);\n * ```\n */\nexport function markdownMiddleware(options?: MiddlewareOptions): Middleware {\n const tokenHeader = options?.tokenHeader ?? 'x-markdown-tokens';\n\n return async (request: Request, next: Handler): Promise<Response> => {\n const response = await next(request);\n\n // Always signal that responses vary by Accept so caches store\n // separate entries for HTML and Markdown representations.\n response.headers.append('vary', 'Accept');\n\n if (!wantsMarkdown(request)) {\n return response;\n }\n\n const contentType = response.headers.get('content-type') ?? '';\n\n if (!contentType.includes('text/html')) {\n return response;\n }\n\n const html = await response.text();\n const { markdown, tokenEstimate, contentHash } = convert(html, options);\n\n const headers = new Headers(response.headers);\n headers.set('content-type', 'text/markdown; charset=utf-8');\n headers.set(tokenHeader, String(tokenEstimate.tokens));\n headers.set('etag', `\"${contentHash}\"`);\n\n return new Response(markdown, {\n status: response.status,\n statusText: response.statusText,\n headers\n });\n };\n}\n"],"mappings":";;;AAQA,SAAS,cAAc,SAA2B;AAE9C,SADe,QAAQ,QAAQ,IAAI,SAAS,IAAI,IAClC,SAAS,gBAAgB;;;;;;;;;;;;;;;;;;;;AAqB3C,SAAgB,mBAAmB,SAAyC;CACxE,MAAM,cAAc,SAAS,eAAe;AAE5C,QAAO,OAAO,SAAkB,SAAqC;EACjE,MAAM,WAAW,MAAM,KAAK,QAAQ;AAIpC,WAAS,QAAQ,OAAO,QAAQ,SAAS;AAEzC,MAAI,CAAC,cAAc,QAAQ,CACvB,QAAO;AAKX,MAAI,EAFgB,SAAS,QAAQ,IAAI,eAAe,IAAI,IAE3C,SAAS,YAAY,CAClC,QAAO;EAIX,MAAM,EAAE,UAAU,eAAe,gBAAgB,QADpC,MAAM,SAAS,MAAM,EAC6B,QAAQ;EAEvE,MAAM,UAAU,IAAI,QAAQ,SAAS,QAAQ;AAC7C,UAAQ,IAAI,gBAAgB,+BAA+B;AAC3D,UAAQ,IAAI,aAAa,OAAO,cAAc,OAAO,CAAC;AACtD,UAAQ,IAAI,QAAQ,IAAI,YAAY,GAAG;AAEvC,SAAO,IAAI,SAAS,UAAU;GAC1B,QAAQ,SAAS;GACjB,YAAY,SAAS;GACrB;GACH,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@markdown-for-agents/web",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "Web Standard (Request/Response) 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.1"
23
+ "markdown-for-agents": "0.1.3"
24
24
  },
25
25
  "keywords": [
26
26
  "html",