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 ad36ab5 commit c6b099f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ type Option = {
githubPAT: string;
};

const rehypeEmbeddedGithubCode: Plugin<Option[], Root> = (options): Transformer<Root> => {
const rehypeGithubEmbed: Plugin<Option[], Root> = (options): Transformer<Root> => {
const { githubPAT } = options;

const transform: Transformer<Root> = async (tree) => {
const regex = new RegExp(
/https:\/\/github\.com\/[a-zA-Z0-9._-]+\/[a-zA-Z0-9._-]+\/blob\/[a-zA-Z0-9._-]+\/?[^\s]*/g,
);

const githubCodePromises: Promise<void>[] = [];
const githubEmbedPromises: Promise<void>[] = [];

const headers = {
Accept: "application/vnd.github+json",
Expand Down Expand Up @@ -46,7 +46,7 @@ const rehypeEmbeddedGithubCode: Plugin<Option[], Root> = (options): Transformer<
lines?: Lines;
};

const githubCodePromise = async ({ repoName, ref, href, path, lines }: Props): Promise<void> => {
const githubEmbedPromise = async ({ repoName, ref, href, path, lines }: Props): Promise<void> => {
const requestUrl = buildRequestURL(repoName, ref, path);
try {
const response = await fetch(requestUrl, { headers });
Expand All @@ -69,13 +69,13 @@ const rehypeEmbeddedGithubCode: Plugin<Option[], Root> = (options): Transformer<
const nodeHref = node.properties.href;
const { repoName, ref, path, lines } = extractRepoDataFromURL(nodeHref);

githubCodePromises.push(githubCodePromise({ node, repoName, href: nodeHref, ref, path, lines }));
githubEmbedPromises.push(githubEmbedPromise({ node, repoName, href: nodeHref, ref, path, lines }));
});

await Promise.all(githubCodePromises);
await Promise.all(githubEmbedPromises);
};

return transform;
};

export { rehypeEmbeddedGithubCode };
export { rehypeGithubEmbed };
File renamed without changes.
4 changes: 2 additions & 2 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import rehypeMermaid from './app/packages/rehype-mermaid/rehypeMermaid'
import remarkEmojiName from './app/packages/remark-emoji-name'
import { remarkAttentionBlock } from './app/packages/remark-attention-block'
import { rehypeAttentionBlock } from './app/packages/rehype-attention-block'
import { rehypeEmbeddedGithubCode } from './app/packages/rehype-embedded-github-code'
import { rehypeGithubEmbed } from './app/packages/rehype-github-embed'

export default defineConfig(() => {
return {
Expand All @@ -28,7 +28,7 @@ export default defineConfig(() => {
mdx({
jsxImportSource: 'hono/jsx',
remarkPlugins: [remarkGfm, remarkBreaks, remarkFrontmatter, remarkMdxFrontmatter, remarkMomijiCodeFilename, remarkEmojiName, remarkAttentionBlock],
rehypePlugins: [[rehypeMomiji, { excludeLangs: ['mermaid'] }], rehypeMermaid, [rehypeEmbeddedGithubCode, { githubPAT: "" }], rehypeAttentionBlock],
rehypePlugins: [[rehypeMomiji, { excludeLangs: ['mermaid'] }], rehypeMermaid, [rehypeGithubEmbed, { githubPAT: "" }], rehypeAttentionBlock],
})
],
}
Expand Down

0 comments on commit c6b099f

Please sign in to comment.