Skip to content

Commit 215c86c

Browse files
committed
Titles in markdown (fix #9)
1 parent 1e2a026 commit 215c86c

File tree

6 files changed

+78
-6
lines changed

6 files changed

+78
-6
lines changed

.changeset/eighty-forks-compare.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"bright": patch
3+
---
4+
5+
Titles in markdown

lib/src/index.tsx

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,14 @@ function parseChildren(
185185
if (typeof children === "object" && children?.type === "code") {
186186
return {
187187
code: children.props?.children?.trim(),
188-
lang: (children.props?.className?.replace("language-", "") ||
189-
"text") as LanguageAlias,
188+
...getLanguageAndTitle(children.props?.className),
190189
}
191190
} else if (typeof children === "object") {
192191
const subProps = React.Children.toArray(children as any).map((c: any) => {
193192
const codeProps = c.props?.children?.props
194193
return {
195194
code: codeProps.children?.trim(),
196-
lang: codeProps.className?.replace("language-", "") || "text",
195+
...getLanguageAndTitle(codeProps.className),
197196
}
198197
})
199198
return {
@@ -206,3 +205,22 @@ function parseChildren(
206205
}
207206
}
208207
}
208+
209+
function getLanguageAndTitle(className: string | undefined) {
210+
if (!className) {
211+
return {
212+
lang: "text",
213+
}
214+
}
215+
const metastring = className.replace("language-", "")
216+
const lang = metastring.split(".").pop()
217+
if (lang !== metastring) {
218+
return {
219+
lang: lang!,
220+
title: metastring,
221+
}
222+
}
223+
return {
224+
lang,
225+
}
226+
}

web/app/demos/theme.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,8 @@
455455
"name": "Entity name",
456456
"scope": "entity.name.function",
457457
"settings": {
458-
"foreground": "#82AAFF"
458+
"foreground": "#c3e88dff",
459+
"fontStyle": "normal"
459460
}
460461
},
461462
{
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { NewDemo } from "./new-demo"
2+
3+
const sourceCode = `# Hello
4+
5+
This is how you add the code's title in Markdown/MDX
6+
7+
\`\`\`web/shine.js
8+
let hello = "hello brightness"
9+
console.log(hello, "my old friend")
10+
\`\`\`
11+
`
12+
13+
export default function Demo() {
14+
return (
15+
<>
16+
<div style={{ height: "1rem" }} />
17+
<NewDemo
18+
filename="app/page.mdx"
19+
sourceProps={{
20+
children: sourceCode,
21+
lang: "mdx",
22+
annotations: [
23+
{ name: "focus", ranges: [{ fromLineNumber: 3, toLineNumber: 5 }] },
24+
],
25+
}}
26+
demoProps={{
27+
title: "web/shine.js",
28+
lang: "js",
29+
code: `
30+
let hello = "hello brightness"
31+
console.log(hello, "my old friend")
32+
`.trim(),
33+
}}
34+
/>
35+
</>
36+
)
37+
}

web/app/page.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import ThemeDemo from "./demos/theme"
88
import DarkModeDemo from "./demos/dark-mode"
99
import CustomThemeDemo from "./demos/custom-theme"
1010
import MarkdownDemo from "./demos/markdown"
11+
import TitlesInMarkdownDemo from "./demos/titles-in-markdown"
1112
import ExtensionsDemo from "./demos/customization"
1213

1314
export default async function Page() {
@@ -50,6 +51,10 @@ export default async function Page() {
5051
<h2 style={{ textAlign: "center" }}>Markdown / MDX</h2>
5152
<MarkdownDemo />
5253

54+
<div style={{ height: "3rem" }} />
55+
<h2 style={{ textAlign: "center" }}>Titles in Markdown</h2>
56+
<TitlesInMarkdownDemo />
57+
5358
<div style={{ height: "3rem" }} />
5459
<h2 style={{ textAlign: "center" }}>Customization</h2>
5560
<ExtensionsDemo />

web/app/test-mdx/mdx-demo.mdx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@
44
hello
55
```
66

7-
```jsx
8-
// title app/page.js
7+
# Hello
8+
9+
```web/shine.js
10+
let hello = "hello brightness"
11+
console.log(hello, "my old friend")
12+
```
13+
14+
```app/page.jsx
915
import { Code } from "bright"
1016

1117
Code.extensions = {

0 commit comments

Comments
 (0)