tickle/modules/utils/fetchMarkdown.mjs

24 lines
772 B
JavaScript
Raw Normal View History

//@ts-check
import { fetchText } from "./fetchText.mjs";
import { waitIfLocalHost } from "./waitIfLocalHost.mjs";
import { generateDomFromString } from "./generateDomFromString.mjs";
2023-05-09 02:00:53 +00:00
import {getFirstTitleContent} from "./getFirstTitleContent.mjs";
// @ts-ignore
import { micromark } from "https://esm.sh/micromark@3?bundle";
/**
2023-05-09 02:00:53 +00:00
* 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) => {
2023-05-09 02:00:53 +00:00
const content = generateDomFromString(micromark(raw));
const title = getFirstTitleContent(content) || path.replace(/\.\w{2, 4}$/, "");
return { title, raw, content };
});
export default fetchMarkdown;