@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
61 lines (59 loc) • 2.04 kB
JavaScript
import { getContent } from "../../interpreter/getContent/getContent.mjs";
import { getHTMLCustomComponents } from "../html/getHTMLCustomComponents.mjs";
import { getMarkdownMetadata } from "./getMarkdownMetadata.mjs";
import { validateMarkdown } from "./validateMarkdown.mjs";
import { MARKDOWN, formatNodeType } from "@intlayer/types/nodeType";
//#region src/transpiler/markdown/markdown.ts
const awaitContent = async (content) => {
if (typeof content === "string" || typeof content === "object") return content;
if (typeof content === "function") return content();
if (typeof content.then === "function") return await content;
};
/**
* Function intended to be used to build intlayer dictionaries.
*
* Allow to pick a content based on a quantity.
*
* Usage:
*
* ```ts
* markdown('## Hello world!');
* ```
*
*/
const markdown = (content, components) => {
const metadata = async () => {
const flatContent = getContent(await awaitContent(content), {
dictionaryKey: "",
keyPath: []
});
if (typeof flatContent === "string") {
{
const { issues } = validateMarkdown(flatContent);
for (const issue of issues) if (issue.type === "error") console.error(`[intlayer/markdown] ${issue.message}`);
else console.warn(`[intlayer/markdown] ${issue.message}`);
}
return getMarkdownMetadata(flatContent);
}
};
const getComponents = () => {
if (components) return components;
if (typeof content === "string") return getHTMLCustomComponents(content);
let stringContent;
if (typeof content === "function") stringContent = content();
else if (typeof content.then === "function") stringContent = async () => getHTMLCustomComponents(await content);
if (typeof stringContent === "string") return getHTMLCustomComponents(stringContent);
try {
return getHTMLCustomComponents(JSON.stringify(content));
} catch (_e) {
return [];
}
};
return formatNodeType(MARKDOWN, content, {
metadata,
tags: getComponents()
});
};
//#endregion
export { markdown as md };
//# sourceMappingURL=markdown.mjs.map