Skip to content

Commit 930646c

Browse files
committed
Don't fail with undefined children in remark plugin
1 parent 58c487b commit 930646c

File tree

5 files changed

+76
-6
lines changed

5 files changed

+76
-6
lines changed

.changeset/friendly-rockets-teach.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"codehike": patch
3+
---
4+
5+
Don't fail with `undefined` `children` in remark plugin

apps/web/content/blog/the-curse-of-markdown.mdx

+27-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
---
22
title: The Curse of Markdown
3-
description: And how to break free from it
4-
date: 2024-09-12
3+
description: And the website wasteland
4+
date: 2024-10-12
55
authors: [pomber]
66
draft: true
77
---
88

9+
import { Chart } from "./the-curse-of-markdown"
10+
911
Markdown is so good that it can hurt you.
1012

11-
chapters:
13+
first
14+
15+
<Chart name="first" />
16+
17+
In a world without Markdown,
18+
19+
<Chart name="second" />
20+
21+
chapters:s
1222

1323
- introducing richness vs ax
1424
- pre-markdown tradeoffs
@@ -36,3 +46,17 @@ Examples are:
3646

3747
- stripe
3848
- swiftui
49+
50+
---
51+
52+
specific tools like:
53+
54+
- swiftui docc
55+
- tutorialkit
56+
- codehike before v1
57+
58+
---
59+
60+
The curse:
61+
62+
More often than not, the answer is to stick with Markdown and compromise on the complexity of the site. It’s easier, it’s familiar, and it lets you avoid the cost of adopting more complicated solutions. And that’s where the curse lies: Markdown is so effective at the simple stuff that we often don't even try to build things that are slightly more ambitious. The result is a gap in the spectrum of static sites — a whole category of rich, content-driven sites that never get built because the trade-off doesn’t seem worth it.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const first = [
2+
{ rich: 10, cost: 50, name: "readme" },
3+
{ rich: 20, cost: 50, name: "static blog" },
4+
{ rich: 40, cost: 50, name: "static docs" },
5+
{ rich: 60, cost: 50, name: "interactive blog" },
6+
{ rich: 75, cost: 50, name: "interactive tutorial" },
7+
{ rich: 90, cost: 50, name: "landing page" },
8+
]
9+
10+
const second = [
11+
{ rich: 10, cost: 10, name: "readme" },
12+
{ rich: 20, cost: 18, name: "static blog" },
13+
{ rich: 40, cost: 43, name: "static docs" },
14+
{ rich: 60, cost: 56, name: "interactive blog" },
15+
{ rich: 75, cost: 80, name: "interactive tutorial" },
16+
{ rich: 90, cost: 95, name: "landing page" },
17+
]
18+
19+
const w = 300
20+
const h = 200
21+
22+
export function Chart({ name }: { name: string }) {
23+
const points = name === "first" ? first : second
24+
// Scatter plot
25+
return (
26+
<div className="">
27+
<div
28+
style={{ width: w, height: h }}
29+
className="relative border-l-2 border-b-2"
30+
>
31+
{points.map(({ rich, cost }, i) => (
32+
<div
33+
key={i}
34+
style={{ left: `${rich}%`, bottom: `${cost}%` }}
35+
className="absolute w-2 h-2 bg-blue-500 rounded-full"
36+
/>
37+
))}
38+
</div>
39+
</div>
40+
)
41+
}

packages/codehike/src/mdx/1.0.transform-hikes.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export async function transformAllHikes(root: Root, config: CodeHikeConfig) {
1111
const hikes: MdxJsxFlowElement[] = []
1212

1313
visit(tree, "mdxJsxFlowElement", (node) => {
14-
if (node.children.some(isHikeElement)) {
14+
if (node.children?.some(isHikeElement)) {
1515
hikes.push(node)
1616
}
1717
})
@@ -24,7 +24,7 @@ export async function transformAllHikes(root: Root, config: CodeHikeConfig) {
2424
function wrapInHike(root: Root) {
2525
// if we find any hikeable element outside of <Hike>s,
2626
// let's wrap everything in a <Hike>
27-
if (root.children.some(isHikeElement)) {
27+
if (root.children?.some(isHikeElement)) {
2828
root.children = [
2929
{
3030
type: "mdxJsxFlowElement",

packages/codehike/src/mdx/1.1.remark-list-to-section.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export async function listToSection(
6565
hikeElement: MdxJsxFlowElement,
6666
config: CodeHikeConfig,
6767
): Promise<HikeSection> {
68-
const { children } = hikeElement
68+
const { children = [] } = hikeElement
6969

7070
const root: HikeSection = {
7171
type: "section",

0 commit comments

Comments
 (0)