-
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.
wip: introduce remark / rehype embedded-github-code
- Loading branch information
Showing
4 changed files
with
194 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import type { Element, Root } from "hast"; | ||
import type { Plugin } from "unified"; | ||
import { visit } from "unist-util-visit"; | ||
import { | ||
buildRequestURL, | ||
extractCodeByLines, | ||
extractFilePathFromURL, | ||
extractLinesFromURL, | ||
extractRefFromURL, | ||
extractRepoNameFromURL, | ||
} from "./util"; | ||
|
||
interface EmbeddedGithubCode { | ||
url: string; | ||
index: number; | ||
parent: Root | Element; | ||
} | ||
|
||
const rehypeEmbeddedGithubCode: Plugin<[], Root> = () => { | ||
const embeddedGithubCodes: EmbeddedGithubCode[] = []; | ||
|
||
return 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" | ||
) { | ||
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 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); | ||
} | ||
|
||
const codeElement: Element = { | ||
type: "element", | ||
tagName: "code", | ||
properties: { | ||
className: "", | ||
}, | ||
children: [ | ||
{ | ||
type: "text", | ||
value: code, | ||
}, | ||
], | ||
}; | ||
|
||
const preElement: Element = { | ||
type: "element", | ||
tagName: "pre", | ||
children: [codeElement], | ||
properties: {}, | ||
}; | ||
|
||
embeddedGithubCode.parent.children[embeddedGithubCode.index] = preElement; | ||
}), | ||
); | ||
}; | ||
}; | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
const extractRepoNameFromURL = (url: string): string => { | ||
const base = url.replace("https://github.com/", ""); | ||
const urlParts = base.split("/"); | ||
return urlParts.slice(0, 2).join("/"); | ||
}; | ||
|
||
const extractFilePathFromURL = (url: string): string => { | ||
const base = url.replace("https://github.com/", ""); | ||
const urlParts = base.split("/"); | ||
const result = urlParts.slice(4).join("/"); | ||
const hashIndex = result.indexOf("#"); | ||
if (hashIndex === -1) { | ||
return result; | ||
} | ||
return result.slice(0, hashIndex); | ||
}; | ||
|
||
const extractRefFromURL = (url: string): string => { | ||
const base = url.replace("https://github.com/", ""); | ||
const urlParts = base.split("/"); | ||
return urlParts[3]; | ||
}; | ||
|
||
type LineNumbers = { | ||
startLine: number; | ||
endLine: number; | ||
}; | ||
|
||
const extractLinesFromURL = (url: string): LineNumbers | undefined => { | ||
const base = url.replace("https://github.com/", ""); | ||
const urlParts = base.split("/"); | ||
const result = urlParts.slice(4).join("/"); | ||
const hashIndex = result.indexOf("#"); | ||
if (hashIndex === -1) { | ||
return; | ||
} | ||
const hash = result.slice(hashIndex + 1); | ||
const numbers = hash.match(/\d+/g); | ||
if (!numbers) return; | ||
return { | ||
startLine: Number.parseInt(numbers[0]), | ||
endLine: Number.parseInt(numbers[1]), | ||
}; | ||
}; | ||
|
||
const buildRequestURL = (repo: string, ref: string, path: string): string => { | ||
return `https://api.github.com/repos/${repo}/contents/${path}?ref=${ref}`; | ||
}; | ||
|
||
const extractCodeByLines = (code: string, lines: LineNumbers): string => { | ||
const codeLines = code.split("\n"); | ||
return codeLines.slice(lines.startLine - 1, lines.endLine).join("\n"); | ||
}; | ||
|
||
export { | ||
extractRepoNameFromURL, | ||
extractFilePathFromURL, | ||
extractRefFromURL, | ||
extractLinesFromURL, | ||
buildRequestURL, | ||
extractCodeByLines, | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import type { Root } from "mdast"; | ||
import type { Plugin } from "unified"; | ||
import { visit } from "unist-util-visit"; | ||
|
||
const remarkEmbeddedGithubCode: Plugin<[], Root> = () => { | ||
//TODO: check this regex | ||
const regex = /https:\/\/github\.com\/[a-zA-Z0-9._-]+\/[a-zA-Z0-9._-]+\/blob\/[a-zA-Z0-9._-]+\/?[^\s]*/g; | ||
return (tree) => { | ||
visit(tree, "link", (node, _, parent) => { | ||
if (node.url && typeof node.url === "string") { | ||
const matches = node.url.match(regex); | ||
if (matches) { | ||
node.data = { | ||
hProperties: { | ||
"data-remark-target": "remark-embedded-github-code", | ||
}, | ||
}; | ||
|
||
if (parent && parent.type === "paragraph") { | ||
parent.data = { | ||
hName: "div", | ||
}; | ||
} | ||
} | ||
} | ||
}); | ||
}; | ||
}; | ||
|
||
export { remarkEmbeddedGithubCode }; |
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