@open-document/core 0.12.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{build-DfUfpeTR.js → build-CbgIk6bA.js} +1 -1
- package/dist/{check-CHxiOkQG.js → check-BTroj-5Z.js} +3 -3
- package/dist/cli/bin.js +47 -10
- package/dist/config-BWtOZ1jc.d.ts +45 -0
- package/dist/{config-9IdbhVuT.js → config-DKKNIbvO.js} +68 -320
- package/dist/{context-BLI1-64_.js → context-DXNP8v9I.js} +2 -1
- package/dist/deploy-gcp-BUiSuqz7.js +758 -0
- package/dist/{dev-yNYKv5mm.js → dev-YXumw5iX.js} +1 -1
- package/dist/{diagram-BK4jTZr4.js → diagram-CKf3ptOE.js} +293 -13
- package/dist/{diff-Bs205mOx.js → diff-CTFfU4Tz.js} +2 -2
- package/dist/{export-tkquS5O5.js → export-D2QLtF84.js} +3 -3
- package/dist/{sdk-CHEGeINh.d.ts → flow-CS5apebG.d.ts} +72 -3
- package/dist/{import-D1hIKIr3.js → import-CcmrFetU.js} +2 -2
- package/dist/index.d.ts +155 -59
- package/dist/index.js +896 -90
- package/dist/open-doc-plugin-DnX5usgI.js +320 -0
- package/dist/ops/index.d.ts +2 -2
- package/dist/ops/index.js +2 -2
- package/dist/{ops-Dgqujq5a.js → ops-DmEecq1k.js} +2 -1
- package/dist/{preview-BImdTH6K.js → preview-_mHpBhMk.js} +1 -1
- package/dist/{templates-DT5zzqHq.js → templates-MMblHwmT.js} +3 -3
- package/dist/vite/index.d.ts +1 -1
- package/dist/vite/index.js +2 -1
- package/package.json +1 -1
- package/skills/doc-authoring/SKILL.md +15 -16
- package/skills/doc-authoring/references/long-form.md +43 -2
- package/skills/doc-authoring/references/pagination.md +14 -2
- package/skills/doc-authoring/references/tables-and-charts.md +21 -17
- package/src/app/components/callout.tsx +101 -0
- package/src/app/components/chart.tsx +191 -33
- package/src/app/components/code-block.tsx +249 -0
- package/src/app/components/data-table.tsx +146 -18
- package/src/app/components/flow-page.tsx +57 -8
- package/src/app/components/numbering.tsx +226 -12
- package/src/app/components/page-frame.tsx +42 -0
- package/src/app/lib/chart.test.ts +15 -0
- package/src/app/lib/chart.ts +134 -9
- package/src/app/lib/diagnostics.ts +45 -1
- package/src/app/lib/export-dom.ts +15 -2
- package/src/app/lib/export-pdf.ts +9 -1
- package/src/app/lib/flow-measure.ts +33 -4
- package/src/app/lib/flow.test.ts +20 -1
- package/src/app/lib/flow.ts +120 -20
- package/src/app/lib/labels.ts +29 -10
- package/src/app/lib/sdk.ts +6 -0
- package/src/app/lib/use-doc-pages.ts +23 -8
- package/src/app/routes/doc.tsx +1 -0
- package/dist/config-P61niZMf.d.ts +0 -24
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import type { CSSProperties, ReactNode } from 'react';
|
|
2
|
+
import { LOC_PROP, sourceAttrs } from '../lib/source-loc';
|
|
3
|
+
|
|
4
|
+
export type CalloutVariant = 'info' | 'tip' | 'warning' | 'danger' | 'neutral';
|
|
5
|
+
|
|
6
|
+
export type CalloutProps = {
|
|
7
|
+
variant?: CalloutVariant;
|
|
8
|
+
title?: ReactNode;
|
|
9
|
+
children?: ReactNode;
|
|
10
|
+
style?: CSSProperties;
|
|
11
|
+
className?: string;
|
|
12
|
+
[LOC_PROP]?: string;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const PALETTES: Record<
|
|
16
|
+
CalloutVariant,
|
|
17
|
+
{ border: string; bg: string; badge: string; label: string }
|
|
18
|
+
> = {
|
|
19
|
+
info: {
|
|
20
|
+
border: 'var(--od-accent, #2563eb)',
|
|
21
|
+
bg: 'color-mix(in srgb, var(--od-accent, #2563eb) 7%, var(--od-bg, #ffffff))',
|
|
22
|
+
badge: 'var(--od-accent, #2563eb)',
|
|
23
|
+
label: 'NOTE',
|
|
24
|
+
},
|
|
25
|
+
tip: {
|
|
26
|
+
border: '#15803d',
|
|
27
|
+
bg: 'color-mix(in srgb, #15803d 7%, var(--od-bg, #ffffff))',
|
|
28
|
+
badge: '#15803d',
|
|
29
|
+
label: 'TIP',
|
|
30
|
+
},
|
|
31
|
+
warning: {
|
|
32
|
+
border: '#b45309',
|
|
33
|
+
bg: 'color-mix(in srgb, #d97706 8%, var(--od-bg, #ffffff))',
|
|
34
|
+
badge: '#b45309',
|
|
35
|
+
label: 'WARNING',
|
|
36
|
+
},
|
|
37
|
+
danger: {
|
|
38
|
+
border: '#b91c1c',
|
|
39
|
+
bg: 'color-mix(in srgb, #dc2626 7%, var(--od-bg, #ffffff))',
|
|
40
|
+
badge: '#b91c1c',
|
|
41
|
+
label: 'CRITICAL',
|
|
42
|
+
},
|
|
43
|
+
neutral: {
|
|
44
|
+
border: 'var(--od-muted, #6b7280)',
|
|
45
|
+
bg: 'color-mix(in srgb, var(--od-text, #16181d) 4%, var(--od-bg, #ffffff))',
|
|
46
|
+
badge: 'var(--od-muted, #6b7280)',
|
|
47
|
+
label: 'SUMMARY',
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* A structured admonition box (`info`, `tip`, `warning`, `danger`, `neutral`)
|
|
53
|
+
* rendered as a `<blockquote data-od-callout>` so Word and PDF exporters keep
|
|
54
|
+
* its panel border and shading intact.
|
|
55
|
+
*/
|
|
56
|
+
export function Callout({
|
|
57
|
+
variant = 'info',
|
|
58
|
+
title,
|
|
59
|
+
children,
|
|
60
|
+
style,
|
|
61
|
+
className,
|
|
62
|
+
[LOC_PROP]: loc,
|
|
63
|
+
}: CalloutProps) {
|
|
64
|
+
const palette = PALETTES[variant] ?? PALETTES.info;
|
|
65
|
+
|
|
66
|
+
return (
|
|
67
|
+
<blockquote
|
|
68
|
+
{...sourceAttrs(loc)}
|
|
69
|
+
data-od-callout={variant}
|
|
70
|
+
className={className}
|
|
71
|
+
style={{
|
|
72
|
+
margin: '4px 0 12px',
|
|
73
|
+
padding: '10px 14px',
|
|
74
|
+
borderLeft: `3px solid ${palette.border}`,
|
|
75
|
+
background: palette.bg,
|
|
76
|
+
borderRadius: '0 var(--od-radius, 4px) var(--od-radius, 4px) 0',
|
|
77
|
+
color: 'var(--od-text, #16181d)',
|
|
78
|
+
fontStyle: 'normal',
|
|
79
|
+
fontSize: '0.95em',
|
|
80
|
+
lineHeight: 1.55,
|
|
81
|
+
...style,
|
|
82
|
+
}}
|
|
83
|
+
>
|
|
84
|
+
{title !== undefined && (
|
|
85
|
+
<div
|
|
86
|
+
style={{
|
|
87
|
+
fontFamily: 'var(--od-font-heading, sans-serif)',
|
|
88
|
+
fontWeight: 650,
|
|
89
|
+
fontSize: '0.92em',
|
|
90
|
+
color: palette.badge,
|
|
91
|
+
marginBottom: 4,
|
|
92
|
+
letterSpacing: '0.01em',
|
|
93
|
+
}}
|
|
94
|
+
>
|
|
95
|
+
{typeof title === 'string' ? <span {...sourceAttrs(loc, 'title')}>{title}</span> : title}
|
|
96
|
+
</div>
|
|
97
|
+
)}
|
|
98
|
+
<div>{children}</div>
|
|
99
|
+
</blockquote>
|
|
100
|
+
);
|
|
101
|
+
}
|
|
@@ -114,26 +114,47 @@ export function Chart({
|
|
|
114
114
|
}}
|
|
115
115
|
className={caption ? undefined : className}
|
|
116
116
|
>
|
|
117
|
-
{chart.ticks.map((tick) =>
|
|
118
|
-
|
|
119
|
-
<
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
117
|
+
{chart.ticks.map((tick) =>
|
|
118
|
+
chart.horizontal ? (
|
|
119
|
+
<g key={tick.value}>
|
|
120
|
+
<line
|
|
121
|
+
x1={tick.at}
|
|
122
|
+
x2={tick.at}
|
|
123
|
+
y1={chart.plot.y}
|
|
124
|
+
y2={chart.plot.y + chart.plot.height}
|
|
125
|
+
style={{ stroke: RULE }}
|
|
126
|
+
strokeWidth={1}
|
|
127
|
+
/>
|
|
128
|
+
<text
|
|
129
|
+
x={tick.at}
|
|
130
|
+
y={chart.plot.y + chart.plot.height + CHART_FONT_SIZE * 1.4}
|
|
131
|
+
textAnchor="middle"
|
|
132
|
+
style={{ ...text, fill: MUTED }}
|
|
133
|
+
>
|
|
134
|
+
{tick.label}
|
|
135
|
+
</text>
|
|
136
|
+
</g>
|
|
137
|
+
) : (
|
|
138
|
+
<g key={tick.value}>
|
|
139
|
+
<line
|
|
140
|
+
x1={chart.plot.x}
|
|
141
|
+
x2={chart.plot.x + chart.plot.width}
|
|
142
|
+
y1={tick.at}
|
|
143
|
+
y2={tick.at}
|
|
144
|
+
style={{ stroke: RULE }}
|
|
145
|
+
strokeWidth={1}
|
|
146
|
+
/>
|
|
147
|
+
<text
|
|
148
|
+
x={chart.plot.x - 6}
|
|
149
|
+
y={tick.at + CHART_FONT_SIZE * 0.35}
|
|
150
|
+
textAnchor="end"
|
|
151
|
+
style={{ ...text, fill: MUTED }}
|
|
152
|
+
>
|
|
153
|
+
{tick.label}
|
|
154
|
+
</text>
|
|
155
|
+
</g>
|
|
156
|
+
),
|
|
157
|
+
)}
|
|
137
158
|
{chart.bars.map((bar) => (
|
|
138
159
|
<rect
|
|
139
160
|
key={`${bar.series}-${bar.x}-${bar.y}`}
|
|
@@ -144,18 +165,38 @@ export function Chart({
|
|
|
144
165
|
style={{ fill: colorOf(bar.series, colors) }}
|
|
145
166
|
/>
|
|
146
167
|
))}
|
|
147
|
-
{chart.baseline !== null &&
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
168
|
+
{chart.baseline !== null &&
|
|
169
|
+
chart.bars.length > 0 &&
|
|
170
|
+
(chart.horizontal ? (
|
|
171
|
+
<line
|
|
172
|
+
x1={chart.baseline}
|
|
173
|
+
x2={chart.baseline}
|
|
174
|
+
y1={chart.plot.y}
|
|
175
|
+
y2={chart.plot.y + chart.plot.height}
|
|
176
|
+
style={{ stroke: MUTED }}
|
|
177
|
+
strokeWidth={1}
|
|
178
|
+
/>
|
|
179
|
+
) : (
|
|
180
|
+
<line
|
|
181
|
+
x1={chart.plot.x}
|
|
182
|
+
x2={chart.plot.x + chart.plot.width}
|
|
183
|
+
y1={chart.baseline}
|
|
184
|
+
y2={chart.baseline}
|
|
185
|
+
style={{ stroke: MUTED }}
|
|
186
|
+
strokeWidth={1}
|
|
187
|
+
/>
|
|
188
|
+
))}
|
|
157
189
|
{chart.lines.map((line) => (
|
|
158
190
|
<g key={line.series} style={{ stroke: colorOf(line.series, colors) }}>
|
|
191
|
+
{line.areaPath && (
|
|
192
|
+
<path
|
|
193
|
+
d={line.areaPath}
|
|
194
|
+
stroke="none"
|
|
195
|
+
style={{
|
|
196
|
+
fill: `color-mix(in srgb, ${colorOf(line.series, colors)} 20%, transparent)`,
|
|
197
|
+
}}
|
|
198
|
+
/>
|
|
199
|
+
)}
|
|
159
200
|
<polyline
|
|
160
201
|
points={line.points.map((point) => `${point.x},${point.y}`).join(' ')}
|
|
161
202
|
fill="none"
|
|
@@ -184,10 +225,10 @@ export function Chart({
|
|
|
184
225
|
))}
|
|
185
226
|
{chart.categories.map((category) => (
|
|
186
227
|
<text
|
|
187
|
-
key={category.x}
|
|
228
|
+
key={`${category.x}-${category.y ?? 0}-${category.text}`}
|
|
188
229
|
x={category.x}
|
|
189
|
-
y={chart.plot.y + chart.plot.height + CHART_FONT_SIZE * 1.5}
|
|
190
|
-
textAnchor=
|
|
230
|
+
y={category.y ?? chart.plot.y + chart.plot.height + CHART_FONT_SIZE * 1.5}
|
|
231
|
+
textAnchor={chart.horizontal ? 'end' : 'middle'}
|
|
191
232
|
style={{ ...text, fill: MUTED }}
|
|
192
233
|
>
|
|
193
234
|
{category.text}
|
|
@@ -237,3 +278,120 @@ export function Chart({
|
|
|
237
278
|
</Figure>
|
|
238
279
|
);
|
|
239
280
|
}
|
|
281
|
+
|
|
282
|
+
export type SparklineProps = {
|
|
283
|
+
/** Numeric values to plot in order. */
|
|
284
|
+
data: number[];
|
|
285
|
+
/** Line with subtle fill, or compact inline bars. Defaults to 'line'. */
|
|
286
|
+
type?: 'line' | 'bar';
|
|
287
|
+
width?: number;
|
|
288
|
+
height?: number;
|
|
289
|
+
color?: string;
|
|
290
|
+
/** Highlight the final data point with a dot on line sparklines. Defaults to true. */
|
|
291
|
+
showEndDot?: boolean;
|
|
292
|
+
style?: CSSProperties;
|
|
293
|
+
className?: string;
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* A compact word-sized chart that sits inline inside a paragraph or table cell.
|
|
298
|
+
*/
|
|
299
|
+
export function Sparkline({
|
|
300
|
+
data,
|
|
301
|
+
type = 'line',
|
|
302
|
+
width = 76,
|
|
303
|
+
height = 20,
|
|
304
|
+
color = ACCENT,
|
|
305
|
+
showEndDot = true,
|
|
306
|
+
style,
|
|
307
|
+
className,
|
|
308
|
+
}: SparklineProps) {
|
|
309
|
+
const nums = data.filter((v) => typeof v === 'number' && Number.isFinite(v));
|
|
310
|
+
if (nums.length === 0) return null;
|
|
311
|
+
|
|
312
|
+
const min = Math.min(...nums);
|
|
313
|
+
const max = Math.max(...nums);
|
|
314
|
+
const span = max - min || 1;
|
|
315
|
+
const pad = 2.5;
|
|
316
|
+
const w = Math.max(4, width - pad * 2);
|
|
317
|
+
const h = Math.max(4, height - pad * 2);
|
|
318
|
+
|
|
319
|
+
if (type === 'bar') {
|
|
320
|
+
const baseMin = Math.min(0, min);
|
|
321
|
+
const baseMax = Math.max(0, max) || 1;
|
|
322
|
+
const baseSpan = baseMax - baseMin || 1;
|
|
323
|
+
const step = w / nums.length;
|
|
324
|
+
const barW = Math.max(1.5, step * 0.72);
|
|
325
|
+
return (
|
|
326
|
+
<svg
|
|
327
|
+
viewBox={`0 0 ${width} ${height}`}
|
|
328
|
+
width={width}
|
|
329
|
+
height={height}
|
|
330
|
+
aria-hidden="true"
|
|
331
|
+
className={className}
|
|
332
|
+
style={{
|
|
333
|
+
display: 'inline-block',
|
|
334
|
+
verticalAlign: 'middle',
|
|
335
|
+
overflow: 'visible',
|
|
336
|
+
...style,
|
|
337
|
+
}}
|
|
338
|
+
>
|
|
339
|
+
{nums.map((val, idx) => {
|
|
340
|
+
const bh = Math.max(1.5, (Math.abs(val) / baseSpan) * h);
|
|
341
|
+
const x = pad + idx * step + (step - barW) / 2;
|
|
342
|
+
const y = pad + h - bh;
|
|
343
|
+
return (
|
|
344
|
+
// biome-ignore lint/suspicious/noArrayIndexKey: index is data order
|
|
345
|
+
<rect key={idx} x={x} y={y} width={barW} height={bh} rx={1} style={{ fill: color }} />
|
|
346
|
+
);
|
|
347
|
+
})}
|
|
348
|
+
</svg>
|
|
349
|
+
);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
const pts = nums.map((val, idx) => {
|
|
353
|
+
const x = pad + (nums.length === 1 ? w / 2 : (idx / (nums.length - 1)) * w);
|
|
354
|
+
const y = pad + h - ((val - min) / span) * h;
|
|
355
|
+
return { x: Math.round(x * 10) / 10, y: Math.round(y * 10) / 10 };
|
|
356
|
+
});
|
|
357
|
+
const poly = pts.map((p) => `${p.x},${p.y}`).join(' ');
|
|
358
|
+
const first = pts[0];
|
|
359
|
+
const last = pts[pts.length - 1];
|
|
360
|
+
const area =
|
|
361
|
+
pts.length >= 2 && first && last
|
|
362
|
+
? `M${pts.map((p) => `${p.x},${p.y}`).join(' L')} L${last.x},${pad + h} L${first.x},${pad + h} Z`
|
|
363
|
+
: null;
|
|
364
|
+
|
|
365
|
+
return (
|
|
366
|
+
<svg
|
|
367
|
+
viewBox={`0 0 ${width} ${height}`}
|
|
368
|
+
width={width}
|
|
369
|
+
height={height}
|
|
370
|
+
aria-hidden="true"
|
|
371
|
+
className={className}
|
|
372
|
+
style={{
|
|
373
|
+
display: 'inline-block',
|
|
374
|
+
verticalAlign: 'middle',
|
|
375
|
+
overflow: 'visible',
|
|
376
|
+
...style,
|
|
377
|
+
}}
|
|
378
|
+
>
|
|
379
|
+
{area && (
|
|
380
|
+
<path
|
|
381
|
+
d={area}
|
|
382
|
+
stroke="none"
|
|
383
|
+
style={{ fill: `color-mix(in srgb, ${color} 18%, transparent)` }}
|
|
384
|
+
/>
|
|
385
|
+
)}
|
|
386
|
+
<polyline
|
|
387
|
+
points={poly}
|
|
388
|
+
fill="none"
|
|
389
|
+
strokeWidth={1.6}
|
|
390
|
+
strokeLinecap="round"
|
|
391
|
+
strokeLinejoin="round"
|
|
392
|
+
style={{ stroke: color }}
|
|
393
|
+
/>
|
|
394
|
+
{showEndDot && last && <circle cx={last.x} cy={last.y} r={2.2} style={{ fill: color }} />}
|
|
395
|
+
</svg>
|
|
396
|
+
);
|
|
397
|
+
}
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import type { CSSProperties, ReactNode } from 'react';
|
|
2
|
+
import { LOC_PROP, sourceAttrs } from '../lib/source-loc';
|
|
3
|
+
|
|
4
|
+
export type CodeBlockProps = {
|
|
5
|
+
code?: string;
|
|
6
|
+
children?: string;
|
|
7
|
+
lang?: string;
|
|
8
|
+
title?: ReactNode;
|
|
9
|
+
lineNumbers?: boolean;
|
|
10
|
+
/** 1-based line numbers to highlight. */
|
|
11
|
+
highlightLines?: number[];
|
|
12
|
+
style?: CSSProperties;
|
|
13
|
+
className?: string;
|
|
14
|
+
[LOC_PROP]?: string;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
type TokenKind = 'plain' | 'keyword' | 'string' | 'comment' | 'number' | 'type';
|
|
18
|
+
|
|
19
|
+
type Token = { kind: TokenKind; text: string };
|
|
20
|
+
|
|
21
|
+
const KEYWORDS = new Set([
|
|
22
|
+
'import',
|
|
23
|
+
'from',
|
|
24
|
+
'export',
|
|
25
|
+
'default',
|
|
26
|
+
'const',
|
|
27
|
+
'let',
|
|
28
|
+
'var',
|
|
29
|
+
'function',
|
|
30
|
+
'return',
|
|
31
|
+
'if',
|
|
32
|
+
'else',
|
|
33
|
+
'for',
|
|
34
|
+
'while',
|
|
35
|
+
'switch',
|
|
36
|
+
'case',
|
|
37
|
+
'break',
|
|
38
|
+
'continue',
|
|
39
|
+
'async',
|
|
40
|
+
'await',
|
|
41
|
+
'try',
|
|
42
|
+
'catch',
|
|
43
|
+
'finally',
|
|
44
|
+
'throw',
|
|
45
|
+
'new',
|
|
46
|
+
'class',
|
|
47
|
+
'extends',
|
|
48
|
+
'implements',
|
|
49
|
+
'interface',
|
|
50
|
+
'type',
|
|
51
|
+
'satisfies',
|
|
52
|
+
'as',
|
|
53
|
+
'true',
|
|
54
|
+
'false',
|
|
55
|
+
'null',
|
|
56
|
+
'undefined',
|
|
57
|
+
'def',
|
|
58
|
+
'elif',
|
|
59
|
+
'in',
|
|
60
|
+
'and',
|
|
61
|
+
'or',
|
|
62
|
+
'not',
|
|
63
|
+
'SELECT',
|
|
64
|
+
'FROM',
|
|
65
|
+
'WHERE',
|
|
66
|
+
'GROUP',
|
|
67
|
+
'BY',
|
|
68
|
+
'ORDER',
|
|
69
|
+
'LIMIT',
|
|
70
|
+
'JOIN',
|
|
71
|
+
'ON',
|
|
72
|
+
]);
|
|
73
|
+
|
|
74
|
+
const TOKEN_RE =
|
|
75
|
+
/(\/\/[^\n]*|#[^\n]*|\/\*[\s\S]*?\*\/)|("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|`(?:\\.|[^`\\])*`)|\b(\d+(?:\.\d+)?)\b|\b([A-Za-z_][A-Za-z0-9_]*)\b/g;
|
|
76
|
+
|
|
77
|
+
function tokenizeLine(line: string): Token[] {
|
|
78
|
+
const out: Token[] = [];
|
|
79
|
+
let cursor = 0;
|
|
80
|
+
for (const match of line.matchAll(TOKEN_RE)) {
|
|
81
|
+
const idx = match.index ?? 0;
|
|
82
|
+
if (idx > cursor) {
|
|
83
|
+
out.push({ kind: 'plain', text: line.slice(cursor, idx) });
|
|
84
|
+
}
|
|
85
|
+
const [full, comment, str, num, ident] = match;
|
|
86
|
+
if (comment !== undefined) {
|
|
87
|
+
out.push({ kind: 'comment', text: full });
|
|
88
|
+
} else if (str !== undefined) {
|
|
89
|
+
out.push({ kind: 'string', text: full });
|
|
90
|
+
} else if (num !== undefined) {
|
|
91
|
+
out.push({ kind: 'number', text: full });
|
|
92
|
+
} else if (ident !== undefined) {
|
|
93
|
+
if (KEYWORDS.has(ident)) {
|
|
94
|
+
out.push({ kind: 'keyword', text: full });
|
|
95
|
+
} else if (/^[A-Z]/.test(ident)) {
|
|
96
|
+
out.push({ kind: 'type', text: full });
|
|
97
|
+
} else {
|
|
98
|
+
out.push({ kind: 'plain', text: full });
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
cursor = idx + full.length;
|
|
102
|
+
}
|
|
103
|
+
if (cursor < line.length) {
|
|
104
|
+
out.push({ kind: 'plain', text: line.slice(cursor) });
|
|
105
|
+
}
|
|
106
|
+
return out;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function tokenStyle(kind: TokenKind): CSSProperties | undefined {
|
|
110
|
+
switch (kind) {
|
|
111
|
+
case 'keyword':
|
|
112
|
+
return { color: 'var(--od-accent, #2563eb)', fontWeight: 600 };
|
|
113
|
+
case 'string':
|
|
114
|
+
return { color: '#0f766e' };
|
|
115
|
+
case 'comment':
|
|
116
|
+
return { color: 'var(--od-muted, #6b7280)', fontStyle: 'italic' };
|
|
117
|
+
case 'number':
|
|
118
|
+
return { color: '#b45309' };
|
|
119
|
+
case 'type':
|
|
120
|
+
return {
|
|
121
|
+
color: 'color-mix(in srgb, var(--od-accent, #2563eb) 75%, var(--od-text, #16181d))',
|
|
122
|
+
fontWeight: 500,
|
|
123
|
+
};
|
|
124
|
+
default:
|
|
125
|
+
return undefined;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Print-ready syntax-highlighted code block with optional header bar, line
|
|
131
|
+
* numbers, and highlighted lines. Uses `--od-*` variables so it harmonizes with
|
|
132
|
+
* the document's design system and exports cleanly to PDF, HTML, and Word.
|
|
133
|
+
*/
|
|
134
|
+
export function CodeBlock({
|
|
135
|
+
code,
|
|
136
|
+
children,
|
|
137
|
+
lang,
|
|
138
|
+
title,
|
|
139
|
+
lineNumbers = false,
|
|
140
|
+
highlightLines,
|
|
141
|
+
style,
|
|
142
|
+
className,
|
|
143
|
+
[LOC_PROP]: loc,
|
|
144
|
+
}: CodeBlockProps) {
|
|
145
|
+
const raw = (code ?? children ?? '').replace(/^\n+|\s+$/g, '');
|
|
146
|
+
const lines = raw.split('\n');
|
|
147
|
+
const highlighted = new Set(highlightLines ?? []);
|
|
148
|
+
|
|
149
|
+
return (
|
|
150
|
+
<div
|
|
151
|
+
{...sourceAttrs(loc)}
|
|
152
|
+
className={className}
|
|
153
|
+
style={{
|
|
154
|
+
border: '1px solid var(--od-rule, #e5e7eb)',
|
|
155
|
+
borderRadius: 'var(--od-radius, 6px)',
|
|
156
|
+
background: 'color-mix(in srgb, var(--od-text, #16181d) 3%, var(--od-bg, #ffffff))',
|
|
157
|
+
overflow: 'hidden',
|
|
158
|
+
margin: '0 0 12px',
|
|
159
|
+
...style,
|
|
160
|
+
}}
|
|
161
|
+
>
|
|
162
|
+
{(title || lang) && (
|
|
163
|
+
<div
|
|
164
|
+
style={{
|
|
165
|
+
display: 'flex',
|
|
166
|
+
alignItems: 'center',
|
|
167
|
+
justifyContent: 'space-between',
|
|
168
|
+
padding: '5px 12px',
|
|
169
|
+
borderBottom: '1px solid var(--od-rule, #e5e7eb)',
|
|
170
|
+
background: 'color-mix(in srgb, var(--od-text, #16181d) 5%, var(--od-bg, #ffffff))',
|
|
171
|
+
fontFamily: 'var(--od-font-mono, monospace)',
|
|
172
|
+
fontSize: 10.5,
|
|
173
|
+
color: 'var(--od-muted, #6b7280)',
|
|
174
|
+
}}
|
|
175
|
+
>
|
|
176
|
+
<span style={{ fontWeight: 600, color: 'var(--od-text, #16181d)' }}>{title ?? ''}</span>
|
|
177
|
+
{lang && (
|
|
178
|
+
<span style={{ textTransform: 'uppercase', letterSpacing: '0.05em', fontSize: 9.5 }}>
|
|
179
|
+
{lang}
|
|
180
|
+
</span>
|
|
181
|
+
)}
|
|
182
|
+
</div>
|
|
183
|
+
)}
|
|
184
|
+
<pre
|
|
185
|
+
style={{
|
|
186
|
+
margin: 0,
|
|
187
|
+
padding: '10px 12px',
|
|
188
|
+
fontFamily: 'var(--od-font-mono, monospace)',
|
|
189
|
+
fontSize: 11,
|
|
190
|
+
lineHeight: 1.55,
|
|
191
|
+
color: 'var(--od-text, #16181d)',
|
|
192
|
+
whiteSpace: 'pre-wrap',
|
|
193
|
+
overflowWrap: 'anywhere',
|
|
194
|
+
}}
|
|
195
|
+
>
|
|
196
|
+
{lines.map((line, index) => {
|
|
197
|
+
const lineNo = index + 1;
|
|
198
|
+
const isHighlighted = highlighted.has(lineNo);
|
|
199
|
+
const tokens = tokenizeLine(line);
|
|
200
|
+
return (
|
|
201
|
+
<div
|
|
202
|
+
// biome-ignore lint/suspicious/noArrayIndexKey: source line order
|
|
203
|
+
key={index}
|
|
204
|
+
style={{
|
|
205
|
+
display: 'flex',
|
|
206
|
+
...(isHighlighted
|
|
207
|
+
? {
|
|
208
|
+
background: 'color-mix(in srgb, var(--od-accent, #2563eb) 10%, transparent)',
|
|
209
|
+
margin: '0 -12px',
|
|
210
|
+
padding: '0 12px',
|
|
211
|
+
borderLeft: '2.5px solid var(--od-accent, #2563eb)',
|
|
212
|
+
}
|
|
213
|
+
: {}),
|
|
214
|
+
}}
|
|
215
|
+
>
|
|
216
|
+
{lineNumbers && (
|
|
217
|
+
<span
|
|
218
|
+
aria-hidden="true"
|
|
219
|
+
style={{
|
|
220
|
+
flex: 'none',
|
|
221
|
+
width: 24,
|
|
222
|
+
paddingRight: 8,
|
|
223
|
+
textAlign: 'right',
|
|
224
|
+
color: 'var(--od-muted, #6b7280)',
|
|
225
|
+
userSelect: 'none',
|
|
226
|
+
fontVariantNumeric: 'tabular-nums',
|
|
227
|
+
opacity: 0.65,
|
|
228
|
+
}}
|
|
229
|
+
>
|
|
230
|
+
{lineNo}
|
|
231
|
+
</span>
|
|
232
|
+
)}
|
|
233
|
+
<span style={{ flex: 1 }}>
|
|
234
|
+
{tokens.length === 0
|
|
235
|
+
? '\u00a0'
|
|
236
|
+
: tokens.map((tok, ti) => (
|
|
237
|
+
// biome-ignore lint/suspicious/noArrayIndexKey: token sequence
|
|
238
|
+
<span key={ti} style={tokenStyle(tok.kind)}>
|
|
239
|
+
{tok.text}
|
|
240
|
+
</span>
|
|
241
|
+
))}
|
|
242
|
+
</span>
|
|
243
|
+
</div>
|
|
244
|
+
);
|
|
245
|
+
})}
|
|
246
|
+
</pre>
|
|
247
|
+
</div>
|
|
248
|
+
);
|
|
249
|
+
}
|