Skip to content

Commit

Permalink
perf: reuse same manifest md instance for all calls
Browse files Browse the repository at this point in the history
  • Loading branch information
ext committed Nov 4, 2024
1 parent 355aa94 commit 616f226
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions src/manifest/manifest-page-from-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,32 @@ type ManifestPage = Manifest["pages"][number];
type ManifestOutline = ManifestPage["outline"];
type ManifestExamples = ManifestPage["examples"];

const md = markdownIt();

md.renderer.rules.fence = (
tokens,
idx,
_options,
collected: ManifestExamples,
) => {
const { content, info, map } = tokens[idx];
const [language, ...tags] = info.split(/\s+/);
const hashContent = `${map?.[0]}:${map?.[1]}:${info}:${content}`;
const fingerprint = getFingerprint(hashContent);
let extension = undefined;

if (language === "import") {
extension = content.split(".").at(-1)?.trim();
}

collected.push({
selector: `#example-${fingerprint}`,
language: extension ?? language,
tags,
});
return "";
};

function flattenOutline(outline: DocumentOutline): ManifestOutline {
return outline.flatMap((entry) => {
return [
Expand All @@ -27,26 +53,7 @@ function findExamples(doc: DocumentLike): ManifestExamples {
return [];
}
const collected: ManifestExamples = [];
const md = markdownIt();
md.renderer.rules.fence = (tokens, idx) => {
const { content, info, map } = tokens[idx];
const [language, ...tags] = info.split(/\s+/);
const hashContent = `${map?.[0]}:${map?.[1]}:${info}:${content}`;
const fingerprint = getFingerprint(hashContent);
let extension = undefined;

if (language === "import") {
extension = content.split(".").at(-1)?.trim();
}

collected.push({
selector: `#example-${fingerprint}`,
language: extension ?? language,
tags,
});
return "";
};
md.render(doc.body);
md.render(doc.body, collected);
return collected;
}

Expand Down

0 comments on commit 616f226

Please sign in to comment.