UNPKG

webforai

Version:

A library that provides a web interface for AI

57 lines (56 loc) 2.42 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var md_splitter_exports = {}; __export(md_splitter_exports, { mdastSplitter: () => mdastSplitter }); module.exports = __toCommonJS(md_splitter_exports); var import_mdast_to_markdown = require("./mdast-to-markdown"); var import_common = require("./utils/common"); var import_mdast_utils = require("./utils/mdast-utils"); const PRIORITY_SPLITTERS = ["h1", "h2", "h3", "h4", "h5", "h6", "list", "table", "code"]; const getSplitterGenerator = function* () { for (const splitter of PRIORITY_SPLITTERS) { yield splitter; } }; const _mdastSplitter = async (contents, checker, splitterGenerator) => { const splitter = splitterGenerator.next().value; const markdown = (0, import_mdast_to_markdown.mdastToMarkdown)((0, import_mdast_utils.warpRoot)(contents)); if (await checker(markdown) || contents.length === 1) { return [contents]; } const chunked = splitter ? contents.reduce((acc, content) => { if ((0, import_mdast_utils.internalType)(content) === splitter || acc.length === 0) { acc.push([content]); return acc; } acc[acc.length - 1].push(content); return acc; }, []) : (0, import_common.chunk)(contents, Math.ceil(contents.length / 2)); const splitting = chunked.map((chunk2) => _mdastSplitter(chunk2, checker, splitterGenerator)); return Promise.all(splitting).then((chunks) => chunks.flat()); }; const mdastSplitter = (mdast, checker, _options) => { return _mdastSplitter((0, import_mdast_utils.unwarpRoot)(mdast), checker, getSplitterGenerator()); }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { mdastSplitter });