@gouvfr/dsfr-nexus
Version:
Le module `dsfr-nexus` est l'interface de ligne de commande (CLI) centrale du Système de Design de l’État - DSFR. Il offre des outils pour gérer et compiler les ressources du DSFR
52 lines (46 loc) • 1.43 kB
JavaScript
import { frontmatter } from 'micromark-extension-frontmatter';
import { directive } from 'micromark-extension-directive';
import { gfm } from 'micromark-extension-gfm';
import { frontmatterFromMarkdown } from 'mdast-util-frontmatter';
import { directiveFromMarkdown } from 'mdast-util-directive';
import { gfmFromMarkdown } from 'mdast-util-gfm';
import { fromMarkdown } from 'mdast-util-from-markdown';
import yaml from 'yaml';
const EXTENSIONS = [
frontmatter(['yaml']),
directive(),
gfm(),
];
const MDAST_EXTENSIONS = [
frontmatterFromMarkdown(['yaml']),
directiveFromMarkdown(),
gfmFromMarkdown(),
];
const OPTIONS = {
extensions: EXTENSIONS,
mdastExtensions: MDAST_EXTENSIONS
};
const parseMarkdown = (markdown) => {
const mdast = fromMarkdown(markdown, OPTIONS);
if (!mdast || !mdast.children || mdast.children.length === 0) {
return [];
}
if (mdast.type === 'root') {
return mdast.children[0].type === 'yaml' ? mdast.children.slice(1) : mdast.children;
}
return mdast.children;
};
const parseFrontMatter = (markdown) => {
const mdast = fromMarkdown(markdown, OPTIONS);
if (!mdast || !mdast.children || mdast.children.length === 0) {
return null;
}
if (mdast.type === 'root') {
const firstChild = mdast.children[0];
if (firstChild.type === 'yaml') {
return yaml.parse(firstChild.value);
}
}
return null;
}
export { parseMarkdown, parseFrontMatter };