tickle/modules/utils/fetchMarkdown.mjs
2023-05-13 17:58:34 +02:00

30 lines
1007 B
JavaScript

//@ts-check
import { fetchText } from "./fetchText.mjs";
import { waitIfLocalHost } from "./waitIfLocalHost.mjs";
import { generateDomFromString } from "./generateDomFromString.mjs";
import {getFirstTitleContent} from "./getFirstTitleContent.mjs";
// @ts-ignore
import { micromark } from "https://esm.sh/micromark@3?bundle";
// @ts-ignore
import {frontmatter, frontmatterHtml} from 'https://esm.sh/micromark-extension-frontmatter@1?bundle'
/**
* Loads and parses a markdown document. Makes use of micromark.
* @param {string} path the path to load
*/
export const fetchMarkdown = (path) =>
fetchText(path)
.then(waitIfLocalHost())
.then((raw) => {
const output = micromark(raw, {
extensions: [frontmatter()],
htmlExtensions: [frontmatterHtml()]
})
const content = generateDomFromString(output);
const title = getFirstTitleContent(content) || path.replace(/\.\w{2, 4}$/, "");
return { title, raw, content };
});
export default fetchMarkdown;