Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
taga3s committed Dec 16, 2024
1 parent 64ce8ef commit 2b74525
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
11 changes: 7 additions & 4 deletions app/packages/rehype-embedded-github-code/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { Root } from "hast";
import type { Plugin, Transformer } from "unified";
import { h } from "hastscript";
import { visit } from "unist-util-visit";
import { buildRequestURL, extractCodeByLines, extractRepoDataFromURL } from "./utils";
import { buildRequestURL, createCodeBlock, extractCodeByLines, extractRepoDataFromURL } from "./utils";

type Option = {
githubPAT: string;
Expand Down Expand Up @@ -46,9 +45,13 @@ const rehypeEmbeddedGithubCode: Plugin<Option[], Root> = (options): Transformer<
const codeBase64 = content.content;
const code = extractCodeByLines(Buffer.from(codeBase64, "base64").toString("utf-8"), lines);

const newElement = h("pre", [h("code", [h(null, [{ type: "text", value: code }])])]);
const codeBlockElement = createCodeBlock(
`${repoName}/${path}${lines ? `#L${lines.startLine}-L${lines.endLine}` : ""}`,
String(node.properties.href),
code,
);

parent.children[index] = newElement;
parent.children[index] = codeBlockElement;
} catch (error) {
throw new Error("Failed to fetch code from GitHub", { cause: error });
}
Expand Down
13 changes: 12 additions & 1 deletion app/packages/rehype-embedded-github-code/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { h } from "hastscript";

const extractRepoName = (urlParts: string[]): string => {
return urlParts.slice(0, 2).join("/");
};
Expand Down Expand Up @@ -56,4 +58,13 @@ const extractCodeByLines = (code: string, lines: LineNumbers | undefined): strin
return codeLines.slice(lines.startLine - 1, lines.endLine).join("\n");
};

export { extractRepoDataFromURL, buildRequestURL, extractCodeByLines };
const createCodeBlock = (title: string, href: string, code: string) => {
return h("div", [
h("span", { class: ["rehype-embedded-github-code-title"] }, [
h("a", { href: href }, [h(null, [{ type: "text", value: title }])]),
]),
h("pre", [h("code", [h(null, [{ type: "text", value: code }])])]),
]);
};

export { extractRepoDataFromURL, buildRequestURL, extractCodeByLines, createCodeBlock };
1 change: 1 addition & 0 deletions app/routes/_renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export default jsxRenderer(({ children, title, description }) => {
{/* global css */}
<link rel="stylesheet" href="/static/markdown-styles.css" />
<link rel="stylesheet" href="/static/rehype-attention-block.css" />
<link rel="stylesheet" href="/static/rehype-embedded-github-code.css" />
<Style />

<Script src="/app/client.ts" async />
Expand Down
3 changes: 3 additions & 0 deletions public/static/rehype-embedded-github-code.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.rehype-embedded-github-code-title {
padding: 0 12px;
}

0 comments on commit 2b74525

Please sign in to comment.