Skip to content

Commit 224fea3

Browse files
committed
Add inline code component
1 parent 6c0568c commit 224fea3

File tree

8 files changed

+82
-6
lines changed

8 files changed

+82
-6
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import React from "react"
2+
import {
3+
EditorTheme,
4+
getColor,
5+
transparent,
6+
ColorName,
7+
} from "@code-hike/utils"
8+
9+
export function InlineCode({
10+
className,
11+
codeConfig,
12+
children,
13+
...rest
14+
}: {
15+
className: string
16+
children?: React.ReactNode
17+
codeConfig: { theme: EditorTheme }
18+
}) {
19+
const { theme } = codeConfig
20+
return (
21+
<span
22+
className={
23+
"ch-inline-code not-prose" +
24+
(className ? " " + className : "")
25+
}
26+
{...rest}
27+
>
28+
<code
29+
style={{
30+
background: transparent(
31+
getColor(theme, ColorName.CodeBackground),
32+
0.9
33+
),
34+
color: getColor(theme, ColorName.CodeForeground),
35+
}}
36+
>
37+
{children}
38+
</code>
39+
</span>
40+
)
41+
}

packages/mdx/src/components.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
Annotation,
1313
} from "./client/annotations"
1414
import { Preview } from "./client/preview"
15+
import { InlineCode } from "./client/inline-code"
1516

1617
export const CH = {
1718
Code,
@@ -24,4 +25,5 @@ export const CH = {
2425
annotations: annotationsMap,
2526
Annotation,
2627
Slideshow,
28+
InlineCode,
2729
}

packages/mdx/src/index.scss

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,10 @@
1717
height: 100%;
1818
}
1919
}
20+
21+
.ch-inline-code > code {
22+
padding: 0.2em 0.4em;
23+
margin: 0.1em -0.1em;
24+
border-radius: 0.25em;
25+
font-size: 0.9em;
26+
}

packages/mdx/src/plugin.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { transformSlideshows } from "./plugin/slideshow"
99
import { valueToEstree } from "./plugin/to-estree"
1010
import { CH_CODE_CONFIG_VAR_NAME } from "./plugin/unist-utils"
1111
import { transformPreviews } from "./plugin/preview"
12+
import { transformInlineCodes } from "./plugin/inline-code"
1213

1314
type CodeHikeConfig = {
1415
theme: any
@@ -36,6 +37,7 @@ export function remarkCodeHike(config: CodeHikeConfig) {
3637
}
3738

3839
try {
40+
await transformInlineCodes(tree)
3941
await transformPreviews(tree)
4042
await transformScrollycodings(tree, config)
4143
await transformSpotlights(tree, config)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {
2+
visitAsync,
3+
toJSX,
4+
CH_CODE_CONFIG_PLACEHOLDER,
5+
} from "./unist-utils"
6+
import { Node } from "unist"
7+
8+
export async function transformInlineCodes(tree: Node) {
9+
await visitAsync(
10+
tree,
11+
["mdxJsxFlowElement", "mdxJsxTextElement"],
12+
async node => {
13+
if (node.name === "CH.InlineCode") {
14+
toJSX(node, {
15+
props: {
16+
codeConfig: CH_CODE_CONFIG_PLACEHOLDER,
17+
},
18+
appendProps: true,
19+
})
20+
}
21+
}
22+
)
23+
}

packages/mdx/src/plugin/unist-utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ export function toJSX(
7070
}
7171
if (!appendProps) {
7272
node.attributes = []
73+
} else {
74+
node.attributes = node.attributes || []
7375
}
7476

7577
Object.keys(props).forEach(key => {

packages/playground/content/test.mdx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
# Hello
22

3-
<CH.Preview
4-
show="/"
5-
style={{ height: 200 }}
6-
url="https://google.com"
7-
/>
3+
Foo bar <CH.InlineCode>Hello</CH.InlineCode> foo bar

packages/utils/src/theme.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,10 @@ export function getColor(
193193
}
194194
}
195195

196-
function transparent(color: Color, opacity: number): Color {
196+
export function transparent(
197+
color: Color,
198+
opacity: number
199+
): Color {
197200
const _opacity = Math.round(
198201
Math.min(Math.max(opacity || 1, 0), 1) * 255
199202
)

0 commit comments

Comments
 (0)