File tree Expand file tree Collapse file tree 8 files changed +82
-6
lines changed
Expand file tree Collapse file tree 8 files changed +82
-6
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ import {
1212 Annotation ,
1313} from "./client/annotations"
1414import { Preview } from "./client/preview"
15+ import { InlineCode } from "./client/inline-code"
1516
1617export const CH = {
1718 Code,
@@ -24,4 +25,5 @@ export const CH = {
2425 annotations : annotationsMap ,
2526 Annotation,
2627 Slideshow,
28+ InlineCode,
2729}
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ import { transformSlideshows } from "./plugin/slideshow"
99import { valueToEstree } from "./plugin/to-estree"
1010import { CH_CODE_CONFIG_VAR_NAME } from "./plugin/unist-utils"
1111import { transformPreviews } from "./plugin/preview"
12+ import { transformInlineCodes } from "./plugin/inline-code"
1213
1314type 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 )
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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 => {
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff 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 )
You can’t perform that action at this time.
0 commit comments