@markdown-for-agents/web 0.1.1
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/LICENSE +21 -0
- package/dist/index.d.mts +27 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +45 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Konstantin Konstantinov
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { MiddlewareOptions, MiddlewareOptions as MiddlewareOptions$1 } from "markdown-for-agents";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
type Handler = (request: Request) => Response | Promise<Response>;
|
|
5
|
+
type Middleware = (request: Request, next: Handler) => Response | Promise<Response>;
|
|
6
|
+
/**
|
|
7
|
+
* Web-standard middleware that converts HTML responses to markdown
|
|
8
|
+
* when the client sends an `Accept: text/markdown` header.
|
|
9
|
+
*
|
|
10
|
+
* Compatible with any runtime that supports the Fetch API
|
|
11
|
+
* (Cloudflare Workers, Deno, Bun, etc.).
|
|
12
|
+
*
|
|
13
|
+
* @param options - Conversion and middleware options.
|
|
14
|
+
* @returns A middleware function that wraps a {@link Handler}.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { markdownMiddleware } from "@markdown-for-agents/web";
|
|
19
|
+
*
|
|
20
|
+
* const md = markdownMiddleware({ extract: true });
|
|
21
|
+
* const response = await md(request, handler);
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
declare function markdownMiddleware(options?: MiddlewareOptions$1): Middleware;
|
|
25
|
+
//#endregion
|
|
26
|
+
export { type MiddlewareOptions, markdownMiddleware };
|
|
27
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"mappings":";;;KAKK,OAAA,IAAW,OAAA,EAAS,OAAA,KAAY,QAAA,GAAW,OAAA,CAAQ,QAAA;AAAA,KACnD,UAAA,IAAc,OAAA,EAAS,OAAA,EAAS,IAAA,EAAM,OAAA,KAAY,QAAA,GAAW,OAAA,CAAQ,QAAA;;;;;;;;;;;;;;;;AADV;;;iBA0BhD,kBAAA,CAAmB,OAAA,GAAU,mBAAA,GAAoB,UAAA"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { convert } from "markdown-for-agents";
|
|
2
|
+
|
|
3
|
+
//#region src/index.ts
|
|
4
|
+
function wantsMarkdown(request) {
|
|
5
|
+
return (request.headers.get("accept") ?? "").includes("text/markdown");
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Web-standard middleware that converts HTML responses to markdown
|
|
9
|
+
* when the client sends an `Accept: text/markdown` header.
|
|
10
|
+
*
|
|
11
|
+
* Compatible with any runtime that supports the Fetch API
|
|
12
|
+
* (Cloudflare Workers, Deno, Bun, etc.).
|
|
13
|
+
*
|
|
14
|
+
* @param options - Conversion and middleware options.
|
|
15
|
+
* @returns A middleware function that wraps a {@link Handler}.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { markdownMiddleware } from "@markdown-for-agents/web";
|
|
20
|
+
*
|
|
21
|
+
* const md = markdownMiddleware({ extract: true });
|
|
22
|
+
* const response = await md(request, handler);
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
function markdownMiddleware(options) {
|
|
26
|
+
const tokenHeader = options?.tokenHeader ?? "x-markdown-tokens";
|
|
27
|
+
return async (request, next) => {
|
|
28
|
+
if (!wantsMarkdown(request)) return next(request);
|
|
29
|
+
const response = await next(request);
|
|
30
|
+
if (!(response.headers.get("content-type") ?? "").includes("text/html")) return response;
|
|
31
|
+
const { markdown, tokenEstimate } = convert(await response.text(), options);
|
|
32
|
+
const headers = new Headers(response.headers);
|
|
33
|
+
headers.set("content-type", "text/markdown; charset=utf-8");
|
|
34
|
+
headers.set(tokenHeader, String(tokenEstimate.tokens));
|
|
35
|
+
return new Response(markdown, {
|
|
36
|
+
status: response.status,
|
|
37
|
+
statusText: response.statusText,
|
|
38
|
+
headers
|
|
39
|
+
});
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
//#endregion
|
|
44
|
+
export { markdownMiddleware };
|
|
45
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +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"}
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@markdown-for-agents/web",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Web Standard (Request/Response) middleware for markdown-for-agents",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=20"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist",
|
|
12
|
+
"LICENSE"
|
|
13
|
+
],
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/index.d.mts",
|
|
17
|
+
"import": "./dist/index.mjs"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"main": "./dist/index.mjs",
|
|
21
|
+
"types": "./dist/index.d.mts",
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"markdown-for-agents": "0.1.1"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"html",
|
|
27
|
+
"markdown",
|
|
28
|
+
"middleware",
|
|
29
|
+
"web",
|
|
30
|
+
"fetch",
|
|
31
|
+
"request",
|
|
32
|
+
"response"
|
|
33
|
+
],
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "git+https://github.com/KKonstantinov/markdown-for-agents.git",
|
|
37
|
+
"directory": "packages/middleware/web"
|
|
38
|
+
},
|
|
39
|
+
"homepage": "https://github.com/KKonstantinov/markdown-for-agents/tree/main/packages/middleware/web#readme",
|
|
40
|
+
"bugs": {
|
|
41
|
+
"url": "https://github.com/KKonstantinov/markdown-for-agents/issues"
|
|
42
|
+
},
|
|
43
|
+
"publishConfig": {
|
|
44
|
+
"access": "public"
|
|
45
|
+
},
|
|
46
|
+
"license": "MIT",
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build": "tsdown",
|
|
49
|
+
"dev": "tsdown --watch",
|
|
50
|
+
"test": "vitest run",
|
|
51
|
+
"test:watch": "vitest",
|
|
52
|
+
"test:integration": "vitest run test/integration",
|
|
53
|
+
"typecheck": "tsc --noEmit"
|
|
54
|
+
}
|
|
55
|
+
}
|