-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathmarkdoc.config.mjs
More file actions
236 lines (229 loc) · 7.86 KB
/
Copy pathmarkdoc.config.mjs
File metadata and controls
236 lines (229 loc) · 7.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import {
defineMarkdocConfig,
component,
nodes,
Markdoc,
} from "@astrojs/markdoc/config";
import schema from "./markdoc.schema.mjs";
import { generateElementId } from "./src/lib/componentUtils/generateElementId.ts";
export default defineMarkdocConfig({
// Custom Markdoc functions for cdocs conditionals. Markdoc's built-in
// functions (equals, and, or, not) and the built-in `if`/`else`/`partial`
// tags are already available, so only the cdocs-specific extras are declared
// here. Unlike Hugo's cdocs-markdoc fork (which retained hidden content for
// client-side toggling on a static site), vanilla Markdoc DROPS content whose
// condition is false at transform time — exactly the SSR behavior we want.
functions: {
// includes($trait, ["a", "b"]) -> true when the trait value is in the list.
includes: {
transform(parameters) {
const value = parameters[0];
const list = parameters[1];
return Array.isArray(list) ? list.includes(value) : false;
},
},
},
nodes: {
fence: {
render: component("./src/components/CodeBlock/CodeBlock.astro"),
attributes: {
...nodes.fence.attributes,
...schema.nodes.fence.attributes,
},
},
},
tags: {
alert: {
render: component("./src/components/Alert/Alert.astro"),
...schema.tags.alert,
},
tabs: {
render: component("./src/components/Tabs/Tabs.astro"),
...schema.tags.tabs,
// Build labels + panel wrappers here instead of letting Tabs
// do it by eager-rendering its slot. Calling Astro.slots.render()
// and then extracting innerHTML with cheerio silently drops the
// hydration-script prefix Astro emits on first island — leaving
// <astro-island> custom elements unregistered in the prod build.
//
// The @astrojs/markdoc integration only resolves component imports
// for tags that appear in the .mdoc AST. A wrapper component used
// only by this transform wouldn't qualify, so emit panels as plain
// divs and match them via :global(.tabs__panel) in Tabs.module.css.
transform(node, config) {
const tabsRender = config.tags.tabs.render;
const groupId = generateElementId("tabs");
const labels = [];
const panelIds = [];
const panels = [];
for (const child of node.children) {
if (child.type !== "tag" || child.tag !== "tab") continue;
const label = child.attributes.label ?? "";
const panelId = `${groupId}-panel-${panels.length}`;
const isActive = panels.length === 0;
labels.push(label);
panelIds.push(panelId);
panels.push(
new Markdoc.Tag(
"div",
{
id: panelId,
role: "tabpanel",
class: isActive
? "tabs__panel tabs__panel--active"
: "tabs__panel",
hidden: !isActive,
},
child.transformChildren(config),
),
);
}
return new Markdoc.Tag(
tabsRender,
{
...node.transformAttributes(config),
id: groupId,
labels,
panelIds,
},
panels,
);
},
},
tab: {
// Consumed by the `tabs` transform; this schema only declares the
// attribute shape for validation.
...schema.tags.tab,
},
stepper: {
render: component("./src/components/Stepper/Stepper.astro"),
...schema.tags.stepper,
// Like `tabs`, the transform assembles the children server-side rather
// than letting Stepper.astro eager-render its slot. Each `step` /
// `stepper-finished` child is a registered component tag, so it renders
// with proper CSS-module classes; here we just inject the shared
// attributes (stepper id, index, position, level) each child needs.
transform(node, config) {
const stepperRender = config.tags.stepper.render;
const stepRender = config.tags.step.render;
const finishedRender = config.tags["stepper-finished"].render;
const stepperId = generateElementId("stepper");
const attributes = node.transformAttributes(config);
const open = attributes.open ?? false;
const level = attributes.level ?? "h3";
const stepNodes = node.children.filter(
(child) => child.type === "tag" && child.tag === "step",
);
const stepCount = stepNodes.length;
const children = [];
let stepIndex = 0;
for (const child of node.children) {
if (child.type !== "tag") continue;
if (child.tag === "step") {
const isLastStep = stepIndex === stepCount - 1;
children.push(
new Markdoc.Tag(
stepRender,
{
title: child.attributes.title ?? "",
stepperId,
stepIndex,
isLastStep,
open,
level,
},
child.transformChildren(config),
),
);
stepIndex++;
} else if (child.tag === "stepper-finished") {
children.push(
new Markdoc.Tag(
finishedRender,
{ stepperId },
child.transformChildren(config),
),
);
}
}
return new Markdoc.Tag(
stepperRender,
{ id: stepperId, open },
children,
);
},
},
step: {
// Rendered via the `stepper` transform; schema declares attributes only.
render: component("./src/components/Stepper/Step.astro"),
...schema.tags.step,
},
"stepper-finished": {
render: component("./src/components/Stepper/StepperFinished.astro"),
...schema.tags["stepper-finished"],
},
"region-selector": {
render: component(
"./src/components/RegionSelector/RegionSelectorIsland.astro",
),
selfClosing: true,
},
"whats-next": {
render: component("./src/components/WhatsNext/WhatsNext.astro"),
...schema.tags["whats-next"],
// Markdoc groups consecutive next-link lines into a paragraph node,
// which would render <ul><p><li>...</li></p></ul> — invalid HTML that
// breaks :first-child / :last-child styling on whatsnext__item.
// Unwrap any paragraph children so next-links become direct <ul> kids.
transform(node, config) {
const whatsNextRender = config.tags["whats-next"].render;
const children = node
.transformChildren(config)
.flatMap((child) =>
child instanceof Markdoc.Tag && child.name === "p"
? child.children
: [child],
);
return new Markdoc.Tag(
whatsNextRender,
node.transformAttributes(config),
children,
);
},
},
"next-link": {
render: component("./src/components/WhatsNext/NextLink.astro"),
...schema.tags["next-link"],
},
"collapse-content": {
render: component(
"./src/components/CollapseContent/CollapseContent.astro",
),
...schema.tags["collapse-content"],
},
ui: {
render: component("./src/components/Ui/Ui.astro"),
...schema.tags.ui,
},
kbd: {
render: component("./src/components/Kbd/Kbd.astro"),
...schema.tags.kbd,
},
sup: {
render: component("./src/components/Sup/Sup.astro"),
...schema.tags.sup,
},
nbsp: {
render: component("./src/components/Nbsp/Nbsp.astro"),
...schema.tags.nbsp,
},
"agent-only": {
render: component("./src/components/AgentOnly/AgentOnly.astro"),
...schema.tags["agent-only"],
},
img: {
render: component("./src/components/Img/Img.astro"),
...schema.tags.img,
},
},
});