webforai 0.0.6 → 0.0.8

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 ADDED
@@ -0,0 +1,61 @@
1
+ # Web for AI
2
+
3
+ A library that provides a web interface for AI
4
+
5
+ ## Features
6
+
7
+ - Ultra-precise HTML2Markdown conversion
8
+ - Ultra-precise Markdown segmentation based on AST
9
+ - Ultra-precise HTML retrieval functions using headless browsers
10
+ - The core functionality is edge-native (runs on Cloudflare Workers!!!)
11
+
12
+ ## Demo
13
+
14
+ There is a demo API for Html2Markdown deployed on CloudflareWorker. Please access the following link
15
+
16
+ - [https://site-reader-api.inaridiy.workers.dev/?url=https://www.npmjs.com/package/webforai](https://site-reader-api.inaridiy.workers.dev/?url=https://www.npmjs.com/package/webforai)
17
+ - [https://site-reader-api.inaridiy.workers.dev/?html=%3Ch1%3EHello%20World%3C/h1%3E](https://site-reader-api.inaridiy.workers.dev/?html=%3Ch1%3EHello%20World%3C/h1%3E)
18
+
19
+ ## Quick Start
20
+
21
+ Just install and execute scripts
22
+
23
+ ```bash
24
+ pnpm i webforai playwright
25
+ ```
26
+
27
+ ```js
28
+ import { promises as fs } from "fs";
29
+ import { htmlToMarkdown, htmlToMdast } from "webforai";
30
+ import { loadHtml } from "webforai/loaders/playwright";
31
+
32
+ const url = "https://www.npmjs.com/package/webforai";
33
+ const html = await loadHtml(url);
34
+
35
+ const markdown = htmlToMarkdown(html, { solveLinks: url });
36
+
37
+ await fs.writeFile("output.md", markdown);
38
+ ```
39
+
40
+ other examples are in [examples](./examples/src/index.ts)
41
+
42
+ ## Usage
43
+
44
+ ### Main Functions
45
+
46
+ **`htmlToMarkdown(html: string, options?: HtmlToMarkdownOptions): string`**
47
+ Convert HTML to Markdown. By default, unnecessary HTML is excluded and processed.
48
+ If `solveLinks` is specified, the relative links in the Mdast will be resolved.
49
+ This function just calls htmlToMdast and mdastToMarkdown in that order internally.
50
+
51
+ **`htmlToMdast(html: string, options?: HtmlToMdastOptions): Mdast`**
52
+ This project uses Hast and Mdast as defined by syntax-tree internally.
53
+ This function converts HTML to Mdast, an intermediate representation, which is required when using `mdastSplitter`, etc.
54
+
55
+ **`mdastToMarkdown(mdast: Mdast | RootContent[], options?: { solveLinks?: string }): string`**
56
+ Convert Mdast to Markdown. If `solveLinks` is specified, the relative links in the Mdast will be resolved.
57
+
58
+ ### Loader Functions
59
+
60
+ **`loadHtml(url: string, options?: LoadHtmlOptions): Promise<string>`**
61
+ Load HTML from the specified URL. This function uses Playwright internally.
@@ -1,5 +1,5 @@
1
- import type { Nodes as Mdast } from "mdast";
2
- export declare const mdastToMarkdown: (mdast: Mdast, options?: {
1
+ import type { Nodes as Mdast, RootContent } from "mdast";
2
+ export declare const mdastToMarkdown: (mdast: Mdast | RootContent[], options?: {
3
3
  solveLinks?: string;
4
4
  }) => string;
5
5
  //# sourceMappingURL=mdast-to-markdown.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"mdast-to-markdown.d.ts","sourceRoot":"","sources":["../src/mdast-to-markdown.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,IAAI,KAAK,EAAE,MAAM,OAAO,CAAC;AAQ5C,eAAO,MAAM,eAAe,UAAW,KAAK,YAAY;IAAE,UAAU,CAAC,EAAE,MAAM,CAAA;CAAE,KAAG,MAQjF,CAAC"}
1
+ {"version":3,"file":"mdast-to-markdown.d.ts","sourceRoot":"","sources":["../src/mdast-to-markdown.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,IAAI,KAAK,EAAE,WAAW,EAAE,MAAM,OAAO,CAAC;AASzD,eAAO,MAAM,eAAe,UAAW,KAAK,GAAG,WAAW,EAAE,YAAY;IAAE,UAAU,CAAC,EAAE,MAAM,CAAA;CAAE,KAAG,MAQjG,CAAC"}
@@ -5,8 +5,9 @@ const mdast_util_gfm_1 = require("mdast-util-gfm");
5
5
  const mdast_util_math_1 = require("mdast-util-math");
6
6
  const mdast_util_to_markdown_1 = require("mdast-util-to-markdown");
7
7
  const link_replacer_1 = require("./link-replacer");
8
+ const mdast_utils_1 = require("./utils/mdast-utils");
8
9
  const mdastToMarkdown = (mdast, options) => {
9
- let markdown = (0, mdast_util_to_markdown_1.toMarkdown)(mdast, {
10
+ let markdown = (0, mdast_util_to_markdown_1.toMarkdown)((0, mdast_utils_1.warpRoot)(mdast), {
10
11
  extensions: [(0, mdast_util_gfm_1.gfmToMarkdown)(), (0, mdast_util_math_1.mathToMarkdown)()],
11
12
  });
12
13
  if (options?.solveLinks)
@@ -14,4 +15,4 @@ const mdastToMarkdown = (mdast, options) => {
14
15
  return markdown;
15
16
  };
16
17
  exports.mdastToMarkdown = mdastToMarkdown;
17
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibWRhc3QtdG8tbWFya2Rvd24uanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvbWRhc3QtdG8tbWFya2Rvd24udHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7O0FBRUEsbURBQStDO0FBQy9DLHFEQUFpRDtBQUNqRCxtRUFBb0Q7QUFFcEQsbURBQStDO0FBRXhDLE1BQU0sZUFBZSxHQUFHLENBQUMsS0FBWSxFQUFFLE9BQWlDLEVBQVUsRUFBRTtJQUMxRixJQUFJLFFBQVEsR0FBRyxJQUFBLG1DQUFVLEVBQUMsS0FBSyxFQUFFO1FBQ2hDLFVBQVUsRUFBRSxDQUFDLElBQUEsOEJBQWEsR0FBRSxFQUFFLElBQUEsZ0NBQWMsR0FBRSxDQUFDO0tBQy9DLENBQUMsQ0FBQztJQUVILElBQUksT0FBTyxFQUFFLFVBQVU7UUFBRSxRQUFRLEdBQUcsSUFBQSw0QkFBWSxFQUFDLFFBQVEsRUFBRSxPQUFPLENBQUMsVUFBVSxDQUFDLENBQUM7SUFFL0UsT0FBTyxRQUFRLENBQUM7QUFDakIsQ0FBQyxDQUFDO0FBUlcsUUFBQSxlQUFlLG1CQVExQiJ9
18
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibWRhc3QtdG8tbWFya2Rvd24uanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvbWRhc3QtdG8tbWFya2Rvd24udHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7O0FBRUEsbURBQStDO0FBQy9DLHFEQUFpRDtBQUNqRCxtRUFBb0Q7QUFFcEQsbURBQStDO0FBQy9DLHFEQUErQztBQUV4QyxNQUFNLGVBQWUsR0FBRyxDQUFDLEtBQTRCLEVBQUUsT0FBaUMsRUFBVSxFQUFFO0lBQzFHLElBQUksUUFBUSxHQUFHLElBQUEsbUNBQVUsRUFBQyxJQUFBLHNCQUFRLEVBQUMsS0FBSyxDQUFDLEVBQUU7UUFDMUMsVUFBVSxFQUFFLENBQUMsSUFBQSw4QkFBYSxHQUFFLEVBQUUsSUFBQSxnQ0FBYyxHQUFFLENBQUM7S0FDL0MsQ0FBQyxDQUFDO0lBRUgsSUFBSSxPQUFPLEVBQUUsVUFBVTtRQUFFLFFBQVEsR0FBRyxJQUFBLDRCQUFZLEVBQUMsUUFBUSxFQUFFLE9BQU8sQ0FBQyxVQUFVLENBQUMsQ0FBQztJQUUvRSxPQUFPLFFBQVEsQ0FBQztBQUNqQixDQUFDLENBQUM7QUFSVyxRQUFBLGVBQWUsbUJBUTFCIn0=
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webforai",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "description": "",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,13 +1,14 @@
1
- import type { Nodes as Mdast } from "mdast";
1
+ import type { Nodes as Mdast, RootContent } from "mdast";
2
2
 
3
3
  import { gfmToMarkdown } from "mdast-util-gfm";
4
4
  import { mathToMarkdown } from "mdast-util-math";
5
5
  import { toMarkdown } from "mdast-util-to-markdown";
6
6
 
7
7
  import { linkReplacer } from "./link-replacer";
8
+ import { warpRoot } from "./utils/mdast-utils";
8
9
 
9
- export const mdastToMarkdown = (mdast: Mdast, options?: { solveLinks?: string }): string => {
10
- let markdown = toMarkdown(mdast, {
10
+ export const mdastToMarkdown = (mdast: Mdast | RootContent[], options?: { solveLinks?: string }): string => {
11
+ let markdown = toMarkdown(warpRoot(mdast), {
11
12
  extensions: [gfmToMarkdown(), mathToMarkdown()],
12
13
  });
13
14