webforai 3.0.0 → 4.0.0
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 +25 -5
- package/dist/bin.js +1277 -479
- package/dist/cjs/detect-client-shell.js +78 -0
- package/dist/cjs/extract-meta-refresh.js +71 -0
- package/dist/cjs/extractors/lib/sanitize.js +4 -0
- package/dist/cjs/html-to-mdast.js +2 -1
- package/dist/cjs/index.js +12 -0
- package/dist/cjs/link-replacer.js +19 -13
- package/dist/cjs/loaders/cf-puppeteer.js +14 -7
- package/dist/cjs/loaders/fetch.js +14 -2
- package/dist/cjs/loaders/playwright.js +32 -30
- package/dist/cjs/loaders/puppeteer.js +14 -7
- package/dist/cjs/md-splitter.js +24 -11
- package/dist/cjs/mdast-handlers/custom-code-handler.js +18 -7
- package/dist/cjs/mdast-handlers/custom-div-handler.js +12 -33
- package/dist/cjs/mdast-handlers/definition-list-handler.js +17 -6
- package/dist/cjs/mdast-to-markdown.js +2 -5
- package/dist/cjs/normalize/index.js +11 -2
- package/dist/cjs/platform/client.js +73 -16
- package/dist/cjs/platform/index.js +4 -0
- package/dist/cjs/platform/types.js +7 -1
- package/dist/detect-client-shell.js +53 -0
- package/dist/extract-meta-refresh.js +47 -0
- package/dist/extractors/lib/sanitize.js +4 -0
- package/dist/html-to-mdast.js +2 -1
- package/dist/index.js +14 -0
- package/dist/link-replacer.js +19 -13
- package/dist/loaders/cf-puppeteer.js +15 -8
- package/dist/loaders/fetch.js +14 -2
- package/dist/loaders/playwright.js +32 -30
- package/dist/loaders/puppeteer.js +15 -8
- package/dist/md-splitter.js +24 -11
- package/dist/mdast-handlers/custom-code-handler.js +17 -7
- package/dist/mdast-handlers/custom-div-handler.js +12 -33
- package/dist/mdast-handlers/definition-list-handler.js +17 -6
- package/dist/mdast-to-markdown.js +2 -5
- package/dist/normalize/index.js +12 -2
- package/dist/platform/client.js +73 -16
- package/dist/platform/index.js +4 -0
- package/dist/platform/types.js +5 -1
- package/dist/types/cli/bin.js.map +1 -1
- package/dist/types/cli/commands/jobs/files.d.ts +28 -0
- package/dist/types/cli/commands/jobs/files.js.map +1 -0
- package/dist/types/cli/commands/jobs/llms-txt.d.ts +33 -0
- package/dist/types/cli/commands/jobs/llms-txt.js.map +1 -0
- package/dist/types/cli/commands/jobs/options.d.ts +58 -0
- package/dist/types/cli/commands/jobs/options.js.map +1 -0
- package/dist/types/cli/commands/jobs/run.d.ts +44 -0
- package/dist/types/cli/commands/jobs/run.js.map +1 -0
- package/dist/types/cli/commands/skill/content.js.map +1 -1
- package/dist/types/cli/commands/webforai/interactive.d.ts +1 -1
- package/dist/types/cli/commands/webforai/interactive.js.map +1 -1
- package/dist/types/cli/commands/webforai/options.d.ts +9 -2
- package/dist/types/cli/commands/webforai/options.js.map +1 -1
- package/dist/types/cli/commands/webforai/run.js.map +1 -1
- package/dist/types/cli/utils.d.ts +8 -1
- package/dist/types/cli/utils.js.map +1 -1
- package/dist/types/detect-client-shell.d.ts +24 -0
- package/dist/types/detect-client-shell.js.map +1 -0
- package/dist/types/extract-meta-refresh.d.ts +29 -0
- package/dist/types/extract-meta-refresh.js.map +1 -0
- package/dist/types/extractors/lib/sanitize.js.map +1 -1
- package/dist/types/html-to-mdast.js.map +1 -1
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.js.map +1 -1
- package/dist/types/link-replacer.d.ts +3 -1
- package/dist/types/link-replacer.js.map +1 -1
- package/dist/types/loaders/cf-puppeteer.js.map +1 -1
- package/dist/types/loaders/fetch.d.ts +4 -0
- package/dist/types/loaders/fetch.js.map +1 -1
- package/dist/types/loaders/playwright.js.map +1 -1
- package/dist/types/loaders/puppeteer.js.map +1 -1
- package/dist/types/md-splitter.d.ts +1 -1
- package/dist/types/md-splitter.js.map +1 -1
- package/dist/types/mdast-handlers/custom-code-handler.d.ts +2 -0
- package/dist/types/mdast-handlers/custom-code-handler.js.map +1 -1
- package/dist/types/mdast-handlers/custom-div-handler.js.map +1 -1
- package/dist/types/mdast-handlers/definition-list-handler.d.ts +0 -11
- package/dist/types/mdast-handlers/definition-list-handler.js.map +1 -1
- package/dist/types/mdast-to-markdown.js.map +1 -1
- package/dist/types/normalize/index.js.map +1 -1
- package/dist/types/platform/client.d.ts +2 -1
- package/dist/types/platform/client.js.map +1 -1
- package/dist/types/platform/error.d.ts +5 -3
- package/dist/types/platform/error.js.map +1 -1
- package/dist/types/platform/fetch.d.ts +2 -0
- package/dist/types/platform/fetch.js.map +1 -1
- package/dist/types/platform/index.d.ts +1 -1
- package/dist/types/platform/index.js.map +1 -1
- package/dist/types/platform/types.d.ts +35 -6
- package/dist/types/platform/types.js.map +1 -1
- package/package.json +16 -5
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var detect_client_shell_exports = {};
|
|
20
|
+
__export(detect_client_shell_exports, {
|
|
21
|
+
SHELL_EMPTY_THRESHOLD: () => SHELL_EMPTY_THRESHOLD,
|
|
22
|
+
SHELL_SUSPICION_THRESHOLD: () => SHELL_SUSPICION_THRESHOLD,
|
|
23
|
+
detectClientShell: () => detectClientShell
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(detect_client_shell_exports);
|
|
26
|
+
const SHELL_SUSPICION_THRESHOLD = 300;
|
|
27
|
+
const SHELL_EMPTY_THRESHOLD = 50;
|
|
28
|
+
const BODY_PATTERN = /<body[^>]*>([\s\S]*?)<\/body>/i;
|
|
29
|
+
const HEAD_PATTERN = /<head[^>]*>[\s\S]*?<\/head>/gi;
|
|
30
|
+
const NOSCRIPT_PATTERN = /<noscript[^>]*>([\s\S]*?)<\/noscript>/gi;
|
|
31
|
+
const INVISIBLE_ELEMENT_PATTERN = /<(script|style|template|svg|noscript)[^>]*>[\s\S]*?<\/\1>/gi;
|
|
32
|
+
const COMMENT_PATTERN = /<!--[\s\S]*?-->/g;
|
|
33
|
+
const TAG_PATTERN = /<[^>]+>/g;
|
|
34
|
+
const MOUNT_POINT_PATTERN = /<[a-z][^>]*\sid=["'](?:root|app|__next|___gatsby|__nuxt|q-app|svelte|react-root)["']|<app-root[\s>]|\sdata-reactroot[\s>=]/i;
|
|
35
|
+
const CHALLENGE_PATTERN = /cf-chl|challenge-platform|cf_chl_opt|just a moment|attention required|checking your browser|_incapsula_|px-captcha|datadome/i;
|
|
36
|
+
const ENTITIES = {
|
|
37
|
+
" ": " ",
|
|
38
|
+
"&": "&",
|
|
39
|
+
"<": "<",
|
|
40
|
+
">": ">",
|
|
41
|
+
""": '"',
|
|
42
|
+
"'": "'"
|
|
43
|
+
};
|
|
44
|
+
const visibleTextOf = (markup) => markup.replace(COMMENT_PATTERN, " ").replace(INVISIBLE_ELEMENT_PATTERN, " ").replace(TAG_PATTERN, " ").replace(/&(?:nbsp|amp|lt|gt|quot|#39);/g, (entity) => ENTITIES[entity] ?? " ").replace(/\s+/g, " ").trim();
|
|
45
|
+
const noscriptMentionsJavascript = (html) => {
|
|
46
|
+
for (const match of html.matchAll(NOSCRIPT_PATTERN)) {
|
|
47
|
+
if (/javascript/i.test(match[1] ?? "")) {
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return false;
|
|
52
|
+
};
|
|
53
|
+
const detectClientShell = (html) => {
|
|
54
|
+
const body = BODY_PATTERN.exec(html)?.[1] ?? html.replace(HEAD_PATTERN, " ");
|
|
55
|
+
const visibleTextLength = visibleTextOf(body).length;
|
|
56
|
+
if (visibleTextLength >= SHELL_SUSPICION_THRESHOLD) {
|
|
57
|
+
return { isShell: false, visibleTextLength };
|
|
58
|
+
}
|
|
59
|
+
if (CHALLENGE_PATTERN.test(html)) {
|
|
60
|
+
return { isShell: true, reason: "anti-bot-challenge", visibleTextLength };
|
|
61
|
+
}
|
|
62
|
+
if (MOUNT_POINT_PATTERN.test(body)) {
|
|
63
|
+
return { isShell: true, reason: "spa-shell", visibleTextLength };
|
|
64
|
+
}
|
|
65
|
+
if (noscriptMentionsJavascript(html)) {
|
|
66
|
+
return { isShell: true, reason: "noscript-only", visibleTextLength };
|
|
67
|
+
}
|
|
68
|
+
if (visibleTextLength < SHELL_EMPTY_THRESHOLD) {
|
|
69
|
+
return { isShell: true, reason: "empty-body", visibleTextLength };
|
|
70
|
+
}
|
|
71
|
+
return { isShell: false, visibleTextLength };
|
|
72
|
+
};
|
|
73
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
74
|
+
0 && (module.exports = {
|
|
75
|
+
SHELL_EMPTY_THRESHOLD,
|
|
76
|
+
SHELL_SUSPICION_THRESHOLD,
|
|
77
|
+
detectClientShell
|
|
78
|
+
});
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var extract_meta_refresh_exports = {};
|
|
20
|
+
__export(extract_meta_refresh_exports, {
|
|
21
|
+
MAX_META_REFRESH_DELAY_SECONDS: () => MAX_META_REFRESH_DELAY_SECONDS,
|
|
22
|
+
extractMetaRefresh: () => extractMetaRefresh
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(extract_meta_refresh_exports);
|
|
25
|
+
const MAX_META_REFRESH_DELAY_SECONDS = 10;
|
|
26
|
+
const META_TAG_PATTERN = /<meta\b[^>]*>/gi;
|
|
27
|
+
const HTTP_EQUIV_REFRESH_PATTERN = /\bhttp-equiv\s*=\s*("\s*refresh\s*"|'\s*refresh\s*'|refresh\b)/i;
|
|
28
|
+
const CONTENT_ATTR_PATTERN = /\bcontent\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/i;
|
|
29
|
+
const CONTENT_VALUE_PATTERN = /^\s*(\d+(?:\.\d+)?)\s*[;,]\s*url\s*=\s*(?:"([^"]*)"|'([^']*)'|(.+?))\s*$/i;
|
|
30
|
+
const resolveTarget = (raw, baseUrl) => {
|
|
31
|
+
try {
|
|
32
|
+
return baseUrl === void 0 ? new URL(raw) : new URL(raw, baseUrl);
|
|
33
|
+
} catch {
|
|
34
|
+
return void 0;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
const extractMetaRefresh = (html, baseUrl) => {
|
|
38
|
+
for (const [tag] of html.matchAll(META_TAG_PATTERN)) {
|
|
39
|
+
if (!HTTP_EQUIV_REFRESH_PATTERN.test(tag)) {
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
const contentMatch = CONTENT_ATTR_PATTERN.exec(tag);
|
|
43
|
+
const content = contentMatch?.[1] ?? contentMatch?.[2] ?? contentMatch?.[3];
|
|
44
|
+
if (!content) {
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
const value = CONTENT_VALUE_PATTERN.exec(content);
|
|
48
|
+
if (!value) {
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
const delaySeconds = Number.parseFloat(value[1] ?? "");
|
|
52
|
+
const raw = (value[2] ?? value[3] ?? value[4] ?? "").trim();
|
|
53
|
+
if (!raw || delaySeconds > MAX_META_REFRESH_DELAY_SECONDS) {
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
const target = resolveTarget(raw, baseUrl);
|
|
57
|
+
if (!target || target.protocol !== "http:" && target.protocol !== "https:") {
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
if (baseUrl !== void 0 && resolveTarget(baseUrl, void 0)?.href === target.href) {
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
return { url: target.href, delaySeconds };
|
|
64
|
+
}
|
|
65
|
+
return void 0;
|
|
66
|
+
};
|
|
67
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
68
|
+
0 && (module.exports = {
|
|
69
|
+
MAX_META_REFRESH_DELAY_SECONDS,
|
|
70
|
+
extractMetaRefresh
|
|
71
|
+
});
|
|
@@ -383,7 +383,11 @@ const countAnchors = (element) => {
|
|
|
383
383
|
}
|
|
384
384
|
return count;
|
|
385
385
|
};
|
|
386
|
+
const LITERAL_TEXT_TAGS = /* @__PURE__ */ new Set(["code", "kbd", "samp", "var", "pre"]);
|
|
386
387
|
const isMidArticleWidget = (element) => {
|
|
388
|
+
if (LITERAL_TEXT_TAGS.has(element.tagName)) {
|
|
389
|
+
return false;
|
|
390
|
+
}
|
|
387
391
|
if (element.tagName === "form" || element.tagName === "fieldset") {
|
|
388
392
|
return true;
|
|
389
393
|
}
|
|
@@ -34,13 +34,14 @@ var import_definition_list_handler = require("./mdast-handlers/definition-list-h
|
|
|
34
34
|
var import_math_handler = require("./mdast-handlers/math-handler");
|
|
35
35
|
var import_sup_sub_handler = require("./mdast-handlers/sup-sub-handler");
|
|
36
36
|
var import_normalize = require("./normalize");
|
|
37
|
+
var import_hast_fast = require("./utils/hast-fast");
|
|
37
38
|
var import_hast_utils = require("./utils/hast-utils");
|
|
38
39
|
const htmlToMdast = (htmlOrHast, options) => {
|
|
39
40
|
const { extractors, url: defaultUrl, lang: defaultLang } = options || {};
|
|
40
41
|
const [lang, hast] = typeof htmlOrHast === "string" ? [defaultLang || (0, import_hast_utils.getLangFromStr)(htmlOrHast), (0, import_hast_util_from_html.fromHtml)(htmlOrHast, { fragment: true })] : [defaultLang || (0, import_hast_utils.getLangFromHast)(htmlOrHast), htmlOrHast];
|
|
41
42
|
const isOwned = typeof htmlOrHast === "string" || (options?.owned ?? false);
|
|
42
43
|
const url = defaultUrl || (0, import_hast_utils.getUrlFromHast)(hast);
|
|
43
|
-
const extractedHast = (0, import_extractors.pipeExtractors)({ hast, lang, url, owned:
|
|
44
|
+
const extractedHast = (0, import_extractors.pipeExtractors)({ hast: isOwned ? hast : (0, import_hast_fast.cloneHast)(hast), lang, url, owned: true }, extractors);
|
|
44
45
|
(0, import_normalize.normalizeHast)(extractedHast, options?.normalize);
|
|
45
46
|
const mdast = (0, import_hast_util_to_mdast.toMdast)(extractedHast, {
|
|
46
47
|
handlers: {
|
package/dist/cjs/index.js
CHANGED
|
@@ -19,11 +19,16 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
19
19
|
var src_exports = {};
|
|
20
20
|
__export(src_exports, {
|
|
21
21
|
BUILTIN_ADAPTERS: () => import_adapters.BUILTIN_ADAPTERS,
|
|
22
|
+
MAX_META_REFRESH_DELAY_SECONDS: () => import_extract_meta_refresh.MAX_META_REFRESH_DELAY_SECONDS,
|
|
23
|
+
SHELL_EMPTY_THRESHOLD: () => import_detect_client_shell.SHELL_EMPTY_THRESHOLD,
|
|
24
|
+
SHELL_SUSPICION_THRESHOLD: () => import_detect_client_shell.SHELL_SUSPICION_THRESHOLD,
|
|
22
25
|
adapterById: () => import_adapters.adapterById,
|
|
23
26
|
autoExtractor: () => import_extractors.autoExtractor,
|
|
24
27
|
buildPageSignature: () => import_adapters.buildPageSignature,
|
|
25
28
|
createAutoExtractor: () => import_extractors.createAutoExtractor,
|
|
26
29
|
defineSelectorAdapter: () => import_adapters.defineSelectorAdapter,
|
|
30
|
+
detectClientShell: () => import_detect_client_shell.detectClientShell,
|
|
31
|
+
extractMetaRefresh: () => import_extract_meta_refresh.extractMetaRefresh,
|
|
27
32
|
extractMetadata: () => import_metadata.extractMetadata,
|
|
28
33
|
htmlToMarkdown: () => import_html_to_markdown.htmlToMarkdown,
|
|
29
34
|
htmlToMarkdownWithMetadata: () => import_html_to_markdown.htmlToMarkdownWithMetadata,
|
|
@@ -46,16 +51,23 @@ var import_html_to_mdast = require("./html-to-mdast");
|
|
|
46
51
|
var import_mdast_to_markdown = require("./mdast-to-markdown");
|
|
47
52
|
var import_extractors = require("./extractors");
|
|
48
53
|
var import_adapters = require("./adapters");
|
|
54
|
+
var import_detect_client_shell = require("./detect-client-shell");
|
|
55
|
+
var import_extract_meta_refresh = require("./extract-meta-refresh");
|
|
49
56
|
var import_metadata = require("./metadata");
|
|
50
57
|
var import_normalize = require("./normalize");
|
|
51
58
|
// Annotate the CommonJS export names for ESM import in node:
|
|
52
59
|
0 && (module.exports = {
|
|
53
60
|
BUILTIN_ADAPTERS,
|
|
61
|
+
MAX_META_REFRESH_DELAY_SECONDS,
|
|
62
|
+
SHELL_EMPTY_THRESHOLD,
|
|
63
|
+
SHELL_SUSPICION_THRESHOLD,
|
|
54
64
|
adapterById,
|
|
55
65
|
autoExtractor,
|
|
56
66
|
buildPageSignature,
|
|
57
67
|
createAutoExtractor,
|
|
58
68
|
defineSelectorAdapter,
|
|
69
|
+
detectClientShell,
|
|
70
|
+
extractMetaRefresh,
|
|
59
71
|
extractMetadata,
|
|
60
72
|
htmlToMarkdown,
|
|
61
73
|
htmlToMarkdownWithMetadata,
|
|
@@ -21,19 +21,25 @@ __export(link_replacer_exports, {
|
|
|
21
21
|
linkReplacer: () => linkReplacer
|
|
22
22
|
});
|
|
23
23
|
module.exports = __toCommonJS(link_replacer_exports);
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
24
|
+
const absoluteUrl = (url, base) => {
|
|
25
|
+
if (/^(?:[a-z][a-z\d+.-]*:|#)/i.test(url)) {
|
|
26
|
+
return url;
|
|
27
|
+
}
|
|
28
|
+
try {
|
|
29
|
+
return new URL(url, base).href;
|
|
30
|
+
} catch {
|
|
31
|
+
return url;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
const linkReplacer = (node, base) => {
|
|
35
|
+
let result = node;
|
|
36
|
+
if (node.type === "link" || node.type === "image" || node.type === "definition") {
|
|
37
|
+
result = { ...node, url: absoluteUrl(node.url, base) };
|
|
38
|
+
}
|
|
39
|
+
if ("children" in result) {
|
|
40
|
+
return { ...result, children: result.children.map((child) => linkReplacer(child, base)) };
|
|
41
|
+
}
|
|
42
|
+
return result;
|
|
37
43
|
};
|
|
38
44
|
// Annotate the CommonJS export names for ESM import in node:
|
|
39
45
|
0 && (module.exports = {
|
|
@@ -32,15 +32,22 @@ __export(cf_puppeteer_exports, {
|
|
|
32
32
|
});
|
|
33
33
|
module.exports = __toCommonJS(cf_puppeteer_exports);
|
|
34
34
|
var import_puppeteer = __toESM(require("@cloudflare/puppeteer"), 1);
|
|
35
|
-
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
36
35
|
const loadHtml = async (url, ctx) => {
|
|
37
36
|
const browser = await import_puppeteer.default.launch(ctx);
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
37
|
+
try {
|
|
38
|
+
const page = await browser.newPage();
|
|
39
|
+
await page.goto(url);
|
|
40
|
+
try {
|
|
41
|
+
await page.waitForNetworkIdle({ timeout: 1e4 });
|
|
42
|
+
} catch (error) {
|
|
43
|
+
if (!(error instanceof import_puppeteer.TimeoutError)) {
|
|
44
|
+
throw error;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return await page.content();
|
|
48
|
+
} finally {
|
|
49
|
+
await browser.close();
|
|
50
|
+
}
|
|
44
51
|
};
|
|
45
52
|
// Annotate the CommonJS export names for ESM import in node:
|
|
46
53
|
0 && (module.exports = {
|
|
@@ -22,10 +22,22 @@ __export(fetch_exports, {
|
|
|
22
22
|
loadHtml: () => loadHtml
|
|
23
23
|
});
|
|
24
24
|
module.exports = __toCommonJS(fetch_exports);
|
|
25
|
+
var import_extract_meta_refresh = require("../extract-meta-refresh");
|
|
25
26
|
const USER_AGENT = "mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/125.0.0.0 safari/537.36";
|
|
27
|
+
const MAX_META_REFRESH_HOPS = 3;
|
|
26
28
|
const loadHtml = async (url, userAgent = USER_AGENT) => {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
+
let current = url;
|
|
30
|
+
let html = "";
|
|
31
|
+
for (let hop = 0; hop <= MAX_META_REFRESH_HOPS; hop++) {
|
|
32
|
+
const response = await fetch(current, { headers: { "User-Agent": userAgent } });
|
|
33
|
+
html = await response.text();
|
|
34
|
+
const refresh = (0, import_extract_meta_refresh.extractMetaRefresh)(html, response.url || current);
|
|
35
|
+
if (!refresh) {
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
current = refresh.url;
|
|
39
|
+
}
|
|
40
|
+
return html;
|
|
29
41
|
};
|
|
30
42
|
// Annotate the CommonJS export names for ESM import in node:
|
|
31
43
|
0 && (module.exports = {
|
|
@@ -35,40 +35,42 @@ const SUPER_BYPASS_DEVICE = {
|
|
|
35
35
|
const loadHtml = async (url, options) => {
|
|
36
36
|
const { browser, waitUntil, timeout, superBypassMode } = options ?? {};
|
|
37
37
|
const _browser = browser ?? await import_playwright_core.chromium.launch({ headless: true });
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
await
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
await
|
|
49
|
-
|
|
50
|
-
|
|
38
|
+
let context;
|
|
39
|
+
try {
|
|
40
|
+
context = await _browser.newContext(superBypassMode ? SUPER_BYPASS_DEVICE : import_playwright_core.devices["Desktop Chrome"]);
|
|
41
|
+
if (superBypassMode) {
|
|
42
|
+
await context.addInitScript(() => {
|
|
43
|
+
Object.defineProperty(navigator, "webdriver", { get: () => void 0 });
|
|
44
|
+
Object.defineProperty(navigator, "languages", { get: () => ["en-US", "en"] });
|
|
45
|
+
Object.defineProperty(navigator, "plugins", { get: () => [1, 2, 3, 4, 5] });
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
const page = await context.newPage();
|
|
49
|
+
if (superBypassMode) {
|
|
50
|
+
await page.route("**/*.js", (route) => {
|
|
51
|
+
if (route.request().url().includes("captcha-delivery")) {
|
|
52
|
+
return route.abort();
|
|
53
|
+
}
|
|
54
|
+
return route.continue();
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
await page.goto(url, { waitUntil: waitUntil ?? "load", timeout });
|
|
58
|
+
await page.evaluate(() => {
|
|
59
|
+
const elements = document.querySelectorAll("*");
|
|
60
|
+
for (const element of elements) {
|
|
61
|
+
const rect = element.getBoundingClientRect();
|
|
62
|
+
element.setAttribute("data-rwidth", rect.width.toString());
|
|
63
|
+
element.setAttribute("data-rheight", rect.height.toString());
|
|
51
64
|
}
|
|
52
|
-
return route.continue();
|
|
53
65
|
});
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
element.setAttribute("data-rwidth", rect.width.toString());
|
|
61
|
-
element.setAttribute("data-rheight", rect.height.toString());
|
|
66
|
+
return await page.content();
|
|
67
|
+
} finally {
|
|
68
|
+
if (browser) {
|
|
69
|
+
await context?.close();
|
|
70
|
+
} else {
|
|
71
|
+
await _browser.close();
|
|
62
72
|
}
|
|
63
|
-
});
|
|
64
|
-
const html = await page.content();
|
|
65
|
-
await page.close();
|
|
66
|
-
if (browser) {
|
|
67
|
-
await context.close();
|
|
68
|
-
} else {
|
|
69
|
-
await _browser.close();
|
|
70
73
|
}
|
|
71
|
-
return html;
|
|
72
74
|
};
|
|
73
75
|
// Annotate the CommonJS export names for ESM import in node:
|
|
74
76
|
0 && (module.exports = {
|
|
@@ -32,7 +32,6 @@ __export(puppeteer_exports, {
|
|
|
32
32
|
});
|
|
33
33
|
module.exports = __toCommonJS(puppeteer_exports);
|
|
34
34
|
var import_puppeteer = __toESM(require("puppeteer"), 1);
|
|
35
|
-
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
36
35
|
const loadHtml = async (url, ctx) => {
|
|
37
36
|
const browser = await import_puppeteer.default.launch(
|
|
38
37
|
ctx || {
|
|
@@ -40,12 +39,20 @@ const loadHtml = async (url, ctx) => {
|
|
|
40
39
|
args: ["--no-sandbox", "--disable-setuid-sandbox"]
|
|
41
40
|
}
|
|
42
41
|
);
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
42
|
+
try {
|
|
43
|
+
const page = await browser.newPage();
|
|
44
|
+
await page.goto(url);
|
|
45
|
+
try {
|
|
46
|
+
await page.waitForNetworkIdle({ timeout: 1e4 });
|
|
47
|
+
} catch (error) {
|
|
48
|
+
if (!(error instanceof import_puppeteer.TimeoutError)) {
|
|
49
|
+
throw error;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return await page.content();
|
|
53
|
+
} finally {
|
|
54
|
+
await browser.close();
|
|
55
|
+
}
|
|
49
56
|
};
|
|
50
57
|
// Annotate the CommonJS export names for ESM import in node:
|
|
51
58
|
0 && (module.exports = {
|
package/dist/cjs/md-splitter.js
CHANGED
|
@@ -25,15 +25,13 @@ var import_mdast_to_markdown = require("./mdast-to-markdown");
|
|
|
25
25
|
var import_common = require("./utils/common");
|
|
26
26
|
var import_mdast_utils = require("./utils/mdast-utils");
|
|
27
27
|
const PRIORITY_SPLITTERS = ["h1", "h2", "h3", "h4", "h5", "h6", "list", "table", "code"];
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
const _mdastSplitter = async (contents, checker, splitterGenerator) => {
|
|
34
|
-
const splitter = splitterGenerator.next().value;
|
|
28
|
+
const _mdastSplitter = async (contents, checker, priority, signal) => {
|
|
29
|
+
signal?.throwIfAborted();
|
|
30
|
+
const splitter = PRIORITY_SPLITTERS[priority];
|
|
35
31
|
const markdown = (0, import_mdast_to_markdown.mdastToMarkdown)((0, import_mdast_utils.warpRoot)(contents));
|
|
36
|
-
|
|
32
|
+
const accepted = await checker(markdown);
|
|
33
|
+
signal?.throwIfAborted();
|
|
34
|
+
if (accepted || contents.length === 1) {
|
|
37
35
|
return [contents];
|
|
38
36
|
}
|
|
39
37
|
const chunked = splitter ? contents.reduce((acc, content) => {
|
|
@@ -44,11 +42,26 @@ const _mdastSplitter = async (contents, checker, splitterGenerator) => {
|
|
|
44
42
|
acc[acc.length - 1].push(content);
|
|
45
43
|
return acc;
|
|
46
44
|
}, []) : (0, import_common.chunk)(contents, Math.ceil(contents.length / 2));
|
|
47
|
-
const splitting = chunked.map((chunk2) => _mdastSplitter(chunk2, checker,
|
|
45
|
+
const splitting = chunked.map((chunk2) => _mdastSplitter(chunk2, checker, priority + 1, signal));
|
|
48
46
|
return Promise.all(splitting).then((chunks) => chunks.flat());
|
|
49
47
|
};
|
|
50
|
-
const mdastSplitter = (mdast, checker,
|
|
51
|
-
|
|
48
|
+
const mdastSplitter = async (mdast, checker, options) => {
|
|
49
|
+
const signal = options?.signal;
|
|
50
|
+
if (!signal) {
|
|
51
|
+
return _mdastSplitter((0, import_mdast_utils.unwarpRoot)(mdast), checker, 0);
|
|
52
|
+
}
|
|
53
|
+
signal.throwIfAborted();
|
|
54
|
+
let onAbort = () => {
|
|
55
|
+
};
|
|
56
|
+
const aborted = new Promise((_, reject) => {
|
|
57
|
+
onAbort = () => reject(signal.reason);
|
|
58
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
59
|
+
});
|
|
60
|
+
try {
|
|
61
|
+
return await Promise.race([_mdastSplitter((0, import_mdast_utils.unwarpRoot)(mdast), checker, 0, signal), aborted]);
|
|
62
|
+
} finally {
|
|
63
|
+
signal.removeEventListener("abort", onAbort);
|
|
64
|
+
}
|
|
52
65
|
};
|
|
53
66
|
// Annotate the CommonJS export names for ESM import in node:
|
|
54
67
|
0 && (module.exports = {
|
|
@@ -18,20 +18,30 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
var custom_code_handler_exports = {};
|
|
20
20
|
__export(custom_code_handler_exports, {
|
|
21
|
+
codeLanguage: () => codeLanguage,
|
|
21
22
|
customCodeHandler: () => customCodeHandler
|
|
22
23
|
});
|
|
23
24
|
module.exports = __toCommonJS(custom_code_handler_exports);
|
|
24
25
|
var import_hast_util_to_text = require("hast-util-to-text");
|
|
25
26
|
var import_trim_trailing_lines = require("trim-trailing-lines");
|
|
26
27
|
var import_detect_code_lang = require("../utils/detect-code-lang");
|
|
27
|
-
|
|
28
|
+
var import_hast_fast = require("../utils/hast-fast");
|
|
29
|
+
const LANGUAGE_MATCH_REGEX = [/^language-(\S+)$/, /^highlight-source-(\S+)$/, /^CodeBlock--language-(\S+)$/];
|
|
30
|
+
const LANGUAGE_ATTRIBUTES = ["dataLanguage", "dataLang"];
|
|
31
|
+
const attributeLanguage = (element) => {
|
|
32
|
+
for (const name of LANGUAGE_ATTRIBUTES) {
|
|
33
|
+
const value = element.properties?.[name];
|
|
34
|
+
if (typeof value === "string" && /^[\w+#.-]+$/.test(value.trim()) && value.trim() !== "plaintext") {
|
|
35
|
+
return value.trim();
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return void 0;
|
|
39
|
+
};
|
|
40
|
+
const codeLanguage = (...elements) => elements.flatMap(import_hast_fast.classList).map((className) => LANGUAGE_MATCH_REGEX.map((regex) => className.match(regex)).find(Boolean)?.[1]).find(Boolean) ?? elements.map(attributeLanguage).find(Boolean);
|
|
28
41
|
const customCodeHandler = (state, node) => {
|
|
29
|
-
const
|
|
30
|
-
const codeValue = (0, import_trim_trailing_lines.trimTrailingLines)((0, import_hast_util_to_text.toText)(node))
|
|
31
|
-
const classLang =
|
|
32
|
-
const match = LANGUAGE_MATCH_REGEX.map((regex) => className.match(regex)).find((match2) => match2);
|
|
33
|
-
return match?.[1];
|
|
34
|
-
}).find((className) => className);
|
|
42
|
+
const code = node.children.find((child) => (0, import_hast_fast.isElement)(child) && child.tagName === "code");
|
|
43
|
+
const codeValue = (0, import_trim_trailing_lines.trimTrailingLines)((0, import_hast_util_to_text.toText)(node));
|
|
44
|
+
const classLang = codeLanguage(...(0, import_hast_fast.isElement)(code) ? [code, node] : [node]);
|
|
35
45
|
const lang = classLang || (0, import_detect_code_lang.detectLanguage)(codeValue) || null;
|
|
36
46
|
const result = { type: "code", lang, meta: null, value: codeValue };
|
|
37
47
|
state.patch(node, result);
|
|
@@ -39,5 +49,6 @@ const customCodeHandler = (state, node) => {
|
|
|
39
49
|
};
|
|
40
50
|
// Annotate the CommonJS export names for ESM import in node:
|
|
41
51
|
0 && (module.exports = {
|
|
52
|
+
codeLanguage,
|
|
42
53
|
customCodeHandler
|
|
43
54
|
});
|
|
@@ -27,44 +27,23 @@ var import_hast_util_to_string = require("hast-util-to-string");
|
|
|
27
27
|
var import_hast_util_to_text = require("hast-util-to-text");
|
|
28
28
|
var import_trim_trailing_lines = require("trim-trailing-lines");
|
|
29
29
|
var import_detect_code_lang = require("../utils/detect-code-lang");
|
|
30
|
+
var import_hast_fast = require("../utils/hast-fast");
|
|
31
|
+
var import_custom_code_handler = require("./custom-code-handler");
|
|
30
32
|
const CODE_BLOCK_REGEX = /highlight-source|language-|codegroup|codeblock|code-block/i;
|
|
31
33
|
const CODE_FILENAME_SELECTORS = "[class*='fileName'],[class*='fileName'],[class*='title'],[class*='Title']";
|
|
32
|
-
const LANGUAGE_MATCH_REGEX = [/language-(\w+)/, /highlight-source-(\w+)/, /CodeBlock--language-(\w+)/];
|
|
33
|
-
const findRecursive = (array, condition, maxDepth = 3) => {
|
|
34
|
-
if (maxDepth <= 0) {
|
|
35
|
-
return null;
|
|
36
|
-
}
|
|
37
|
-
for (const value of array) {
|
|
38
|
-
const result = condition(value);
|
|
39
|
-
if (Array.isArray(result)) {
|
|
40
|
-
return findRecursive(result, condition, maxDepth - 1);
|
|
41
|
-
}
|
|
42
|
-
if (result) {
|
|
43
|
-
return value;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
return null;
|
|
47
|
-
};
|
|
48
34
|
const customDivHandler = (state, node) => {
|
|
49
|
-
const classNames =
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
return child.children.filter((child2) => child2.type === "element");
|
|
58
|
-
});
|
|
59
|
-
if (codeBlock && classNames.some((className) => CODE_BLOCK_REGEX.test(className))) {
|
|
60
|
-
const codeBlockClassNames = codeBlock.type === "element" ? codeBlock.properties.className ?? [] : [];
|
|
61
|
-
const codeValue = (0, import_trim_trailing_lines.trimTrailingLines)((0, import_hast_util_to_text.toText)(codeBlock)).trim();
|
|
35
|
+
const classNames = (0, import_hast_fast.classList)(node);
|
|
36
|
+
if (!classNames.some((className) => CODE_BLOCK_REGEX.test(className))) {
|
|
37
|
+
return import_hast_util_to_mdast.defaultHandlers.div(state, node);
|
|
38
|
+
}
|
|
39
|
+
const codeBlocks = (0, import_hast_fast.collectElements)(node, (element) => element.tagName === "pre");
|
|
40
|
+
const codeBlock = codeBlocks.length === 1 ? codeBlocks[0] : void 0;
|
|
41
|
+
if (codeBlock) {
|
|
42
|
+
const codeValue = (0, import_trim_trailing_lines.trimTrailingLines)((0, import_hast_util_to_text.toText)(codeBlock));
|
|
62
43
|
const filenameElement = (0, import_hast_util_select.select)(CODE_FILENAME_SELECTORS, node);
|
|
63
44
|
const fileLang = filenameElement ? (0, import_hast_util_to_string.toString)(filenameElement).match(/\.(\w+)$/)?.[1] : null;
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
return match?.[1];
|
|
67
|
-
}).find((className) => className);
|
|
45
|
+
const code = codeBlock.children.find((child) => (0, import_hast_fast.isElement)(child) && child.tagName === "code");
|
|
46
|
+
const classLang = (0, import_custom_code_handler.codeLanguage)(...(0, import_hast_fast.isElement)(code) ? [code, codeBlock, node] : [codeBlock, node]);
|
|
68
47
|
const lang = fileLang || classLang || (0, import_detect_code_lang.detectLanguage)(codeValue) || null;
|
|
69
48
|
const result = { type: "code", lang, meta: null, value: codeValue };
|
|
70
49
|
state.patch(node, result);
|
|
@@ -50,6 +50,20 @@ const hoistBoundaryWhitespace = (nodes) => {
|
|
|
50
50
|
}
|
|
51
51
|
return result;
|
|
52
52
|
};
|
|
53
|
+
const termsAndDefinitions = (parent) => {
|
|
54
|
+
const found = [];
|
|
55
|
+
for (const child of parent.children) {
|
|
56
|
+
if (child.type !== "element") {
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
if (child.tagName === "dt" || child.tagName === "dd") {
|
|
60
|
+
found.push(child);
|
|
61
|
+
} else if (child.tagName === "div") {
|
|
62
|
+
found.push(...termsAndDefinitions(child));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return found;
|
|
66
|
+
};
|
|
53
67
|
const definitionListHandler = (state, node) => {
|
|
54
68
|
const items = [];
|
|
55
69
|
let current;
|
|
@@ -64,11 +78,7 @@ const definitionListHandler = (state, node) => {
|
|
|
64
78
|
items.push({ type: "listItem", spread: false, children: [heading, ...current.definitions] });
|
|
65
79
|
current = void 0;
|
|
66
80
|
};
|
|
67
|
-
for (const
|
|
68
|
-
if (child.type !== "element") {
|
|
69
|
-
continue;
|
|
70
|
-
}
|
|
71
|
-
const element = child;
|
|
81
|
+
for (const element of termsAndDefinitions(node)) {
|
|
72
82
|
if (element.tagName === "dt") {
|
|
73
83
|
if (current && current.definitions.length > 0) {
|
|
74
84
|
flush();
|
|
@@ -87,7 +97,8 @@ const definitionListHandler = (state, node) => {
|
|
|
87
97
|
}
|
|
88
98
|
flush();
|
|
89
99
|
if (items.length === 0) {
|
|
90
|
-
|
|
100
|
+
const fallback = state.toFlow(state.all(node));
|
|
101
|
+
return fallback.length > 0 ? fallback : void 0;
|
|
91
102
|
}
|
|
92
103
|
const result = { type: "list", ordered: false, spread: false, children: items };
|
|
93
104
|
state.patch(node, result);
|
|
@@ -33,11 +33,8 @@ const DEFAULT_MDAST_TO_MARKDOWN_OPTIONS = {
|
|
|
33
33
|
};
|
|
34
34
|
const mdastToMarkdown = (mdast, options) => {
|
|
35
35
|
const { baseUrl, ...toMarkdownOptions } = { ...DEFAULT_MDAST_TO_MARKDOWN_OPTIONS, ...options };
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
markdown = (0, import_link_replacer.linkReplacer)(markdown, baseUrl);
|
|
39
|
-
}
|
|
40
|
-
return markdown;
|
|
36
|
+
const tree = (0, import_mdast_utils.warpRoot)(mdast);
|
|
37
|
+
return (0, import_mdast_util_to_markdown.toMarkdown)(baseUrl ? (0, import_link_replacer.linkReplacer)(tree, baseUrl) : tree, toMarkdownOptions);
|
|
41
38
|
};
|
|
42
39
|
// Annotate the CommonJS export names for ESM import in node:
|
|
43
40
|
0 && (module.exports = {
|