UNPKG

webforai

Version:

A library that provides a web interface for AI

79 lines (78 loc) 3.62 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 custom_div_handler_exports = {}; __export(custom_div_handler_exports, { customDivHandler: () => customDivHandler }); module.exports = __toCommonJS(custom_div_handler_exports); var import_hast_util_select = require("hast-util-select"); var import_hast_util_to_mdast = require("hast-util-to-mdast"); var import_hast_util_to_string = require("hast-util-to-string"); var import_hast_util_to_text = require("hast-util-to-text"); var import_trim_trailing_lines = require("trim-trailing-lines"); var import_detect_code_lang = require("../utils/detect-code-lang"); const CODE_BLOCK_REGEX = /highlight-source|language-|codegroup|codeblock|code-block/i; const CODE_FILENAME_SELECTORS = "[class*='fileName'],[class*='fileName'],[class*='title'],[class*='Title']"; const LANGUAGE_MATCH_REGEX = [/language-(\w+)/, /highlight-source-(\w+)/, /CodeBlock--language-(\w+)/]; const findRecursive = (array, condition, maxDepth = 3) => { if (maxDepth <= 0) { return null; } for (const value of array) { const result = condition(value); if (Array.isArray(result)) { return findRecursive(result, condition, maxDepth - 1); } if (result) { return value; } } return null; }; const customDivHandler = (state, node) => { const classNames = Array.isArray(node.properties.className) ? node.properties.className : []; const codeBlock = findRecursive(node.children, (child) => { if (child.type !== "element") { return false; } if (child.tagName === "pre") { return true; } return child.children.filter((child2) => child2.type === "element"); }); if (codeBlock && classNames.some((className) => CODE_BLOCK_REGEX.test(className))) { const codeBlockClassNames = codeBlock.type === "element" ? codeBlock.properties.className ?? [] : []; const codeValue = (0, import_trim_trailing_lines.trimTrailingLines)((0, import_hast_util_to_text.toText)(codeBlock)).trim(); const filenameElement = (0, import_hast_util_select.select)(CODE_FILENAME_SELECTORS, node); const fileLang = filenameElement ? (0, import_hast_util_to_string.toString)(filenameElement).match(/\.(\w+)$/)?.[1] : null; const classLang = [...classNames, ...codeBlockClassNames].map((className) => { const match = LANGUAGE_MATCH_REGEX.map((regex) => className.match(regex)).find((match2) => match2); return match?.[1]; }).find((className) => className); const lang = fileLang || classLang || (0, import_detect_code_lang.detectLanguage)(codeValue) || null; const result = { type: "code", lang, meta: null, value: codeValue }; state.patch(node, result); return result; } return import_hast_util_to_mdast.defaultHandlers.div(state, node); }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { customDivHandler });