2022-08-11 12:38:08 +00:00
|
|
|
//@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";
|
2022-08-11 12:38:08 +00:00
|
|
|
// @ts-ignore
|
|
|
|
import { micromark } from "https://esm.sh/micromark@3?bundle";
|
2023-05-13 15:58:34 +00:00
|
|
|
// @ts-ignore
|
|
|
|
import {frontmatter, frontmatterHtml} from 'https://esm.sh/micromark-extension-frontmatter@1?bundle'
|
2022-08-11 12:38:08 +00:00
|
|
|
|
|
|
|
/**
|
2023-05-09 02:00:53 +00:00
|
|
|
* Loads and parses a markdown document. Makes use of micromark.
|
2022-08-11 12:38:08 +00:00
|
|
|
* @param {string} path the path to load
|
|
|
|
*/
|
|
|
|
export const fetchMarkdown = (path) =>
|
|
|
|
fetchText(path)
|
|
|
|
.then(waitIfLocalHost())
|
|
|
|
.then((raw) => {
|
2023-05-13 15:58:34 +00:00
|
|
|
const output = micromark(raw, {
|
|
|
|
extensions: [frontmatter()],
|
|
|
|
htmlExtensions: [frontmatterHtml()]
|
|
|
|
})
|
|
|
|
const content = generateDomFromString(output);
|
2023-05-09 02:00:53 +00:00
|
|
|
const title = getFirstTitleContent(content) || path.replace(/\.\w{2, 4}$/, "");
|
2022-08-11 12:38:08 +00:00
|
|
|
return { title, raw, content };
|
|
|
|
});
|
2022-08-11 20:29:46 +00:00
|
|
|
|
|
|
|
export default fetchMarkdown;
|