2022-08-11 12:38:08 +00:00
|
|
|
//@ts-check
|
|
|
|
|
|
|
|
import { fetchText } from "./fetchText.mjs";
|
|
|
|
import { waitIfLocalHost } from "./waitIfLocalHost.mjs";
|
|
|
|
import { rewriteLocalUrls } from "./rewriteLocalUrls.mjs";
|
|
|
|
import { generateDomFromString } from "./generateDomFromString.mjs";
|
|
|
|
// @ts-ignore
|
|
|
|
import { micromark } from "https://esm.sh/micromark@3?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 content = rewriteLocalUrls(generateDomFromString(micromark(raw)));
|
|
|
|
const firstTitleElement = content.querySelector("h1");
|
|
|
|
const title = firstTitleElement
|
|
|
|
? firstTitleElement.textContent
|
|
|
|
: path.replace(/\.\w{2, 4}$/, "");
|
|
|
|
return { title, raw, content };
|
|
|
|
});
|
2022-08-11 20:29:46 +00:00
|
|
|
|
|
|
|
export default fetchMarkdown;
|