-
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.
refactor(code): rehype code wrapper (#13)
- Loading branch information
Showing
9 changed files
with
119 additions
and
70 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,24 @@ | ||
--- | ||
import { Code } from "astro/components" | ||
import type { HTMLAttributes } from "astro/types" | ||
import { CopyButton } from "@/components/copy-button" | ||
import { cn } from "@/lib/utils" | ||
interface Props { | ||
lang?: "tsx" | "vue" | ||
interface Props extends HTMLAttributes<"div"> { | ||
lang?: "tsx" | "vue" | "ts" | "json" | ||
className?: string | ||
src: string | ||
hidden?: boolean | ||
} | ||
const { src, lang, hidden } = Astro.props | ||
const html = Astro.slots.render("default") | ||
const { src, lang, className } = Astro.props | ||
--- | ||
|
||
<div class="code-wrapper relative" hidden={hidden}> | ||
<div class={cn("code-wrapper relative", className)}> | ||
{ | ||
lang && src ? ( | ||
<Code lang={lang} wrap={false} theme="vesper" code={src} /> | ||
) : ( | ||
<Fragment set:html={html} /> | ||
) | ||
) : null | ||
} | ||
<CopyButton client:visible className="absolute top-4 right-4" value={src} /> | ||
</div> |
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import type { ShikiConfig } from "@astrojs/markdown-remark" | ||
import { transformerMetaHighlight } from "@shikijs/transformers" | ||
import type { Element, Root } from "hast" | ||
import type { MdxjsEsm } from "mdast-util-mdx" | ||
import type { MdxJsxFlowElementHast } from "mdast-util-mdx-jsx" | ||
import { visit } from "unist-util-visit" | ||
|
||
export const shikiConfig = { | ||
theme: "vesper", | ||
transformers: [ | ||
{ | ||
root(root) { | ||
const pre = root.children?.[0] as Element | ||
if (pre) pre.properties.source = this.source | ||
}, | ||
}, | ||
transformerMetaHighlight({ | ||
className: "line--highlighted", | ||
}), | ||
], | ||
} satisfies ShikiConfig | ||
|
||
export function rehypeCodeWrapper() { | ||
return (tree: Root) => { | ||
visit(tree, { type: "element", tagName: "pre" }, (node, index, parent) => { | ||
if ( | ||
typeof node.properties.dataLanguage === "string" && | ||
typeof node.properties.source === "string" | ||
) { | ||
const componentElement: MdxJsxFlowElementHast = { | ||
type: "mdxJsxFlowElement", | ||
name: "CodeWrapper", | ||
attributes: [ | ||
{ | ||
type: "mdxJsxAttribute", | ||
name: "lang", | ||
value: node.properties.dataLanguage, | ||
}, | ||
{ | ||
type: "mdxJsxAttribute", | ||
name: "src", | ||
value: node.properties.source, | ||
}, | ||
], | ||
children: node.children, | ||
} | ||
// biome-ignore lint/style/noNonNullAssertion: <explanation> | ||
parent!.children.splice(index!, 1, componentElement) | ||
} | ||
}) | ||
|
||
const codeWrapperImport: MdxjsEsm = { | ||
type: "mdxjsEsm", | ||
value: "", | ||
data: { | ||
estree: { | ||
body: [ | ||
{ | ||
type: "ImportDeclaration", | ||
specifiers: [ | ||
{ | ||
type: "ImportDefaultSpecifier", | ||
local: { | ||
type: "Identifier", | ||
name: "CodeWrapper", | ||
}, | ||
}, | ||
], | ||
source: { | ||
type: "Literal", | ||
value: "@/components/content/code-wrapper.astro", | ||
}, | ||
}, | ||
], | ||
type: "Program", | ||
sourceType: "module", | ||
}, | ||
}, | ||
} | ||
|
||
tree.children.unshift(codeWrapperImport) | ||
} | ||
} |
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,19 +1,6 @@ | ||
import fs from "node:fs" | ||
import path from "node:path" | ||
import { type ClassValue, clsx } from "clsx" | ||
import { twMerge } from "tailwind-merge" | ||
|
||
export function cn(...inputs: ClassValue[]) { | ||
return twMerge(clsx(inputs)) | ||
} | ||
|
||
export const loadFileSource = async (relativePath: string) => { | ||
try { | ||
if (!relativePath) return "No file provided" | ||
|
||
return fs.readFileSync(path.resolve("./src", relativePath), "utf-8") | ||
} catch (e) { | ||
console.error(e) | ||
return "Not yet available" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.