-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
76 additions
and
99 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,98 +1,63 @@ | ||
import type { Element, Root } from "hast"; | ||
import type { Plugin } from "unified"; | ||
import type { Root } from "hast"; | ||
import type { Plugin, Transformer } from "unified"; | ||
import { h } from "hastscript"; | ||
import { visit } from "unist-util-visit"; | ||
import { | ||
buildRequestURL, | ||
extractCodeByLines, | ||
extractFilePathFromURL, | ||
extractLinesFromURL, | ||
extractRefFromURL, | ||
extractRepoNameFromURL, | ||
} from "./util"; | ||
import { buildRequestURL, extractCodeByLines, extractRepoDataFromURL } from "./utils"; | ||
|
||
interface EmbeddedGithubCode { | ||
url: string; | ||
index: number; | ||
parent: Root | Element; | ||
} | ||
type Option = { | ||
githubPAT: string; | ||
}; | ||
|
||
const rehypeEmbeddedGithubCode: Plugin<Option[], Root> = (options): Transformer<Root> => { | ||
const { githubPAT } = options; | ||
|
||
const headers = { | ||
Accept: "application/vnd.github+json", | ||
Authorization: `Bearer ${githubPAT}`, | ||
"X-GitHub-Api-Version": "2022-11-28", | ||
}; | ||
|
||
const rehypeEmbeddedGithubCode: Plugin<[], Root> = () => { | ||
const embeddedGithubCodes: EmbeddedGithubCode[] = []; | ||
const githubCodePromises: Promise<void>[] = []; | ||
|
||
return async (tree) => { | ||
const transform: Transformer<Root> = async (tree) => { | ||
visit(tree, "element", (node, index, parent) => { | ||
if ( | ||
node.tagName !== "a" || | ||
node.properties["data-remark-target"] !== "remark-embedded-github-code" || | ||
typeof node.properties.href !== "string" | ||
) { | ||
if (node.tagName !== "a" || node.properties["data-remark"] !== "remark-embedded-github-code") { | ||
return; | ||
} | ||
|
||
if (index === undefined || parent === undefined) { | ||
return; | ||
} | ||
|
||
if (parent.type !== "root" && parent.type !== "element") { | ||
return; | ||
} | ||
|
||
embeddedGithubCodes.push({ | ||
url: node.properties.href, | ||
index, | ||
parent, | ||
}); | ||
}); | ||
|
||
await Promise.all( | ||
embeddedGithubCodes.map(async (embeddedGithubCode) => { | ||
const repoName = extractRepoNameFromURL(embeddedGithubCode.url); | ||
const ref = extractRefFromURL(embeddedGithubCode.url); | ||
const path = extractFilePathFromURL(embeddedGithubCode.url); | ||
const lines = extractLinesFromURL(embeddedGithubCode.url); | ||
|
||
const githubCodePromise = async (): Promise<void> => { | ||
const { repoName, ref, path, lines } = extractRepoDataFromURL(String(node.properties.href)); | ||
const requestUrl = buildRequestURL(repoName, ref, path); | ||
|
||
const response = await fetch(requestUrl, { | ||
headers: { | ||
Accept: "application/vnd.github+json", | ||
Authorization: `Bearer ${process.env.GITHUB_PAT}`, | ||
"X-GitHub-Api-Version": "2022-11-28", | ||
}, | ||
}); | ||
const content = (await response.json()) as { content: string }; | ||
|
||
const codeBase64 = content.content; | ||
let code = Buffer.from(codeBase64, "base64").toString("utf-8"); | ||
if (lines) { | ||
code = extractCodeByLines(code, lines); | ||
} | ||
try { | ||
const response = await fetch(requestUrl, { headers }); | ||
if (!response.ok) { | ||
throw new Error(`Failed to fetch code from GitHub: ${response.status}`); | ||
} | ||
const content = (await response.json()) as { content: string }; | ||
const codeBase64 = content.content; | ||
const code = extractCodeByLines(Buffer.from(codeBase64, "base64").toString("utf-8"), lines); | ||
|
||
const codeElement: Element = { | ||
type: "element", | ||
tagName: "code", | ||
properties: { | ||
className: "", | ||
}, | ||
children: [ | ||
{ | ||
type: "text", | ||
value: code, | ||
}, | ||
], | ||
}; | ||
const newElement = h("pre", [h("code", [h(null, [{ type: "text", value: code }])])]); | ||
|
||
const preElement: Element = { | ||
type: "element", | ||
tagName: "pre", | ||
children: [codeElement], | ||
properties: {}, | ||
}; | ||
parent.children[index] = newElement; | ||
} catch (error) { | ||
throw new Error("Failed to fetch code from GitHub", { cause: error }); | ||
} | ||
}; | ||
githubCodePromises.push(githubCodePromise()); | ||
}); | ||
|
||
embeddedGithubCode.parent.children[embeddedGithubCode.index] = preElement; | ||
}), | ||
); | ||
await Promise.all(githubCodePromises); | ||
}; | ||
|
||
return transform; | ||
}; | ||
|
||
export { rehypeEmbeddedGithubCode }; |
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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