@markdown-for-agents/web 0.1.1 → 0.1.2
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 +114 -0
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
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
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @markdown-for-agents/web markdown-for-agents
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
### Cloudflare Workers
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { markdownMiddleware } from '@markdown-for-agents/web';
|
|
26
|
+
|
|
27
|
+
const mw = markdownMiddleware({ extract: true });
|
|
28
|
+
|
|
29
|
+
export default {
|
|
30
|
+
async fetch(request: Request): Promise<Response> {
|
|
31
|
+
return mw(request, async () => {
|
|
32
|
+
const html = await renderPage();
|
|
33
|
+
return new Response(html, {
|
|
34
|
+
headers: { 'content-type': 'text/html' }
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Deno
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
import { markdownMiddleware } from '@markdown-for-agents/web';
|
|
45
|
+
|
|
46
|
+
const mw = markdownMiddleware({ extract: true });
|
|
47
|
+
|
|
48
|
+
Deno.serve(async request => {
|
|
49
|
+
return mw(request, async () => {
|
|
50
|
+
return new Response('<h1>Hello</h1>', {
|
|
51
|
+
headers: { 'content-type': 'text/html' }
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Bun
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
import { markdownMiddleware } from '@markdown-for-agents/web';
|
|
61
|
+
|
|
62
|
+
const mw = markdownMiddleware({ extract: true });
|
|
63
|
+
|
|
64
|
+
Bun.serve({
|
|
65
|
+
async fetch(request) {
|
|
66
|
+
return mw(request, async () => {
|
|
67
|
+
return new Response('<h1>Hello</h1>', {
|
|
68
|
+
headers: { 'content-type': 'text/html' }
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
# Normal HTML response
|
|
77
|
+
curl http://localhost:3000
|
|
78
|
+
|
|
79
|
+
# Markdown response for AI agents
|
|
80
|
+
curl -H "Accept: text/markdown" http://localhost:3000
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Options
|
|
84
|
+
|
|
85
|
+
Accepts all [`markdown-for-agents` options](https://www.npmjs.com/package/markdown-for-agents#options):
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
const mw = markdownMiddleware({
|
|
89
|
+
// Strip nav, ads, sidebars, cookie banners
|
|
90
|
+
extract: true,
|
|
91
|
+
|
|
92
|
+
// Resolve relative URLs
|
|
93
|
+
baseUrl: 'https://example.com',
|
|
94
|
+
|
|
95
|
+
// Remove duplicate content blocks
|
|
96
|
+
deduplicate: true,
|
|
97
|
+
|
|
98
|
+
// Custom token counter (e.g. tiktoken)
|
|
99
|
+
tokenCounter: text => ({ tokens: enc.encode(text).length, characters: text.length, words: text.split(/\s+/).filter(Boolean).length })
|
|
100
|
+
});
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Other frameworks
|
|
104
|
+
|
|
105
|
+
| Package | Framework |
|
|
106
|
+
| -------------------------------------------------------------------------------------------- | --------- |
|
|
107
|
+
| [`@markdown-for-agents/express`](https://www.npmjs.com/package/@markdown-for-agents/express) | Express |
|
|
108
|
+
| [`@markdown-for-agents/fastify`](https://www.npmjs.com/package/@markdown-for-agents/fastify) | Fastify |
|
|
109
|
+
| [`@markdown-for-agents/hono`](https://www.npmjs.com/package/@markdown-for-agents/hono) | Hono |
|
|
110
|
+
| [`@markdown-for-agents/nextjs`](https://www.npmjs.com/package/@markdown-for-agents/nextjs) | Next.js |
|
|
111
|
+
|
|
112
|
+
## License
|
|
113
|
+
|
|
114
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@markdown-for-agents/web",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
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.
|
|
23
|
+
"markdown-for-agents": "0.1.2"
|
|
24
24
|
},
|
|
25
25
|
"keywords": [
|
|
26
26
|
"html",
|