-
-
Save brunosabot/7c438152e946327b4493ca62e562ff69 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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