Skip to content

Commit 8b13e64

Browse files
authored
Merge pull request #103 from code-hike/empty-lang
Allow empty lang
2 parents 60e0c20 + faafeb2 commit 8b13e64

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

packages/highlighter/src/index.tsx

+11
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import { Code } from "@code-hike/utils"
1212
let highlighterPromise: Promise<Highlighter> | null = null
1313
let highlighter: Highlighter | null = null
1414

15+
const newlineRe = /\r\n|\r|\n/
16+
1517
export async function highlight({
1618
code,
1719
lang,
@@ -21,6 +23,15 @@ export async function highlight({
2123
lang: string
2224
theme: any // TODO type this
2325
}): Promise<Code> {
26+
if (lang === "text") {
27+
const lines = code ? code.split(newlineRe) : [""]
28+
return {
29+
lines: lines.map(line => ({
30+
tokens: [{ content: line, props: {} }],
31+
})),
32+
lang,
33+
}
34+
}
2435
if (highlighterPromise === null) {
2536
setCDN("https://unpkg.com/shiki/")
2637
highlighterPromise = getHighlighter({

packages/mdx/src/plugin/code.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,11 @@ async function mapFile(
149149
): Promise<CodeStep & FileOptions & { name: string }> {
150150
const { theme } = config
151151

152+
const lang = (node.lang as string) || "text"
153+
152154
const code = await highlight({
153155
code: node.value as string,
154-
lang: node.lang as string,
156+
lang,
155157
theme,
156158
})
157159

packages/playground/content/simple-code.mdx

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
22

3+
```
4+
function lorem(ipsum, dolor = 1) {
5+
return ipsum + dolor;
6+
}
7+
```
8+
9+
---
10+
311
```js
412
function lorem(ipsum, dolor = 1) {}
513
```

0 commit comments

Comments
 (0)