Skip to content

Commit

Permalink
Extract another helper function in htmlToMd.ts (Qiskit#945)
Browse files Browse the repository at this point in the history
Part of Qiskit#845.
  • Loading branch information
Eric-Arellano authored Mar 4, 2024
1 parent 8b9050e commit 9196122
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions scripts/lib/api/htmlToMd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,7 @@ async function generateMarkdownFile(
handlers,
})
.use(remarkStringify, remarkStringifyOptions)
.use(() => (root: Root) => {
// merge contiguous emphasis
visit(root, "emphasis", (node, index, parent) => {
if (index === null || parent === null) return;
let nextIndex = index + 1;
while (parent.children[nextIndex]?.type === "emphasis") {
node.children.push(
...((parent.children[nextIndex] as any).children ?? []),
);
nextIndex++;
}
parent.children.splice(index + 1, nextIndex - (index + 1));

removeEmphasisSpaces(node, index, parent, "initial");
removeEmphasisSpaces(node, index, parent, "tail");
});
})
.use(() => (root: Root) => visit(root, "emphasis", mergeContiguousEmphasis))
.process(mainHtml);

return mdFile.toString().replaceAll(`<!---->`, "");
Expand Down Expand Up @@ -144,6 +128,23 @@ function prepareHandlers(meta: Metadata): Record<string, Handle> {
return handlers;
}

function mergeContiguousEmphasis(
node: Emphasis,
index: number | null,
parent: any | null,
): void {
if (index === null || parent === null) return;
let nextIndex = index + 1;
while (parent.children[nextIndex]?.type === "emphasis") {
node.children.push(...((parent.children[nextIndex] as any).children ?? []));
nextIndex++;
}
parent.children.splice(index + 1, nextIndex - (index + 1));

removeEmphasisSpaces(node, index, parent, "initial");
removeEmphasisSpaces(node, index, parent, "tail");
}

function removeEmphasisSpaces(
node: Emphasis,
index: number,
Expand Down

0 comments on commit 9196122

Please sign in to comment.