Skip to content

Commit d4b9afc

Browse files
authored
Merge pull request #253 from code-hike/add-lang-attr
Add lang attribute
2 parents 5e8435c + e697cd6 commit d4b9afc

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

packages/mdx/src/smooth-code/code-tween.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
getColor,
1111
ColorName,
1212
getColorScheme,
13+
anyValue,
1314
} from "../utils"
1415
import {
1516
useStepParser,
@@ -62,6 +63,7 @@ function useCodeShift({
6263
theme,
6364
focus: map(tween, tween => tween.focus),
6465
annotations: map(tween, tween => tween.annotations),
66+
lang: anyValue(tween, tween => tween?.code?.lang),
6567
})
6668
}
6769

packages/mdx/src/smooth-code/partial-step-parser.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type ParseInput = {
3333
focus: Tween<FocusString>
3434
annotations?: Tween<CodeAnnotation[] | undefined>
3535
highlightedLines: FullTween<HighlightedLine[]>
36+
lang: string
3637
}
3738

3839
export function useStepParser(input: ParseInput) {
@@ -54,6 +55,7 @@ function parse({
5455
focus,
5556
annotations,
5657
highlightedLines,
58+
lang,
5759
}: ParseInput) {
5860
const normalCode = getCode(highlightedLines)
5961

@@ -74,7 +76,11 @@ function parse({
7476
multilineAnnotations
7577
)
7678

77-
const codeStep = addExtraStuff(annotatedCode, normalCode)
79+
const codeStep = addExtraStuff(
80+
annotatedCode,
81+
normalCode,
82+
lang
83+
)
7884

7985
// console.log({ codeStep })
8086

@@ -274,11 +280,13 @@ export type CodeShift = {
274280
verticalInterval: [number, number]
275281
lineCount: FullTween<number>
276282
code: FullTween<string>
283+
lang: string
277284
}
278285

279286
function addExtraStuff(
280287
codeStep: AnnotatedCode,
281-
code: FullTween<string>
288+
code: FullTween<string>,
289+
lang: string
282290
): CodeShift {
283291
const vInterval = verticalInterval(
284292
codeStep.enterCount,
@@ -302,6 +310,7 @@ function addExtraStuff(
302310
groups: newGroups,
303311
verticalInterval: vInterval,
304312
code,
313+
lang,
305314
}
306315
}
307316

packages/mdx/src/smooth-code/smooth-container.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export function SmoothContainer({
6868
<Container
6969
width={dimensions!.containerWidth}
7070
height={dimensions!.containerHeight}
71+
lang={codeStep.lang}
7172
>
7273
<Content
7374
dx={dx}
@@ -89,9 +90,11 @@ function Container({
8990
width,
9091
height,
9192
children,
93+
lang,
9294
}: {
9395
width: number
9496
height: number
97+
lang: string
9598
children: React.ReactNode
9699
}) {
97100
return (
@@ -104,6 +107,7 @@ function Container({
104107
}}
105108
className="ch-code-scroll-parent"
106109
children={children}
110+
data-ch-lang={lang}
107111
/>
108112
)
109113
}

packages/mdx/src/utils/tween.ts

+7
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,10 @@ export function mapWithDefault<T, R>(
4343
): FullTween<R> {
4444
return map(withDefault(tween, deft), fn)
4545
}
46+
47+
export function anyValue<T, R>(
48+
tween: AnyTween<T>,
49+
fn: (t: T) => R
50+
): R {
51+
return fn(tween.prev) || fn(tween.next)
52+
}

0 commit comments

Comments
 (0)