Skip to content

Commit

Permalink
Fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
ptgott committed Nov 18, 2024
1 parent 213885c commit 29c4c9b
Showing 1 changed file with 6 additions and 18 deletions.
24 changes: 6 additions & 18 deletions server/rehype-hljs-var.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
import { unified, Transformer } from "unified";
import type { VFile } from "vfile";
import type {
MdxJsxFlowElement,
MdxJsxTextElement,
MdxJsxAttribute,
MdxJsxAttributeValueExpression,
} from "mdast-util-mdx-jsx";
import type { MDXJSEsm } from "mdast-util-mdxjs-esm";
import type {
MDXFlowExpression,
MDXTextExpression,
} from "mdast-util-mdx-expression";
import rehypeHighlight, {
Options as RehypeHighlightOptions,
} from "rehype-highlight";
Expand All @@ -35,7 +24,7 @@ export const rehypeVarInHLJS = (
return (root: Parent, file: VFile) => {
const highlighter = rehypeHighlight(options);

let placeholdersToVars: Record<string, MdxJsxFlowElement> = {};
let placeholdersToVars: Record<string, Node> = {};

// In a code snippet, Var elements are parsed as text. Replace these with
// UUID strings to ensure that the parser won't split these up and make
Expand Down Expand Up @@ -64,15 +53,14 @@ export const rehypeVarInHLJS = (
.parse(match);
if (
varElement.children.length !== 1 ||
(varElement.children[0] as MdxJsxFlowElement).name !== "Var"
(varElement.children[0] as any).name !== "Var"
) {
throw new Error(
`Problem parsing file ${file.path}: malformed Var element within a code snippet`
);
}

placeholdersToVars[placeholder] = varElement
.children[0] as MdxJsxFlowElement;
placeholdersToVars[placeholder] = varElement.children[0];
return placeholder;
}
);
Expand Down Expand Up @@ -125,7 +113,7 @@ export const rehypeVarInHLJS = (
// The element's text includes one or more Vars among other content, so
// we need to replace the span with a series of spans separated by
// Vars.
let newChildren: Array<MdxJsxFlowElement | Element> = [];
let newChildren: Array<Text | Element> = [];

// Assemble a map of indexes to their corresponding placeholders so we
// can tell whether a given index falls within a placeholder.
Expand All @@ -141,7 +129,7 @@ export const rehypeVarInHLJS = (
if (placeholderIndices.has(valueIdx)) {
const placeholder = placeholderIndices.get(valueIdx);
valueIdx += placeholder.length;
newChildren.push(placeholdersToVars[placeholder]);
newChildren.push(placeholdersToVars[placeholder] as Element);
continue;
}
// The current index is outside a placeholder, so assemble a text or
Expand Down Expand Up @@ -175,7 +163,7 @@ export const rehypeVarInHLJS = (
}

// Delete the current span and replace it with the new children.
(parent.children as Array<MdxJsxFlowElement | Element>).splice(
(parent.children as Array<Text | Element>).splice(
index,
1,
...newChildren
Expand Down

0 comments on commit 29c4c9b

Please sign in to comment.