Skip to content

Commit 1f0f4c8

Browse files
Merge pull request #41 from linked-planet/dev
first simple draft of a code block
2 parents ca570a9 + 967a88e commit 1f0f4c8

File tree

9 files changed

+1581
-455
lines changed

9 files changed

+1581
-455
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { common, createLowlight } from "lowlight"
2+
import { Fragment, jsx, jsxs } from "react/jsx-runtime"
3+
import { toJsxRuntime } from "hast-util-to-jsx-runtime"
4+
import { memo } from "react"
5+
6+
import "./theme.scss"
7+
8+
/**
9+
* @typedef {import('hast').Root} Root
10+
*
11+
* @typedef {import('lowlight')}
12+
*/
13+
14+
const lowLight = createLowlight(common)
15+
16+
function CodeBlock({
17+
children,
18+
language,
19+
}: {
20+
/* children is the code to be rendered */
21+
children: string
22+
/* language is the language of the code */
23+
language?: string
24+
}) {
25+
const ast = language
26+
? lowLight.highlight(language, children)
27+
: lowLight.highlightAuto(children)
28+
// @ts-expect-error: react types don’t type these.
29+
const jsxTree = toJsxRuntime(ast, { Fragment, jsx, jsxs })
30+
31+
return (
32+
<pre className="overflow-auto bg-neutral p-2 border-l-4 border-l-solid border-l-neutral-pressed">
33+
{jsxTree}
34+
</pre>
35+
)
36+
}
37+
38+
const memoizedCodeBlock = memo(CodeBlock)
39+
export { memoizedCodeBlock as CodeBlock }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { CodeBlock } from "./CodeBlock"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@use "sass:meta";
2+
3+
html[data-color-mode="light"],
4+
html[data-color-mode="fallback"],
5+
html[data-color-mode="aui-light"],
6+
html[data-color-mode="auto"]
7+
{
8+
@include meta.load-css("highlight.js/styles/github");
9+
}
10+
html[data-color-mode="dark"] {
11+
@include meta.load-css("highlight.js/styles/github-dark-dimmed");
12+
}
13+

library/src/components/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ export * from "./Banner"
3232
export * from "./Blanket"
3333
export * from "./Breadcrumbs"
3434
export * from "./SectionMessage"
35+
export * from "./codeblock"

0 commit comments

Comments
 (0)