From 64ce8efc45604439de0824b3ef2f3f77767b4786 Mon Sep 17 00:00:00 2001 From: taga3s Date: Mon, 16 Dec 2024 00:35:53 +0900 Subject: [PATCH] refactor --- .../rehype-embedded-github-code/index.ts | 4 ++- .../remark-embedded-github-code/index.ts | 30 ------------------- vite.config.ts | 3 +- 3 files changed, 4 insertions(+), 33 deletions(-) delete mode 100644 app/packages/remark-embedded-github-code/index.ts diff --git a/app/packages/rehype-embedded-github-code/index.ts b/app/packages/rehype-embedded-github-code/index.ts index a91225e..8cafb43 100644 --- a/app/packages/rehype-embedded-github-code/index.ts +++ b/app/packages/rehype-embedded-github-code/index.ts @@ -19,9 +19,11 @@ const rehypeEmbeddedGithubCode: Plugin = (options): Transformer< const githubCodePromises: Promise[] = []; + const regex = new RegExp(/https:\/\/github\.com\/[a-zA-Z0-9._-]+\/[a-zA-Z0-9._-]+\/blob\/[a-zA-Z0-9._-]+\/?[^\s]*/g); + const transform: Transformer = async (tree) => { visit(tree, "element", (node, index, parent) => { - if (node.tagName !== "a" || node.properties["data-remark"] !== "remark-embedded-github-code") { + if (node.tagName !== "a" || node.properties.href === undefined || !regex.test(String(node.properties.href))) { return; } if (index === undefined || parent === undefined) { diff --git a/app/packages/remark-embedded-github-code/index.ts b/app/packages/remark-embedded-github-code/index.ts deleted file mode 100644 index c018a56..0000000 --- a/app/packages/remark-embedded-github-code/index.ts +++ /dev/null @@ -1,30 +0,0 @@ -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 = new RegExp(/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": "remark-embedded-github-code", - }, - }; - - if (parent && parent.type === "paragraph") { - parent.data = { - hName: "div", - }; - } - } - } - }); - }; -}; - -export { remarkEmbeddedGithubCode }; diff --git a/vite.config.ts b/vite.config.ts index d96ec1f..9d6a17f 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -14,7 +14,6 @@ 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 { remarkEmbeddedGithubCode } from './app/packages/remark-embedded-github-code' import { rehypeEmbeddedGithubCode } from './app/packages/rehype-embedded-github-code' export default defineConfig(() => { @@ -28,7 +27,7 @@ export default defineConfig(() => { ssg({ entry: "./app/server.ts" }), mdx({ jsxImportSource: 'hono/jsx', - remarkPlugins: [remarkGfm, remarkBreaks, remarkFrontmatter, remarkMdxFrontmatter, remarkEmbeddedGithubCode, remarkMomijiCodeFilename, remarkEmojiName, remarkAttentionBlock], + remarkPlugins: [remarkGfm, remarkBreaks, remarkFrontmatter, remarkMdxFrontmatter, remarkMomijiCodeFilename, remarkEmojiName, remarkAttentionBlock], rehypePlugins: [[rehypeMomiji, { excludeLangs: ['mermaid'] }], rehypeMermaid, [rehypeEmbeddedGithubCode, { githubPAT: "" }], rehypeAttentionBlock], }) ],