Skip to content

Instantly share code, notes, and snippets.

@brunosabot
Last active February 27, 2022 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunosabot/7c438152e946327b4493ca62e562ff69 to your computer and use it in GitHub Desktop.
Save brunosabot/7c438152e946327b4493ca62e562ff69 to your computer and use it in GitHub Desktop.
import visit from "async-unist-util-visit";
const gist = () => async (ast: IAST) => {
const promises: Promise<void>[] = [];
const createGist = (node: IAST) => {
if (node.children === undefined) return;
if (node.children.length === 0) return;
const hasGist = node.children.some(
({ type, value }) =>
type === "inlineCode" && value?.startsWith("gist:") && value?.length > 5
);
if (hasGist) {
const promise = loadAndTransformGist(node, node.children[0]).then(
(newNode) => {
Object.assign(node, newNode);
if (node.children?.length === 0) {
delete node.children;
}
}
);
promises.push(promise);
}
};
await visit(ast, "paragraph", createGist);
await Promise.all(promises);
return;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment