UNPKG

webforai

Version:

A library that provides a web interface for AI

44 lines (43 loc) 2.04 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_code_handler_exports = {}; __export(custom_code_handler_exports, { customCodeHandler: () => customCodeHandler }); module.exports = __toCommonJS(custom_code_handler_exports); 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 LANGUAGE_MATCH_REGEX = [/language-(\w+)/, /highlight-source-(\w+)/, /CodeBlock--language-(\w+)/]; const customCodeHandler = (state, node) => { const classNames = node.properties?.className || []; const codeValue = (0, import_trim_trailing_lines.trimTrailingLines)((0, import_hast_util_to_text.toText)(node)).trim(); const classLang = classNames.map((className) => { const match = LANGUAGE_MATCH_REGEX.map((regex) => className.match(regex)).find((match2) => match2); return match?.[1]; }).find((className) => className); const lang = classLang || (0, import_detect_code_lang.detectLanguage)(codeValue) || null; const result = { type: "code", lang, meta: null, value: codeValue }; state.patch(node, result); return result; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { customCodeHandler });