webforai
Version:
A library that provides a web interface for AI
56 lines (55 loc) • 2.36 kB
JavaScript
// src/mdast-handlers/custom-div-handler.ts
import { select } from "hast-util-select";
import { defaultHandlers } from "hast-util-to-mdast";
import { toString as hastToString } from "hast-util-to-string";
import { toText } from "hast-util-to-text";
import { trimTrailingLines } from "trim-trailing-lines";
import { detectLanguage } from "../utils/detect-code-lang.js";
var CODE_BLOCK_REGEX = /highlight-source|language-|codegroup|codeblock|code-block/i;
var CODE_FILENAME_SELECTORS = "[class*='fileName'],[class*='fileName'],[class*='title'],[class*='Title']";
var LANGUAGE_MATCH_REGEX = [/language-(\w+)/, /highlight-source-(\w+)/, /CodeBlock--language-(\w+)/];
var 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;
};
var 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 = trimTrailingLines(toText(codeBlock)).trim();
const filenameElement = select(CODE_FILENAME_SELECTORS, node);
const fileLang = filenameElement ? hastToString(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 || detectLanguage(codeValue) || null;
const result = { type: "code", lang, meta: null, value: codeValue };
state.patch(node, result);
return result;
}
return defaultHandlers.div(state, node);
};
export {
customDivHandler
};