UNPKG

@intlayer/chokidar

Version:

Uses chokidar to scan and build Intlayer declaration files into dictionaries based on Intlayer configuration.

87 lines (85 loc) 2.45 kB
import { getMarkdownMetadata } from "@intlayer/core/markdown"; import { getInsertionValues, html, insert, md } from "@intlayer/core/transpiler"; import * as NodeTypes from "@intlayer/types/nodeType"; //#region src/utils/autoDecorateContent.ts /** * Check if a string is a markdown string */ const isMarkdown = (str) => { return [ /^\s*---/m, /^\s*#+\s/m, /^\s*[-*+]\s/m, /^\s*\d+\.\s/m, /^\s*>\s/m, /\[.+\]\(.+\)/, /!\[.+\]\(.+\)/, /`{1,3}.+`{1,3}/, /\*\*.+\*\*/, /__.+__/, /<(https?:\/\/[^\s>]+)>/ ].some((pattern) => pattern.test(str)); }; /** * Check if a string is an insertion string */ const isInsertion = (str) => getInsertionValues(str).length > 0; /** * Check if a string is an HTML/JSX string * Matches: * - <Tag> * - </Tag> * - <Tag /> * - <Tag attribute="value"> * - <Component.SubComponent> */ const isHTML = (str) => { return /<[a-zA-Z][a-zA-Z0-9\-.]*(\s+[^>]*)?\/?>/.test(str) || /<\/[a-zA-Z][a-zA-Z0-9\-.]*\s*>/.test(str); }; const leafNodeTypes = [ NodeTypes.HTML, NodeTypes.MARKDOWN, NodeTypes.INSERTION, NodeTypes.FILE, NodeTypes.REACT_NODE, NodeTypes.TEXT, NodeTypes.NUMBER, NodeTypes.BOOLEAN, NodeTypes.NULL, NodeTypes.UNKNOWN ]; /** * Automatically decorate content strings with md() or insert() if they match */ const autoDecorateContent = (content, options = true) => { if (options === false) return content; const { markdown = true, html: htmlOption = true, insertion = true } = typeof options === "object" ? options : {}; if (typeof content === "string") { if (markdown && isMarkdown(content)) return { ...md(content), metadata: getMarkdownMetadata(content) }; if (htmlOption && isHTML(content)) return html(content); if (insertion && isInsertion(content)) return insert(content); return content; } if (Array.isArray(content)) return content.map((item) => autoDecorateContent(item, options)); if (content && typeof content === "object") { if ("nodeType" in content) { const nodeType = content.nodeType; if (leafNodeTypes.includes(nodeType)) return content; if (nodeType in content) return { ...content, [nodeType]: autoDecorateContent(content[nodeType], options) }; return content; } const result = {}; for (const key of Object.keys(content)) result[key] = autoDecorateContent(content[key], options); return result; } return content; }; //#endregion export { autoDecorateContent }; //# sourceMappingURL=autoDecorateContent.mjs.map