webforai 1.5.1 → 1.6.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/dist/bin.js +258 -278
- package/dist/cjs/extract-hast/index.js +4 -3
- package/dist/cjs/extract-hast/readability.js +28 -61
- package/dist/cjs/extract-hast/strongly.js +2 -1
- package/dist/cjs/extract-hast/utils.js +0 -15
- package/dist/cjs/html-to-mdast.js +6 -6
- package/dist/cjs/link-replacer.js +10 -5
- package/dist/cjs/loaders/playwright.js +36 -7
- package/dist/cjs/{cli/constants.js → mdast-handlers/custom-br-handler.js} +9 -12
- package/dist/cjs/mdast-to-markdown.js +1 -1
- package/dist/cjs/utils/hast-utils.js +71 -0
- package/dist/extract-hast/index.js +4 -3
- package/dist/extract-hast/readability.js +29 -62
- package/dist/extract-hast/strongly.js +2 -1
- package/dist/extract-hast/utils.js +0 -14
- package/dist/html-to-mdast.js +6 -6
- package/dist/link-replacer.js +10 -5
- package/dist/loaders/playwright.js +36 -7
- package/dist/mdast-handlers/custom-br-handler.js +8 -0
- package/dist/mdast-to-markdown.js +1 -1
- package/dist/types/cli/bin.js.map +1 -1
- package/dist/types/cli/commands/webforai/index.d.ts +6 -0
- package/dist/types/cli/commands/webforai/index.js.map +1 -0
- package/dist/types/cli/commands/webforai/loadHtml.d.ts +3 -0
- package/dist/types/cli/commands/webforai/loadHtml.js.map +1 -0
- package/dist/types/extract-hast/index.d.ts +9 -4
- package/dist/types/extract-hast/index.js.map +1 -1
- package/dist/types/extract-hast/readability.d.ts +2 -1
- package/dist/types/extract-hast/readability.js.map +1 -1
- package/dist/types/extract-hast/strongly.d.ts +2 -1
- package/dist/types/extract-hast/strongly.js.map +1 -1
- package/dist/types/extract-hast/utils.d.ts +0 -1
- package/dist/types/extract-hast/utils.js.map +1 -1
- package/dist/types/html-to-mdast.d.ts +4 -0
- package/dist/types/html-to-mdast.js.map +1 -1
- package/dist/types/link-replacer.d.ts +1 -1
- package/dist/types/link-replacer.js.map +1 -1
- package/dist/types/loaders/playwright.d.ts +8 -2
- package/dist/types/loaders/playwright.js.map +1 -1
- package/dist/types/mdast-handlers/custom-br-handler.d.ts +2 -0
- package/dist/types/mdast-handlers/custom-br-handler.js.map +1 -0
- package/dist/types/mdast-to-markdown.js.map +1 -1
- package/dist/types/utils/hast-utils.d.ts +4 -0
- package/dist/types/utils/hast-utils.js.map +1 -0
- package/dist/utils/hast-utils.js +46 -0
- package/package.json +22 -10
- package/dist/cjs/cli/bin.js +0 -34
- package/dist/cjs/cli/commands/handlers/index.js +0 -84
- package/dist/cjs/cli/commands/handlers/loadHtml.js +0 -127
- package/dist/cjs/cli/helpers/assertContinue.js +0 -34
- package/dist/cjs/cli/helpers/inputOutputPath.js +0 -89
- package/dist/cjs/cli/helpers/inputSourcePath.js +0 -64
- package/dist/cjs/cli/helpers/selectExtractMode.js +0 -39
- package/dist/cjs/cli/helpers/selectLoader.js +0 -44
- package/dist/cjs/cli/utils.js +0 -122
- package/dist/cjs/html-to-markdown.test.js +0 -118
- package/dist/cjs/link-replacer.test.js +0 -47
- package/dist/cjs/loaders/fetch.test.js +0 -9
- package/dist/cjs/loaders/playwright.test.js +0 -17
- package/dist/cjs/loaders/puppeteer.test.js +0 -17
- package/dist/cli/bin.js +0 -12
- package/dist/cli/commands/handlers/index.js +0 -51
- package/dist/cli/commands/handlers/loadHtml.js +0 -94
- package/dist/cli/constants.js +0 -9
- package/dist/cli/helpers/assertContinue.js +0 -11
- package/dist/cli/helpers/inputOutputPath.js +0 -56
- package/dist/cli/helpers/inputSourcePath.js +0 -31
- package/dist/cli/helpers/selectExtractMode.js +0 -16
- package/dist/cli/helpers/selectLoader.js +0 -21
- package/dist/cli/utils.js +0 -85
- package/dist/html-to-markdown.test.js +0 -118
- package/dist/link-replacer.test.js +0 -47
- package/dist/loaders/fetch.test.js +0 -9
- package/dist/loaders/playwright.test.js +0 -17
- package/dist/loaders/puppeteer.test.js +0 -17
- package/dist/types/cli/commands/handlers/index.d.ts +0 -13
- package/dist/types/cli/commands/handlers/index.js.map +0 -1
- package/dist/types/cli/commands/handlers/loadHtml.d.ts +0 -2
- package/dist/types/cli/commands/handlers/loadHtml.js.map +0 -1
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var import_vitest = require("vitest");
|
|
3
|
-
var import_link_replacer = require("./link-replacer");
|
|
4
|
-
const markdown = `# Hello, world!
|
|
5
|
-
|
|
6
|
-
This is a paragraph.
|
|
7
|
-
|
|
8
|
-
[Example](/example.html)
|
|
9
|
-
|
|
10
|
-

|
|
11
|
-
|
|
12
|
-
[Absolute Link](https://www.google.com)
|
|
13
|
-
|
|
14
|
-
[Link with hash](/page#hash)
|
|
15
|
-
|
|
16
|
-
[Link with query](/page?query=string)`;
|
|
17
|
-
const expected = `# Hello, world!
|
|
18
|
-
|
|
19
|
-
This is a paragraph.
|
|
20
|
-
|
|
21
|
-
[Example](https://example.com/example.html)
|
|
22
|
-
|
|
23
|
-

|
|
24
|
-
|
|
25
|
-
[Absolute Link](https://www.google.com)
|
|
26
|
-
|
|
27
|
-
[Link with hash](https://example.com/page#hash)
|
|
28
|
-
|
|
29
|
-
[Link with query](https://example.com/page?query=string)`;
|
|
30
|
-
(0, import_vitest.describe)("linkReplacer", () => {
|
|
31
|
-
(0, import_vitest.it)("should replace relative links", () => {
|
|
32
|
-
const replaced = (0, import_link_replacer.linkReplacer)(markdown, "https://example.com");
|
|
33
|
-
(0, import_vitest.expect)(replaced).toEqual(expected);
|
|
34
|
-
});
|
|
35
|
-
(0, import_vitest.it)("should not replace absolute links", () => {
|
|
36
|
-
const replaced = (0, import_link_replacer.linkReplacer)("[Absolute Link](https://www.google.com)", "https://example.com");
|
|
37
|
-
(0, import_vitest.expect)(replaced).toEqual("[Absolute Link](https://www.google.com)");
|
|
38
|
-
});
|
|
39
|
-
(0, import_vitest.it)("should handle links with hashes", () => {
|
|
40
|
-
const replaced = (0, import_link_replacer.linkReplacer)("[Link with hash](/page#hash)", "https://example.com");
|
|
41
|
-
(0, import_vitest.expect)(replaced).toEqual("[Link with hash](https://example.com/page#hash)");
|
|
42
|
-
});
|
|
43
|
-
(0, import_vitest.it)("should handle links with query parameters", () => {
|
|
44
|
-
const replaced = (0, import_link_replacer.linkReplacer)("[Link with query](/page?query=string)", "https://example.com");
|
|
45
|
-
(0, import_vitest.expect)(replaced).toEqual("[Link with query](https://example.com/page?query=string)");
|
|
46
|
-
});
|
|
47
|
-
});
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var import_vitest = require("vitest");
|
|
3
|
-
var import_fetch = require("./fetch");
|
|
4
|
-
(0, import_vitest.describe)("Fetch loader", () => {
|
|
5
|
-
(0, import_vitest.it)("should load the HTML of a URL", async () => {
|
|
6
|
-
const html = await (0, import_fetch.loadHtml)("https://example.com");
|
|
7
|
-
(0, import_vitest.expect)(html).toContain("Example Domain");
|
|
8
|
-
});
|
|
9
|
-
});
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var import_playwright = require("playwright");
|
|
3
|
-
var import_vitest = require("vitest");
|
|
4
|
-
var import_playwright2 = require("./playwright");
|
|
5
|
-
(0, import_vitest.describe)("Playwright loader", () => {
|
|
6
|
-
(0, import_vitest.it)("should load the HTML of a URL", async () => {
|
|
7
|
-
const html = await (0, import_playwright2.loadHtml)("https://example.com");
|
|
8
|
-
(0, import_vitest.expect)(html).toContain("Example Domain");
|
|
9
|
-
});
|
|
10
|
-
(0, import_vitest.it)("should load the HTML of a URL using a custom context", async () => {
|
|
11
|
-
const context = await import_playwright.chromium.launch({ headless: true });
|
|
12
|
-
const html = await (0, import_playwright2.loadHtml)("https://example.com", context);
|
|
13
|
-
(0, import_vitest.expect)(html).toContain("Example Domain");
|
|
14
|
-
(0, import_vitest.expect)(context.isConnected()).toBe(true);
|
|
15
|
-
await context.close();
|
|
16
|
-
});
|
|
17
|
-
});
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var import_playwright = require("playwright");
|
|
3
|
-
var import_vitest = require("vitest");
|
|
4
|
-
var import_puppeteer = require("./puppeteer");
|
|
5
|
-
(0, import_vitest.describe)("Puppeteer loader", () => {
|
|
6
|
-
(0, import_vitest.it)("should load the HTML of a URL", async () => {
|
|
7
|
-
const html = await (0, import_puppeteer.loadHtml)("https://example.com");
|
|
8
|
-
(0, import_vitest.expect)(html).toContain("Example Domain");
|
|
9
|
-
});
|
|
10
|
-
(0, import_vitest.it)("should load the HTML of a URL using a custom puppeteer context", async () => {
|
|
11
|
-
const context = await import_playwright.chromium.launch({ headless: true });
|
|
12
|
-
const html = await (0, import_puppeteer.loadHtml)("https://example.com", { headless: true });
|
|
13
|
-
(0, import_vitest.expect)(html).toContain("Example Domain");
|
|
14
|
-
(0, import_vitest.expect)(context.isConnected()).toBe(true);
|
|
15
|
-
await context.close();
|
|
16
|
-
});
|
|
17
|
-
});
|
package/dist/cli/bin.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
// src/cli/bin.ts
|
|
2
|
-
import { intro } from "@clack/prompts";
|
|
3
|
-
import { program } from "commander";
|
|
4
|
-
import pc from "picocolors";
|
|
5
|
-
import packageInfo from "../../package.json";
|
|
6
|
-
import { webforaiCommand } from "./commands/handlers/index.js";
|
|
7
|
-
import { LOADERS, MODES } from "./constants.js";
|
|
8
|
-
program.name("webforai").description("CLI tool for ultra-precise HTML to Markdown conversion").version(packageInfo.version, "-v, --version", "output the current version").argument("[source]", "URL or path to process").argument("[outputPath]", "Path to output file or directory").option("--mode <mode>", `Processing mode (${MODES.join(", ")})`).option("--loader <loader>", `Loader to use (${LOADERS.join(", ")})`).option("--baseUrl <baseUrl>", "Base URL for relative links").option("-o, --stdout", "output to stdout").option("-d, --debug", "output extra debugging information").on("option:debug", () => {
|
|
9
|
-
process.env.DEBUG = "true";
|
|
10
|
-
}).action(webforaiCommand);
|
|
11
|
-
intro(pc.inverse(`webforai CLI version ${packageInfo.version}`));
|
|
12
|
-
program.parse();
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
// src/cli/commands/handlers/index.ts
|
|
2
|
-
import fs from "node:fs";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
import { outro } from "@clack/prompts";
|
|
5
|
-
import { htmlToMarkdown } from "../../../html-to-markdown.js";
|
|
6
|
-
import { inputOutputPath } from "../../helpers/inputOutputPath.js";
|
|
7
|
-
import { inputSourcePath } from "../../helpers/inputSourcePath.js";
|
|
8
|
-
import { selectExtractMode } from "../../helpers/selectExtractMode.js";
|
|
9
|
-
import { selectLoader } from "../../helpers/selectLoader.js";
|
|
10
|
-
import { isUrl } from "../../utils.js";
|
|
11
|
-
import { loadHtml } from "./loadHtml.js";
|
|
12
|
-
var aiModeOptions = { linkAsText: true, tableAsText: true, hideImage: true };
|
|
13
|
-
var readabilityModeOptions = { linkAsText: false, tableAsText: false, hideImage: false };
|
|
14
|
-
var webforaiCommand = async (initialPath, initialOutputPath, options) => {
|
|
15
|
-
const sourcePath = initialPath ?? await inputSourcePath();
|
|
16
|
-
if (options.debug) {
|
|
17
|
-
console.debug(`sourcePath: ${sourcePath}`);
|
|
18
|
-
}
|
|
19
|
-
const loader = isUrl(sourcePath) ? { type: "web", loader: options.loader ?? await selectLoader() } : { type: "local", loader: null };
|
|
20
|
-
if (options.debug) {
|
|
21
|
-
console.debug(`loader: ${loader}`);
|
|
22
|
-
}
|
|
23
|
-
const outputPath = initialOutputPath ?? await inputOutputPath(sourcePath);
|
|
24
|
-
if (options.debug) {
|
|
25
|
-
console.debug(`outputPath: ${outputPath}`);
|
|
26
|
-
}
|
|
27
|
-
const mode = options.mode ?? await selectExtractMode();
|
|
28
|
-
if (options.debug) {
|
|
29
|
-
console.debug(`mode: ${mode}`);
|
|
30
|
-
}
|
|
31
|
-
const content = await loadHtml(sourcePath, loader);
|
|
32
|
-
if (options.debug) {
|
|
33
|
-
console.debug(`content: ${content}`);
|
|
34
|
-
}
|
|
35
|
-
const markdown = htmlToMarkdown(content, {
|
|
36
|
-
baseUrl: isUrl(sourcePath) ? sourcePath : options.baseUrl,
|
|
37
|
-
...mode === "ai" ? aiModeOptions : readabilityModeOptions
|
|
38
|
-
});
|
|
39
|
-
if (options.debug) {
|
|
40
|
-
console.debug(`markdown: ${markdown}`);
|
|
41
|
-
}
|
|
42
|
-
const directory = path.dirname(outputPath);
|
|
43
|
-
if (!fs.existsSync(directory)) {
|
|
44
|
-
fs.mkdirSync(directory, { recursive: true });
|
|
45
|
-
}
|
|
46
|
-
fs.writeFileSync(outputPath, markdown);
|
|
47
|
-
outro("Markdown conversion is complete!");
|
|
48
|
-
};
|
|
49
|
-
export {
|
|
50
|
-
webforaiCommand
|
|
51
|
-
};
|
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
// src/cli/commands/handlers/loadHtml.ts
|
|
2
|
-
import fs from "node:fs/promises";
|
|
3
|
-
import { cancel, spinner as clackSpinner, confirm, spinner } from "@clack/prompts";
|
|
4
|
-
import pc from "picocolors";
|
|
5
|
-
import { $ } from "zx";
|
|
6
|
-
var loadHtml = async (sourcePath, loader) => {
|
|
7
|
-
const loadSpinner = clackSpinner();
|
|
8
|
-
try {
|
|
9
|
-
switch (loader.type) {
|
|
10
|
-
case "local":
|
|
11
|
-
return await loadLocalHtml(sourcePath, loadSpinner);
|
|
12
|
-
case "web":
|
|
13
|
-
switch (loader.loader) {
|
|
14
|
-
case "fetch":
|
|
15
|
-
return await loadFetchHtml(sourcePath, loadSpinner);
|
|
16
|
-
case "playwright":
|
|
17
|
-
return await loadPlaywrightHtml(sourcePath, loadSpinner);
|
|
18
|
-
default:
|
|
19
|
-
throw new Error("Invalid loader");
|
|
20
|
-
}
|
|
21
|
-
default:
|
|
22
|
-
throw new Error("Invalid loader type");
|
|
23
|
-
}
|
|
24
|
-
} catch (error) {
|
|
25
|
-
loadSpinner.stop(pc.red("Content loading failed!"));
|
|
26
|
-
console.error(error);
|
|
27
|
-
process.exit(1);
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
var loadLocalHtml = async (sourcePath, loadSpinner) => {
|
|
31
|
-
loadSpinner.start("Retrieving content.");
|
|
32
|
-
try {
|
|
33
|
-
await fs.access(sourcePath);
|
|
34
|
-
} catch {
|
|
35
|
-
throw new Error("The file does not exist");
|
|
36
|
-
}
|
|
37
|
-
const stats = await fs.stat(sourcePath);
|
|
38
|
-
if (!stats.isFile()) {
|
|
39
|
-
throw new Error("The source must be a file");
|
|
40
|
-
}
|
|
41
|
-
const content = await fs.readFile(sourcePath, "utf-8");
|
|
42
|
-
loadSpinner.stop("Content retrieval is complete!");
|
|
43
|
-
return content;
|
|
44
|
-
};
|
|
45
|
-
var loadFetchHtml = async (sourcePath, loadSpinner) => {
|
|
46
|
-
loadSpinner.start("Downloading content.");
|
|
47
|
-
const { loadHtml: loadHtml2 } = await import("../../../loaders/fetch.js");
|
|
48
|
-
const content = await loadHtml2(sourcePath);
|
|
49
|
-
loadSpinner.stop("Content download is complete!");
|
|
50
|
-
return content;
|
|
51
|
-
};
|
|
52
|
-
var loadPlaywrightHtml = async (sourcePath, loadSpinner) => {
|
|
53
|
-
const launchSpinner = clackSpinner();
|
|
54
|
-
launchSpinner.start("Launching playwright.");
|
|
55
|
-
const playwright = await import("playwright");
|
|
56
|
-
let browser;
|
|
57
|
-
try {
|
|
58
|
-
browser = await playwright.chromium.launch();
|
|
59
|
-
launchSpinner.stop("Playwright launched!");
|
|
60
|
-
} catch (error) {
|
|
61
|
-
await handlePlaywrightLaunchError(error);
|
|
62
|
-
browser = await playwright.chromium.launch();
|
|
63
|
-
}
|
|
64
|
-
loadSpinner.start("Loading content.");
|
|
65
|
-
const { loadHtml: loadHtml2 } = await import("../../../loaders/playwright.js");
|
|
66
|
-
const content = await loadHtml2(sourcePath, browser);
|
|
67
|
-
loadSpinner.stop("Content download is complete!");
|
|
68
|
-
await browser.close();
|
|
69
|
-
return content;
|
|
70
|
-
};
|
|
71
|
-
var handlePlaywrightLaunchError = async (error) => {
|
|
72
|
-
if (!error.message.includes("npx playwright install")) {
|
|
73
|
-
throw error;
|
|
74
|
-
}
|
|
75
|
-
const isInstall = await confirm({
|
|
76
|
-
message: "To continue, you will need to download a browser. Do you wish to continue?"
|
|
77
|
-
});
|
|
78
|
-
if (!isInstall) {
|
|
79
|
-
cancel("End of process.");
|
|
80
|
-
process.exit(1);
|
|
81
|
-
}
|
|
82
|
-
const installDepsSpinner = spinner();
|
|
83
|
-
installDepsSpinner.start("Installing browser dependencies.");
|
|
84
|
-
try {
|
|
85
|
-
await $`npx playwright install`;
|
|
86
|
-
installDepsSpinner.stop("Browser dependencies installed!");
|
|
87
|
-
} catch (error2) {
|
|
88
|
-
installDepsSpinner.stop(pc.red("Browser dependencies failed to install!"));
|
|
89
|
-
throw error2;
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
export {
|
|
93
|
-
loadHtml
|
|
94
|
-
};
|
package/dist/cli/constants.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
// src/cli/helpers/assertContinue.ts
|
|
2
|
-
import { cancel, isCancel } from "@clack/prompts";
|
|
3
|
-
function assertContinue(message, cancelMessage = "Canceled.") {
|
|
4
|
-
if (isCancel(message)) {
|
|
5
|
-
cancel(cancelMessage);
|
|
6
|
-
process.exit(1);
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
export {
|
|
10
|
-
assertContinue
|
|
11
|
-
};
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
// src/cli/helpers/inputOutputPath.ts
|
|
2
|
-
import fs from "node:fs";
|
|
3
|
-
import { confirm, text } from "@clack/prompts";
|
|
4
|
-
import { getNextAvailableFilePath, sourcePathToOutputPath } from "../utils.js";
|
|
5
|
-
import { assertContinue } from "./assertContinue.js";
|
|
6
|
-
var inputOutputPath = async (sourcePath) => {
|
|
7
|
-
const outputPath = await text({
|
|
8
|
-
message: "Enter the output file path:",
|
|
9
|
-
placeholder: "output.md",
|
|
10
|
-
initialValue: sourcePathToOutputPath(sourcePath),
|
|
11
|
-
validate: (value) => {
|
|
12
|
-
if (value.trim() === "") {
|
|
13
|
-
return "Output path is required";
|
|
14
|
-
}
|
|
15
|
-
if (fs.existsSync(value) && fs.statSync(value).isDirectory()) {
|
|
16
|
-
return "No directory can be specified.";
|
|
17
|
-
}
|
|
18
|
-
if (!fs.existsSync(value) && value.endsWith("/")) {
|
|
19
|
-
return "No directory can be specified.";
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
});
|
|
23
|
-
assertContinue(outputPath);
|
|
24
|
-
if (!fs.existsSync(outputPath)) {
|
|
25
|
-
return outputPath;
|
|
26
|
-
}
|
|
27
|
-
const isOutputFileOverwrite = await confirm({
|
|
28
|
-
message: "The file already exists. Overwrite?",
|
|
29
|
-
initialValue: false
|
|
30
|
-
});
|
|
31
|
-
assertContinue(isOutputFileOverwrite);
|
|
32
|
-
if (isOutputFileOverwrite) {
|
|
33
|
-
return outputPath;
|
|
34
|
-
}
|
|
35
|
-
const escapedOutputPath = await text({
|
|
36
|
-
message: "Enter the output file path:",
|
|
37
|
-
placeholder: "output.md",
|
|
38
|
-
initialValue: getNextAvailableFilePath(outputPath),
|
|
39
|
-
validate: (value) => {
|
|
40
|
-
if (value.trim() === "") {
|
|
41
|
-
return "Output path is required";
|
|
42
|
-
}
|
|
43
|
-
if (fs.existsSync(value)) {
|
|
44
|
-
return "The file already exists";
|
|
45
|
-
}
|
|
46
|
-
if (!fs.existsSync(value) && value.endsWith("/")) {
|
|
47
|
-
return "No directory can be specified.";
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
assertContinue(escapedOutputPath);
|
|
52
|
-
return escapedOutputPath;
|
|
53
|
-
};
|
|
54
|
-
export {
|
|
55
|
-
inputOutputPath
|
|
56
|
-
};
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
// src/cli/helpers/inputSourcePath.ts
|
|
2
|
-
import fs from "node:fs";
|
|
3
|
-
import { text } from "@clack/prompts";
|
|
4
|
-
import { DEFAULT_PATH } from "../constants.js";
|
|
5
|
-
import { isUrl } from "../utils.js";
|
|
6
|
-
import { assertContinue } from "./assertContinue.js";
|
|
7
|
-
var inputSourcePath = async () => {
|
|
8
|
-
const result = await text({
|
|
9
|
-
message: "Enter the URL or html path to be converted to markdown:",
|
|
10
|
-
placeholder: DEFAULT_PATH,
|
|
11
|
-
initialValue: "",
|
|
12
|
-
validate: (value) => {
|
|
13
|
-
if (value.trim() === "") {
|
|
14
|
-
return "Source is required";
|
|
15
|
-
}
|
|
16
|
-
if (!isUrl(value)) {
|
|
17
|
-
if (!fs.existsSync(value)) {
|
|
18
|
-
return "It appears that you are specifying a local file, but the file cannot be found. hint: when specifying a url, start with http or https.";
|
|
19
|
-
}
|
|
20
|
-
if (fs.statSync(value).isDirectory()) {
|
|
21
|
-
return "You are specifying a local file, but you cannot specify a directory. hint: when specifying a url, start with http or https.";
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
});
|
|
26
|
-
assertContinue(result);
|
|
27
|
-
return result;
|
|
28
|
-
};
|
|
29
|
-
export {
|
|
30
|
-
inputSourcePath
|
|
31
|
-
};
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
// src/cli/helpers/selectExtractMode.ts
|
|
2
|
-
import { select } from "@clack/prompts";
|
|
3
|
-
import { MODES } from "../constants.js";
|
|
4
|
-
import { assertContinue } from "./assertContinue.js";
|
|
5
|
-
var selectExtractMode = async () => {
|
|
6
|
-
const result = await select({
|
|
7
|
-
message: "Select processing mode:",
|
|
8
|
-
options: MODES.map((mode) => ({ value: mode, label: mode })),
|
|
9
|
-
initialValue: MODES[0]
|
|
10
|
-
});
|
|
11
|
-
assertContinue(result);
|
|
12
|
-
return result;
|
|
13
|
-
};
|
|
14
|
-
export {
|
|
15
|
-
selectExtractMode
|
|
16
|
-
};
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
// src/cli/helpers/selectLoader.ts
|
|
2
|
-
import { select } from "@clack/prompts";
|
|
3
|
-
import { LOADERS } from "../constants.js";
|
|
4
|
-
import { assertContinue } from "./assertContinue.js";
|
|
5
|
-
var loadersHint = {
|
|
6
|
-
fetch: "Fetch HTML content from the given URL",
|
|
7
|
-
playwright: "Retrieve HTML content after rendering using Playwright; Playwright must be installed in advance.",
|
|
8
|
-
puppeteer: "Retrieve HTML content after rendering using Puppeteer; Puppeteer must be installed in advance."
|
|
9
|
-
};
|
|
10
|
-
var selectLoader = async () => {
|
|
11
|
-
const result = await select({
|
|
12
|
-
message: "Select loader:",
|
|
13
|
-
initialValue: "fetch",
|
|
14
|
-
options: LOADERS.map((mode) => ({ value: mode, label: mode, hint: loadersHint[mode] || "" }))
|
|
15
|
-
});
|
|
16
|
-
assertContinue(result);
|
|
17
|
-
return result;
|
|
18
|
-
};
|
|
19
|
-
export {
|
|
20
|
-
selectLoader
|
|
21
|
-
};
|
package/dist/cli/utils.js
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
// src/cli/utils.ts
|
|
2
|
-
import fs from "node:fs";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
var isUrl = (maybeUrl) => {
|
|
5
|
-
try {
|
|
6
|
-
new URL(maybeUrl);
|
|
7
|
-
return true;
|
|
8
|
-
} catch {
|
|
9
|
-
return false;
|
|
10
|
-
}
|
|
11
|
-
};
|
|
12
|
-
function changeFileExtension(filePath, newExtension) {
|
|
13
|
-
const parsedPath = filePath.split("/");
|
|
14
|
-
const fileName = parsedPath[parsedPath.length - 1];
|
|
15
|
-
const formattedNewExtension = newExtension.startsWith(".") ? newExtension : `.${newExtension}`;
|
|
16
|
-
if (fileName.startsWith(".")) {
|
|
17
|
-
const parts = fileName.split(".");
|
|
18
|
-
if (parts.length === 2) {
|
|
19
|
-
return parsedPath.slice(0, -1).concat(`${fileName}${formattedNewExtension}`).join("/");
|
|
20
|
-
}
|
|
21
|
-
parts[parts.length - 1] = newExtension.replace(/^\./, "");
|
|
22
|
-
return parsedPath.slice(0, -1).concat(parts.join(".")).join("/");
|
|
23
|
-
}
|
|
24
|
-
const lastDotIndex = fileName.lastIndexOf(".");
|
|
25
|
-
const baseName = lastDotIndex !== -1 ? fileName.slice(0, lastDotIndex) : fileName;
|
|
26
|
-
const newFileName = `${baseName}${formattedNewExtension}`;
|
|
27
|
-
parsedPath[parsedPath.length - 1] = newFileName;
|
|
28
|
-
return parsedPath.join("/");
|
|
29
|
-
}
|
|
30
|
-
function urlToFilename(url) {
|
|
31
|
-
try {
|
|
32
|
-
const urlObj = new URL(url);
|
|
33
|
-
const domainParts = urlObj.hostname.split(".").reverse().reduce((acc, part, index) => {
|
|
34
|
-
if (index === 0) {
|
|
35
|
-
return acc;
|
|
36
|
-
}
|
|
37
|
-
if (acc.length >= 2) {
|
|
38
|
-
return acc;
|
|
39
|
-
}
|
|
40
|
-
if (part === "www") {
|
|
41
|
-
return acc;
|
|
42
|
-
}
|
|
43
|
-
return [part, ...acc];
|
|
44
|
-
}, []);
|
|
45
|
-
const domainString = domainParts.reverse().join("-");
|
|
46
|
-
const pathParts = urlObj.pathname.split("/").filter(Boolean);
|
|
47
|
-
const relevantPathParts = pathParts.slice(-2);
|
|
48
|
-
const pathString = relevantPathParts.map((part) => decodeURIComponent(part)).join("-");
|
|
49
|
-
let filename = [domainString, pathString].filter(Boolean).join("-");
|
|
50
|
-
filename = filename.toLowerCase().replace(/[<>:"/\\|?*\x00-\x1F]/g, "").replace(/[\s.]+/g, "-").replace(/^-+|-+$/g, "");
|
|
51
|
-
return filename || "output";
|
|
52
|
-
} catch {
|
|
53
|
-
return "output";
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
var sourcePathToOutputPath = (sourcePath) => {
|
|
57
|
-
return isUrl(sourcePath) ? `${urlToFilename(sourcePath)}.md` : changeFileExtension(sourcePath, "md");
|
|
58
|
-
};
|
|
59
|
-
function getNextAvailableFilePath(filePath) {
|
|
60
|
-
const parsedPath = path.parse(filePath);
|
|
61
|
-
const directory = parsedPath.dir;
|
|
62
|
-
const fullName = parsedPath.base;
|
|
63
|
-
const [firstPart, ...restParts] = fullName.split(".");
|
|
64
|
-
const restName = restParts.length > 0 ? `.${restParts.join(".")}` : "";
|
|
65
|
-
const baseName = firstPart.replace(/_\d+$/, "");
|
|
66
|
-
let counter = 1;
|
|
67
|
-
let nextFilePath = filePath;
|
|
68
|
-
while (fs.existsSync(nextFilePath)) {
|
|
69
|
-
const match = firstPart.match(/_(\d+)$/);
|
|
70
|
-
if (match) {
|
|
71
|
-
counter = Number.parseInt(match[1], 10) + 1;
|
|
72
|
-
}
|
|
73
|
-
const newName = `${baseName}_${counter}${restName}`;
|
|
74
|
-
nextFilePath = path.join(directory, newName);
|
|
75
|
-
counter++;
|
|
76
|
-
}
|
|
77
|
-
return nextFilePath;
|
|
78
|
-
}
|
|
79
|
-
export {
|
|
80
|
-
changeFileExtension,
|
|
81
|
-
getNextAvailableFilePath,
|
|
82
|
-
isUrl,
|
|
83
|
-
sourcePathToOutputPath,
|
|
84
|
-
urlToFilename
|
|
85
|
-
};
|
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
// src/html-to-markdown.test.ts
|
|
2
|
-
import { distance } from "fastest-levenshtein";
|
|
3
|
-
import { describe, expect, it } from "vitest";
|
|
4
|
-
import { htmlToMarkdown } from "./html-to-markdown.js";
|
|
5
|
-
import { loadHtml } from "./loaders/fetch.js";
|
|
6
|
-
var html = `
|
|
7
|
-
<h1>Hello, world!</h1>
|
|
8
|
-
<p>This is a paragraph.</p>
|
|
9
|
-
<a href="/example.html">Example</a>
|
|
10
|
-
<img src="/example.jpg" alt="Example" />
|
|
11
|
-
<ul>
|
|
12
|
-
<li>Item 1</li>
|
|
13
|
-
<li>Item 2</li>
|
|
14
|
-
</ul>
|
|
15
|
-
`;
|
|
16
|
-
var expected = `# Hello, world!
|
|
17
|
-
|
|
18
|
-
This is a paragraph.
|
|
19
|
-
|
|
20
|
-
[Example](/example.html)
|
|
21
|
-
|
|
22
|
-

|
|
23
|
-
|
|
24
|
-
* Item 1
|
|
25
|
-
* Item 2
|
|
26
|
-
`;
|
|
27
|
-
var baseUrlReplaced = `# Hello, world!
|
|
28
|
-
|
|
29
|
-
This is a paragraph.
|
|
30
|
-
|
|
31
|
-
[Example](https://example.com/example.html)
|
|
32
|
-
|
|
33
|
-

|
|
34
|
-
|
|
35
|
-
* Item 1
|
|
36
|
-
* Item 2
|
|
37
|
-
`;
|
|
38
|
-
var linkAsText = `# Hello, world!
|
|
39
|
-
|
|
40
|
-
This is a paragraph.
|
|
41
|
-
|
|
42
|
-
Example
|
|
43
|
-
|
|
44
|
-

|
|
45
|
-
|
|
46
|
-
- Item 1
|
|
47
|
-
- Item 2
|
|
48
|
-
`;
|
|
49
|
-
var imageHidden = `# Hello, world!
|
|
50
|
-
|
|
51
|
-
This is a paragraph.
|
|
52
|
-
|
|
53
|
-
[Example](/example.html)
|
|
54
|
-
|
|
55
|
-
- Item 1
|
|
56
|
-
- Item 2
|
|
57
|
-
`;
|
|
58
|
-
var htmlTable = `
|
|
59
|
-
<table>
|
|
60
|
-
<tr>
|
|
61
|
-
<th>Header 1</th>
|
|
62
|
-
<th>Header 2</th>
|
|
63
|
-
</tr>
|
|
64
|
-
<tr>
|
|
65
|
-
<td>Cell 1</td>
|
|
66
|
-
<td>Cell 2</td>
|
|
67
|
-
</tr>
|
|
68
|
-
</table>
|
|
69
|
-
`;
|
|
70
|
-
var expectedTableMarkdown = `
|
|
71
|
-
| Header 1 | Header 2 |
|
|
72
|
-
| -------- | -------- |
|
|
73
|
-
| Cell 1 | Cell 2 |
|
|
74
|
-
`;
|
|
75
|
-
var expectedTableText = `Header 1 Header 2
|
|
76
|
-
Cell 1 Cell 2`;
|
|
77
|
-
describe("htmlToMarkdown", () => {
|
|
78
|
-
it("should convert HTML to Markdown", () => {
|
|
79
|
-
const markdown = htmlToMarkdown(html, { extractors: false });
|
|
80
|
-
const d = distance(markdown, expected);
|
|
81
|
-
expect(d).lte(5);
|
|
82
|
-
});
|
|
83
|
-
it("should convert HTML to Markdown with replaced base URL", () => {
|
|
84
|
-
const markdown = htmlToMarkdown(html, { baseUrl: "https://example.com", extractors: false });
|
|
85
|
-
const d = distance(markdown, baseUrlReplaced);
|
|
86
|
-
expect(d).lte(5);
|
|
87
|
-
});
|
|
88
|
-
it("should convert HTML to Markdown with links as text", () => {
|
|
89
|
-
const markdown = htmlToMarkdown(html, { linkAsText: true, extractors: false });
|
|
90
|
-
const d = distance(markdown, linkAsText);
|
|
91
|
-
expect(d).lte(5);
|
|
92
|
-
});
|
|
93
|
-
it("should convert HTML to Markdown with hidden images", () => {
|
|
94
|
-
const markdown = htmlToMarkdown(html, { hideImage: true, extractors: false });
|
|
95
|
-
const d = distance(markdown, imageHidden);
|
|
96
|
-
expect(d).lte(5);
|
|
97
|
-
});
|
|
98
|
-
it("should convert HTML table to Markdown table", () => {
|
|
99
|
-
const markdown = htmlToMarkdown(htmlTable, { extractors: false });
|
|
100
|
-
const d = distance(markdown, expectedTableMarkdown);
|
|
101
|
-
expect(d).lte(5);
|
|
102
|
-
});
|
|
103
|
-
it("should convert HTML table with table as text option", () => {
|
|
104
|
-
const markdown = htmlToMarkdown(htmlTable, { tableAsText: true, extractors: false });
|
|
105
|
-
const d = distance(markdown, expectedTableText);
|
|
106
|
-
expect(d).lte(10);
|
|
107
|
-
});
|
|
108
|
-
});
|
|
109
|
-
describe("htmlToMarkdown E2E", () => {
|
|
110
|
-
it("Converting for good", async () => {
|
|
111
|
-
const html1 = await loadHtml("https://www.npmjs.com/package/webforai");
|
|
112
|
-
const markdown1 = htmlToMarkdown(html1, { linkAsText: true, hideImage: true });
|
|
113
|
-
const html2 = await loadHtml("https://github.com/inaridiy/webforai");
|
|
114
|
-
const markdown2 = htmlToMarkdown(html2, { linkAsText: true, hideImage: true });
|
|
115
|
-
const d = distance(markdown1, markdown2);
|
|
116
|
-
expect(d).lte(2500);
|
|
117
|
-
});
|
|
118
|
-
});
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
// src/link-replacer.test.ts
|
|
2
|
-
import { describe, expect, it } from "vitest";
|
|
3
|
-
import { linkReplacer } from "./link-replacer.js";
|
|
4
|
-
var markdown = `# Hello, world!
|
|
5
|
-
|
|
6
|
-
This is a paragraph.
|
|
7
|
-
|
|
8
|
-
[Example](/example.html)
|
|
9
|
-
|
|
10
|
-

|
|
11
|
-
|
|
12
|
-
[Absolute Link](https://www.google.com)
|
|
13
|
-
|
|
14
|
-
[Link with hash](/page#hash)
|
|
15
|
-
|
|
16
|
-
[Link with query](/page?query=string)`;
|
|
17
|
-
var expected = `# Hello, world!
|
|
18
|
-
|
|
19
|
-
This is a paragraph.
|
|
20
|
-
|
|
21
|
-
[Example](https://example.com/example.html)
|
|
22
|
-
|
|
23
|
-

|
|
24
|
-
|
|
25
|
-
[Absolute Link](https://www.google.com)
|
|
26
|
-
|
|
27
|
-
[Link with hash](https://example.com/page#hash)
|
|
28
|
-
|
|
29
|
-
[Link with query](https://example.com/page?query=string)`;
|
|
30
|
-
describe("linkReplacer", () => {
|
|
31
|
-
it("should replace relative links", () => {
|
|
32
|
-
const replaced = linkReplacer(markdown, "https://example.com");
|
|
33
|
-
expect(replaced).toEqual(expected);
|
|
34
|
-
});
|
|
35
|
-
it("should not replace absolute links", () => {
|
|
36
|
-
const replaced = linkReplacer("[Absolute Link](https://www.google.com)", "https://example.com");
|
|
37
|
-
expect(replaced).toEqual("[Absolute Link](https://www.google.com)");
|
|
38
|
-
});
|
|
39
|
-
it("should handle links with hashes", () => {
|
|
40
|
-
const replaced = linkReplacer("[Link with hash](/page#hash)", "https://example.com");
|
|
41
|
-
expect(replaced).toEqual("[Link with hash](https://example.com/page#hash)");
|
|
42
|
-
});
|
|
43
|
-
it("should handle links with query parameters", () => {
|
|
44
|
-
const replaced = linkReplacer("[Link with query](/page?query=string)", "https://example.com");
|
|
45
|
-
expect(replaced).toEqual("[Link with query](https://example.com/page?query=string)");
|
|
46
|
-
});
|
|
47
|
-
});
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
// src/loaders/fetch.test.ts
|
|
2
|
-
import { describe, expect, it } from "vitest";
|
|
3
|
-
import { loadHtml } from "./fetch.js";
|
|
4
|
-
describe("Fetch loader", () => {
|
|
5
|
-
it("should load the HTML of a URL", async () => {
|
|
6
|
-
const html = await loadHtml("https://example.com");
|
|
7
|
-
expect(html).toContain("Example Domain");
|
|
8
|
-
});
|
|
9
|
-
});
|