@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
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { CSSProperties, ReactNode } from 'react';
|
|
2
2
|
import { LOC_PROP, sourceAttrs } from '../lib/source-loc';
|
|
3
|
+
import { useTableSlice } from './flow-page';
|
|
3
4
|
import { Figure } from './numbering';
|
|
4
5
|
|
|
5
6
|
export type DataAlign = 'left' | 'center' | 'right';
|
|
@@ -9,8 +10,18 @@ export type DataFormat =
|
|
|
9
10
|
| 'number'
|
|
10
11
|
| 'integer'
|
|
11
12
|
| 'percent'
|
|
13
|
+
| 'currency'
|
|
14
|
+
| 'delta'
|
|
12
15
|
| ((value: unknown, row: Record<string, unknown>) => ReactNode);
|
|
13
16
|
|
|
17
|
+
export type DataSummaryOp =
|
|
18
|
+
| 'sum'
|
|
19
|
+
| 'avg'
|
|
20
|
+
| 'min'
|
|
21
|
+
| 'max'
|
|
22
|
+
| 'count'
|
|
23
|
+
| ((values: number[]) => ReactNode);
|
|
24
|
+
|
|
14
25
|
export type DataColumn = {
|
|
15
26
|
key: string;
|
|
16
27
|
label?: ReactNode;
|
|
@@ -18,6 +29,10 @@ export type DataColumn = {
|
|
|
18
29
|
format?: DataFormat;
|
|
19
30
|
/** Column width. A number is px; `tableLayout: fixed` means these are honoured. */
|
|
20
31
|
width?: number | string;
|
|
32
|
+
/** Draws a proportional horizontal bar behind numeric cells in this column. */
|
|
33
|
+
dataBar?: boolean;
|
|
34
|
+
/** Aggregation for this column when `summaryRow` is enabled on the table. */
|
|
35
|
+
summary?: DataSummaryOp | ReactNode;
|
|
21
36
|
};
|
|
22
37
|
|
|
23
38
|
export type DataTableProps = {
|
|
@@ -33,6 +48,12 @@ export type DataTableProps = {
|
|
|
33
48
|
limit?: number;
|
|
34
49
|
/** Tighter rows, for a dense appendix table. */
|
|
35
50
|
compact?: boolean;
|
|
51
|
+
/** Allows `flow()` to split this table across pages and repeat the `<thead>` on each sheet. */
|
|
52
|
+
splittable?: boolean;
|
|
53
|
+
/** Appends a summary footer row. Pass `true` to use column `summary` ops, or a record of values/ops. */
|
|
54
|
+
summaryRow?: boolean | Record<string, DataSummaryOp | ReactNode>;
|
|
55
|
+
/** Label printed in the first column of the summary row when not overridden. Defaults to 'Total'. */
|
|
56
|
+
summaryLabel?: ReactNode;
|
|
36
57
|
/** What an empty cell prints as. Defaults to an em dash. */
|
|
37
58
|
emptyValue?: ReactNode;
|
|
38
59
|
style?: CSSProperties;
|
|
@@ -74,6 +95,14 @@ function formatValue(
|
|
|
74
95
|
if (format === 'integer') return Math.round(value).toLocaleString();
|
|
75
96
|
if (format === 'percent')
|
|
76
97
|
return `${(value * 100).toLocaleString(undefined, { maximumFractionDigits: 1 })}%`;
|
|
98
|
+
if (format === 'currency')
|
|
99
|
+
return `$${value.toLocaleString(undefined, { minimumFractionDigits: 0, maximumFractionDigits: 2 })}`;
|
|
100
|
+
if (format === 'delta') {
|
|
101
|
+
const sign = value > 0 ? '+' : '';
|
|
102
|
+
const text = `${sign}${value.toLocaleString(undefined, { maximumFractionDigits: 2 })}`;
|
|
103
|
+
const color = value > 0 ? '#15803d' : value < 0 ? '#b91c1c' : 'var(--od-muted)';
|
|
104
|
+
return <span style={{ color, fontWeight: 500 }}>{text}</span>;
|
|
105
|
+
}
|
|
77
106
|
if (format === 'text') return String(value);
|
|
78
107
|
// Grouping separators are what make a column of figures readable; the
|
|
79
108
|
// fraction digits follow the data rather than padding every integer.
|
|
@@ -83,6 +112,26 @@ function formatValue(
|
|
|
83
112
|
return String(value);
|
|
84
113
|
}
|
|
85
114
|
|
|
115
|
+
function computeSummary(
|
|
116
|
+
op: DataSummaryOp | ReactNode,
|
|
117
|
+
values: number[],
|
|
118
|
+
format: DataFormat | undefined,
|
|
119
|
+
emptyValue: ReactNode,
|
|
120
|
+
): ReactNode {
|
|
121
|
+
if (typeof op === 'function') return op(values);
|
|
122
|
+
if (op === 'sum' || op === 'avg' || op === 'min' || op === 'max' || op === 'count') {
|
|
123
|
+
if (values.length === 0) return emptyValue;
|
|
124
|
+
let num = 0;
|
|
125
|
+
if (op === 'count') num = values.length;
|
|
126
|
+
else if (op === 'sum') num = values.reduce((a, b) => a + b, 0);
|
|
127
|
+
else if (op === 'avg') num = values.reduce((a, b) => a + b, 0) / values.length;
|
|
128
|
+
else if (op === 'min') num = Math.min(...values);
|
|
129
|
+
else if (op === 'max') num = Math.max(...values);
|
|
130
|
+
return formatValue(num, op === 'count' ? 'integer' : format, {}, emptyValue);
|
|
131
|
+
}
|
|
132
|
+
return op;
|
|
133
|
+
}
|
|
134
|
+
|
|
86
135
|
/**
|
|
87
136
|
* A print-shaped table from data, so the numbers in a report come from a file
|
|
88
137
|
* instead of being retyped into JSX. Alignment and grouping follow the column's
|
|
@@ -95,21 +144,42 @@ export function DataTable({
|
|
|
95
144
|
id,
|
|
96
145
|
limit,
|
|
97
146
|
compact = false,
|
|
147
|
+
splittable = false,
|
|
148
|
+
summaryRow,
|
|
149
|
+
summaryLabel = 'Total',
|
|
98
150
|
emptyValue = '—',
|
|
99
151
|
style,
|
|
100
152
|
className,
|
|
101
153
|
[LOC_PROP]: loc,
|
|
102
154
|
}: DataTableProps) {
|
|
155
|
+
const slice = useTableSlice();
|
|
103
156
|
const resolved: DataColumn[] = (columns ?? Object.keys(rows[0] ?? {})).map(normalizeColumn);
|
|
104
157
|
|
|
105
|
-
const
|
|
106
|
-
const hidden = rows.length -
|
|
158
|
+
const limited = limit !== undefined ? rows.slice(0, limit) : rows;
|
|
159
|
+
const hidden = rows.length - limited.length;
|
|
160
|
+
|
|
161
|
+
const isContinued = splittable && slice !== null && slice.rowStart > 0;
|
|
162
|
+
const isLastSlice = !splittable || slice === null || slice.rowEnd >= limited.length;
|
|
163
|
+
const shown =
|
|
164
|
+
splittable && slice !== null ? limited.slice(slice.rowStart, slice.rowEnd) : limited;
|
|
165
|
+
|
|
166
|
+
const maxByCol = new Map<string, number>();
|
|
167
|
+
for (const col of resolved) {
|
|
168
|
+
if (!col.dataBar) continue;
|
|
169
|
+
let max = 0;
|
|
170
|
+
for (const row of limited) {
|
|
171
|
+
const v = row[col.key];
|
|
172
|
+
if (isNumber(v) && Math.abs(v) > max) max = Math.abs(v);
|
|
173
|
+
}
|
|
174
|
+
if (max > 0) maxByCol.set(col.key, max);
|
|
175
|
+
}
|
|
107
176
|
|
|
108
177
|
const cellPadding = compact ? '4px 8px' : '7px 8px';
|
|
109
178
|
|
|
110
179
|
const table = (
|
|
111
180
|
<table
|
|
112
181
|
{...(caption ? {} : sourceAttrs(loc))}
|
|
182
|
+
{...(splittable ? { 'data-od-splittable': '' } : {})}
|
|
113
183
|
className={className}
|
|
114
184
|
style={{
|
|
115
185
|
width: '100%',
|
|
@@ -158,6 +228,11 @@ export function DataTable({
|
|
|
158
228
|
<tr key={rowIndex}>
|
|
159
229
|
{resolved.map((column) => {
|
|
160
230
|
const align = column.align ?? inferAlign(rows, column.key);
|
|
231
|
+
const val = row[column.key];
|
|
232
|
+
const maxBar = maxByCol.get(column.key);
|
|
233
|
+
const barRatio =
|
|
234
|
+
maxBar && isNumber(val) ? Math.min(1, Math.max(0, Math.abs(val) / maxBar)) : 0;
|
|
235
|
+
|
|
161
236
|
return (
|
|
162
237
|
<td
|
|
163
238
|
key={column.key}
|
|
@@ -168,29 +243,78 @@ export function DataTable({
|
|
|
168
243
|
borderBottom: '1px solid var(--od-rule)',
|
|
169
244
|
fontVariantNumeric: align === 'right' ? 'tabular-nums' : undefined,
|
|
170
245
|
overflowWrap: 'anywhere',
|
|
246
|
+
...(barRatio > 0
|
|
247
|
+
? {
|
|
248
|
+
backgroundImage: `linear-gradient(to right, color-mix(in srgb, var(--od-accent, #2563eb) 14%, transparent) ${(barRatio * 100).toFixed(1)}%, transparent ${(barRatio * 100).toFixed(1)}%)`,
|
|
249
|
+
}
|
|
250
|
+
: {}),
|
|
171
251
|
}}
|
|
172
252
|
>
|
|
173
|
-
{formatValue(
|
|
253
|
+
{formatValue(val, column.format, row, emptyValue)}
|
|
174
254
|
</td>
|
|
175
255
|
);
|
|
176
256
|
})}
|
|
177
257
|
</tr>
|
|
178
258
|
))}
|
|
179
259
|
</tbody>
|
|
180
|
-
{hidden > 0 && (
|
|
260
|
+
{isLastSlice && (summaryRow || hidden > 0) && (
|
|
181
261
|
<tfoot>
|
|
182
|
-
|
|
183
|
-
<
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
262
|
+
{summaryRow && (
|
|
263
|
+
<tr>
|
|
264
|
+
{resolved.map((column, colIndex) => {
|
|
265
|
+
const align = column.align ?? inferAlign(rows, column.key);
|
|
266
|
+
const explicit =
|
|
267
|
+
typeof summaryRow === 'object' ? summaryRow[column.key] : column.summary;
|
|
268
|
+
let content: ReactNode = null;
|
|
269
|
+
if (explicit !== undefined) {
|
|
270
|
+
if (explicit === 'count') {
|
|
271
|
+
const nonNull = limited.filter(
|
|
272
|
+
(r) =>
|
|
273
|
+
r[column.key] !== null &&
|
|
274
|
+
r[column.key] !== undefined &&
|
|
275
|
+
r[column.key] !== '',
|
|
276
|
+
).length;
|
|
277
|
+
content = formatValue(nonNull, 'integer', {}, emptyValue);
|
|
278
|
+
} else {
|
|
279
|
+
const nums = limited.map((r) => r[column.key]).filter(isNumber);
|
|
280
|
+
content = computeSummary(explicit, nums, column.format, emptyValue);
|
|
281
|
+
}
|
|
282
|
+
} else if (colIndex === 0) {
|
|
283
|
+
content = summaryLabel;
|
|
284
|
+
}
|
|
285
|
+
return (
|
|
286
|
+
<td
|
|
287
|
+
key={column.key}
|
|
288
|
+
style={{
|
|
289
|
+
textAlign: align,
|
|
290
|
+
fontSize: compact ? 11 : 12,
|
|
291
|
+
fontWeight: 600,
|
|
292
|
+
padding: cellPadding,
|
|
293
|
+
borderTop: '1.5px solid var(--od-text)',
|
|
294
|
+
borderBottom: '1px solid var(--od-rule)',
|
|
295
|
+
fontVariantNumeric: align === 'right' ? 'tabular-nums' : undefined,
|
|
296
|
+
}}
|
|
297
|
+
>
|
|
298
|
+
{content}
|
|
299
|
+
</td>
|
|
300
|
+
);
|
|
301
|
+
})}
|
|
302
|
+
</tr>
|
|
303
|
+
)}
|
|
304
|
+
{hidden > 0 && (
|
|
305
|
+
<tr>
|
|
306
|
+
<td
|
|
307
|
+
colSpan={resolved.length}
|
|
308
|
+
style={{
|
|
309
|
+
fontSize: 'var(--od-size-caption)',
|
|
310
|
+
color: 'var(--od-muted)',
|
|
311
|
+
padding: '6px 8px 0',
|
|
312
|
+
}}
|
|
313
|
+
>
|
|
314
|
+
{hidden} more row{hidden === 1 ? '' : 's'} not shown
|
|
315
|
+
</td>
|
|
316
|
+
</tr>
|
|
317
|
+
)}
|
|
194
318
|
</tfoot>
|
|
195
319
|
)}
|
|
196
320
|
</table>
|
|
@@ -198,11 +322,15 @@ export function DataTable({
|
|
|
198
322
|
|
|
199
323
|
if (!caption) return table;
|
|
200
324
|
|
|
325
|
+
const effectiveCaption =
|
|
326
|
+
isContinued && typeof caption === 'string' ? `${caption} (cont.)` : caption;
|
|
327
|
+
|
|
201
328
|
return (
|
|
202
329
|
<Figure
|
|
203
330
|
kind="table"
|
|
204
|
-
caption={
|
|
205
|
-
{...(id !== undefined ? { id } : {})}
|
|
331
|
+
caption={effectiveCaption}
|
|
332
|
+
{...(id !== undefined && !isContinued ? { id } : {})}
|
|
333
|
+
{...(isContinued ? { numbered: false } : {})}
|
|
206
334
|
{...sourceAttrs(loc)}
|
|
207
335
|
>
|
|
208
336
|
{table}
|
|
@@ -1,11 +1,27 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type CSSProperties, createContext, type ReactNode, useContext } from 'react';
|
|
2
2
|
import type { DesignSystem } from '../lib/design';
|
|
3
|
-
import type { FlowSection } from '../lib/flow';
|
|
3
|
+
import type { BlockSlice, FlowSection } from '../lib/flow';
|
|
4
4
|
import type { ExtractedNote } from '../lib/footnotes';
|
|
5
5
|
import { Footnotes } from './footnote';
|
|
6
6
|
|
|
7
7
|
export const FLOW_BLOCK_ATTR = 'data-od-flow-block';
|
|
8
8
|
|
|
9
|
+
export type TableSliceRange = { rowStart: number; rowEnd: number };
|
|
10
|
+
|
|
11
|
+
const SLICE_CONTEXT_KEY = '__open_doc_table_slice_context__';
|
|
12
|
+
type GlobalWithSlice = typeof globalThis & {
|
|
13
|
+
[SLICE_CONTEXT_KEY]?: ReturnType<typeof createContext<TableSliceRange | null>>;
|
|
14
|
+
};
|
|
15
|
+
const g = globalThis as GlobalWithSlice;
|
|
16
|
+
if (!g[SLICE_CONTEXT_KEY]) {
|
|
17
|
+
g[SLICE_CONTEXT_KEY] = createContext<TableSliceRange | null>(null);
|
|
18
|
+
}
|
|
19
|
+
const TableSliceContext = g[SLICE_CONTEXT_KEY];
|
|
20
|
+
|
|
21
|
+
export function useTableSlice(): TableSliceRange | null {
|
|
22
|
+
return useContext(TableSliceContext);
|
|
23
|
+
}
|
|
24
|
+
|
|
9
25
|
/**
|
|
10
26
|
* The page shell a flow section renders into. The framework owns the margin and
|
|
11
27
|
* base typography here — that is the trade for not hand-splitting pages — while
|
|
@@ -33,20 +49,30 @@ export function flowShellStyle(design: DesignSystem | undefined, padding?: numbe
|
|
|
33
49
|
};
|
|
34
50
|
}
|
|
35
51
|
|
|
36
|
-
export function FlowBlock({
|
|
37
|
-
|
|
52
|
+
export function FlowBlock({
|
|
53
|
+
slice,
|
|
54
|
+
children,
|
|
55
|
+
}: {
|
|
56
|
+
slice?: TableSliceRange | null;
|
|
57
|
+
children?: ReactNode;
|
|
58
|
+
}) {
|
|
59
|
+
const inner = <div {...{ [FLOW_BLOCK_ATTR]: '' }}>{children}</div>;
|
|
60
|
+
if (!slice) return inner;
|
|
61
|
+
return <TableSliceContext.Provider value={slice}>{inner}</TableSliceContext.Provider>;
|
|
38
62
|
}
|
|
39
63
|
|
|
40
64
|
export function FlowPage({
|
|
41
65
|
section,
|
|
42
66
|
design,
|
|
43
67
|
blockIndices,
|
|
68
|
+
blockSlices,
|
|
44
69
|
blocks,
|
|
45
70
|
notes,
|
|
46
71
|
}: {
|
|
47
72
|
section: FlowSection;
|
|
48
73
|
design: DesignSystem | undefined;
|
|
49
74
|
blockIndices: number[];
|
|
75
|
+
blockSlices?: BlockSlice[];
|
|
50
76
|
/** Blocks with footnotes already lifted out; falls back to the authored ones. */
|
|
51
77
|
blocks?: ReactNode[];
|
|
52
78
|
notes?: ExtractedNote[];
|
|
@@ -54,13 +80,36 @@ export function FlowPage({
|
|
|
54
80
|
const Header = section.header;
|
|
55
81
|
const Footer = section.footer;
|
|
56
82
|
const source = blocks ?? section.blocks;
|
|
83
|
+
const items: BlockSlice[] = blockSlices ?? blockIndices.map((index) => ({ index }));
|
|
84
|
+
const cols = section.columns && section.columns > 1 ? section.columns : 1;
|
|
85
|
+
|
|
57
86
|
return (
|
|
58
87
|
<div style={flowShellStyle(design, section.padding)}>
|
|
59
88
|
{Header && <Header />}
|
|
60
|
-
<div
|
|
61
|
-
{
|
|
62
|
-
|
|
63
|
-
|
|
89
|
+
<div
|
|
90
|
+
style={{
|
|
91
|
+
flex: cols > 1 ? 'none' : 1,
|
|
92
|
+
minHeight: 0,
|
|
93
|
+
...(cols > 1
|
|
94
|
+
? {
|
|
95
|
+
columnCount: cols,
|
|
96
|
+
columnGap: section.columnGap ?? 24,
|
|
97
|
+
columnFill: 'balance',
|
|
98
|
+
}
|
|
99
|
+
: {}),
|
|
100
|
+
}}
|
|
101
|
+
>
|
|
102
|
+
{items.map((item) => {
|
|
103
|
+
const slice =
|
|
104
|
+
item.rowStart !== undefined && item.rowEnd !== undefined
|
|
105
|
+
? { rowStart: item.rowStart, rowEnd: item.rowEnd }
|
|
106
|
+
: null;
|
|
107
|
+
return (
|
|
108
|
+
<FlowBlock key={`${item.index}-${item.rowStart ?? 0}`} slice={slice}>
|
|
109
|
+
{source[item.index]}
|
|
110
|
+
</FlowBlock>
|
|
111
|
+
);
|
|
112
|
+
})}
|
|
64
113
|
</div>
|
|
65
114
|
{notes && notes.length > 0 && <Footnotes notes={notes} />}
|
|
66
115
|
{Footer && <Footer />}
|
|
@@ -27,6 +27,8 @@ export type FigureProps = {
|
|
|
27
27
|
captionPosition?: 'above' | 'below';
|
|
28
28
|
/** Plain-text caption for the list of figures, when `caption` carries markup. */
|
|
29
29
|
captionText?: string;
|
|
30
|
+
/** When false, prints the caption without registering a new label number (e.g. continued table slices). */
|
|
31
|
+
numbered?: boolean;
|
|
30
32
|
style?: CSSProperties;
|
|
31
33
|
captionStyle?: CSSProperties;
|
|
32
34
|
className?: string;
|
|
@@ -49,6 +51,7 @@ export function Figure({
|
|
|
49
51
|
kind = 'figure',
|
|
50
52
|
captionPosition,
|
|
51
53
|
captionText,
|
|
54
|
+
numbered = true,
|
|
52
55
|
style,
|
|
53
56
|
captionStyle,
|
|
54
57
|
className,
|
|
@@ -75,28 +78,35 @@ export function Figure({
|
|
|
75
78
|
...captionStyle,
|
|
76
79
|
}}
|
|
77
80
|
>
|
|
78
|
-
|
|
79
|
-
{
|
|
80
|
-
|
|
81
|
+
{numbered && (
|
|
82
|
+
<span style={{ fontWeight: 600, color: 'var(--od-text)' }}>
|
|
83
|
+
{nameFor(kind, vocabulary)} {entry?.number ?? ''}
|
|
84
|
+
</span>
|
|
85
|
+
)}
|
|
81
86
|
{typeof caption === 'string' || typeof caption === 'number' ? (
|
|
82
87
|
<span>
|
|
83
|
-
{' — '}
|
|
88
|
+
{numbered ? ' — ' : ''}
|
|
84
89
|
<span {...sourceAttrs(loc, 'caption')}>{caption}</span>
|
|
85
90
|
</span>
|
|
86
91
|
) : caption ? (
|
|
87
|
-
<span>
|
|
92
|
+
<span>
|
|
93
|
+
{numbered ? ' — ' : ''}
|
|
94
|
+
{caption}
|
|
95
|
+
</span>
|
|
88
96
|
) : null}
|
|
89
97
|
</figcaption>
|
|
90
98
|
) : null;
|
|
91
99
|
|
|
92
100
|
return (
|
|
93
101
|
<figure
|
|
94
|
-
{...
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
102
|
+
{...(numbered
|
|
103
|
+
? {
|
|
104
|
+
[LABEL_ATTR]: kind,
|
|
105
|
+
[LABEL_ID_ATTR]: labelId,
|
|
106
|
+
[LABEL_TEXT_ATTR]: text,
|
|
107
|
+
...sourceAttrs(loc),
|
|
108
|
+
}
|
|
109
|
+
: sourceAttrs(loc))}
|
|
100
110
|
className={className}
|
|
101
111
|
style={{ margin: '0 0 16px', ...style }}
|
|
102
112
|
>
|
|
@@ -143,7 +153,10 @@ export function Ref({ to, showPage = 'auto', style, className }: RefProps) {
|
|
|
143
153
|
);
|
|
144
154
|
}
|
|
145
155
|
|
|
146
|
-
const name =
|
|
156
|
+
const name =
|
|
157
|
+
entry.kind === 'footnote' || entry.kind === 'citation'
|
|
158
|
+
? ''
|
|
159
|
+
: `${nameFor(entry.kind, vocabulary)} `;
|
|
147
160
|
const withPage = showPage === 'auto' ? entry.page !== here : showPage;
|
|
148
161
|
|
|
149
162
|
return (
|
|
@@ -219,3 +232,204 @@ export function ListOfFigures(props: Omit<ListOfProps, 'kind'>) {
|
|
|
219
232
|
export function ListOfTables(props: Omit<ListOfProps, 'kind'>) {
|
|
220
233
|
return <ListOf {...props} kind="table" />;
|
|
221
234
|
}
|
|
235
|
+
|
|
236
|
+
export type CiteProps = {
|
|
237
|
+
/** Stable citation key (e.g. 'rfc9110' or 'q3-audit'). */
|
|
238
|
+
id: string;
|
|
239
|
+
/** Citation details printed in `<Bibliography />`. Needs only to be supplied on one `<Cite>` for that id, or in `<Bibliography items={...} />`. */
|
|
240
|
+
source?: string;
|
|
241
|
+
style?: CSSProperties;
|
|
242
|
+
className?: string;
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* An inline bibliographic citation (`[1]`), numbered in order of first
|
|
247
|
+
* appearance across the rendered pages. Multiple `<Cite id="x" />` calls share
|
|
248
|
+
* the same number.
|
|
249
|
+
*/
|
|
250
|
+
export function Cite({ id, source, style, className }: CiteProps) {
|
|
251
|
+
const entry = useDocLabel(id, 'citation');
|
|
252
|
+
return (
|
|
253
|
+
<span
|
|
254
|
+
{...{
|
|
255
|
+
[LABEL_ATTR]: 'citation',
|
|
256
|
+
[LABEL_ID_ATTR]: id,
|
|
257
|
+
...(source ? { [LABEL_TEXT_ATTR]: source } : {}),
|
|
258
|
+
}}
|
|
259
|
+
data-od-cite={id}
|
|
260
|
+
className={className}
|
|
261
|
+
style={{
|
|
262
|
+
fontSize: '0.82em',
|
|
263
|
+
verticalAlign: 'super',
|
|
264
|
+
lineHeight: 0,
|
|
265
|
+
fontWeight: 600,
|
|
266
|
+
color: 'var(--od-accent)',
|
|
267
|
+
fontVariantNumeric: 'tabular-nums',
|
|
268
|
+
...style,
|
|
269
|
+
}}
|
|
270
|
+
>
|
|
271
|
+
[{entry?.number ?? '·'}]
|
|
272
|
+
</span>
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
export type BibliographyItem = {
|
|
277
|
+
id?: string;
|
|
278
|
+
title?: ReactNode;
|
|
279
|
+
source?: ReactNode;
|
|
280
|
+
author?: string;
|
|
281
|
+
year?: string | number;
|
|
282
|
+
url?: string;
|
|
283
|
+
note?: string;
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
export type BibliographyProps = {
|
|
287
|
+
/** Optional catalog of reference details keyed by citation `id` (or an array with `id`). Merged with inline `<Cite source="..." />` texts. */
|
|
288
|
+
items?: Record<string, string | BibliographyItem> | BibliographyItem[];
|
|
289
|
+
/** Optional heading text above the reference list. Pass `""` to omit. */
|
|
290
|
+
title?: ReactNode;
|
|
291
|
+
/** Show page numbers where each citation first appeared. */
|
|
292
|
+
showPageNumbers?: boolean;
|
|
293
|
+
style?: CSSProperties;
|
|
294
|
+
className?: string;
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
function renderBibItem(spec: string | BibliographyItem | undefined, fallback: string): ReactNode {
|
|
298
|
+
if (!spec) return fallback;
|
|
299
|
+
if (typeof spec === 'string') return spec;
|
|
300
|
+
if (spec.source && !spec.title) return spec.source;
|
|
301
|
+
return (
|
|
302
|
+
<span>
|
|
303
|
+
{spec.author ? `${spec.author}. ` : ''}
|
|
304
|
+
<strong style={{ fontWeight: 600, color: 'var(--od-text)' }}>{spec.title}</strong>
|
|
305
|
+
{spec.year ? ` (${spec.year}).` : '.'}
|
|
306
|
+
{spec.note ? ` ${spec.note}` : ''}
|
|
307
|
+
{spec.url ? (
|
|
308
|
+
<>
|
|
309
|
+
{' '}
|
|
310
|
+
<span
|
|
311
|
+
style={{
|
|
312
|
+
fontFamily: 'var(--od-font-mono)',
|
|
313
|
+
fontSize: '0.92em',
|
|
314
|
+
color: 'var(--od-muted)',
|
|
315
|
+
}}
|
|
316
|
+
>
|
|
317
|
+
{spec.url}
|
|
318
|
+
</span>
|
|
319
|
+
</>
|
|
320
|
+
) : null}
|
|
321
|
+
</span>
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Renders the numbered list of all `<Cite>` references in order of appearance,
|
|
327
|
+
* plus any additional uncited entries passed in `items`.
|
|
328
|
+
*/
|
|
329
|
+
export function Bibliography({
|
|
330
|
+
items,
|
|
331
|
+
title,
|
|
332
|
+
showPageNumbers = false,
|
|
333
|
+
style,
|
|
334
|
+
className,
|
|
335
|
+
}: BibliographyProps) {
|
|
336
|
+
const cited = useDocLabels('citation');
|
|
337
|
+
const vocabulary = useLabelVocabulary();
|
|
338
|
+
|
|
339
|
+
const map = new Map<string, string | BibliographyItem>();
|
|
340
|
+
if (Array.isArray(items)) {
|
|
341
|
+
items.forEach((item, idx) => {
|
|
342
|
+
map.set(item.id ?? `bib-${idx + 1}`, item);
|
|
343
|
+
});
|
|
344
|
+
} else if (items) {
|
|
345
|
+
for (const [k, v] of Object.entries(items)) map.set(k, v);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const rows: Array<{ id: string; number: number; content: ReactNode; page?: number }> = cited.map(
|
|
349
|
+
(entry) => ({
|
|
350
|
+
id: entry.id,
|
|
351
|
+
number: entry.number,
|
|
352
|
+
content: renderBibItem(map.get(entry.id), entry.text || entry.id),
|
|
353
|
+
page: entry.page,
|
|
354
|
+
}),
|
|
355
|
+
);
|
|
356
|
+
|
|
357
|
+
for (const [id, spec] of map.entries()) {
|
|
358
|
+
if (!rows.some((r) => r.id === id)) {
|
|
359
|
+
rows.push({
|
|
360
|
+
id,
|
|
361
|
+
number: rows.length + 1,
|
|
362
|
+
content: renderBibItem(spec, id),
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
const heading = title !== undefined ? title : vocabulary.bibliography;
|
|
368
|
+
|
|
369
|
+
return (
|
|
370
|
+
<div
|
|
371
|
+
data-od-bibliography=""
|
|
372
|
+
className={className}
|
|
373
|
+
style={{
|
|
374
|
+
fontFamily: 'var(--od-font-body)',
|
|
375
|
+
fontSize: 'var(--od-size-caption, 11px)',
|
|
376
|
+
lineHeight: 1.55,
|
|
377
|
+
color: 'var(--od-text)',
|
|
378
|
+
margin: '8px 0 14px',
|
|
379
|
+
...style,
|
|
380
|
+
}}
|
|
381
|
+
>
|
|
382
|
+
{heading ? (
|
|
383
|
+
<div
|
|
384
|
+
style={{
|
|
385
|
+
fontFamily: 'var(--od-font-heading)',
|
|
386
|
+
fontSize: 'var(--od-size-h3, 14px)',
|
|
387
|
+
fontWeight: 600,
|
|
388
|
+
marginBottom: 8,
|
|
389
|
+
color: 'var(--od-text)',
|
|
390
|
+
}}
|
|
391
|
+
>
|
|
392
|
+
{heading}
|
|
393
|
+
</div>
|
|
394
|
+
) : null}
|
|
395
|
+
<ol style={{ listStyle: 'none', padding: 0, margin: 0 }}>
|
|
396
|
+
{rows.map((row) => (
|
|
397
|
+
<li
|
|
398
|
+
key={row.id}
|
|
399
|
+
data-od-bib-entry={row.id}
|
|
400
|
+
style={{
|
|
401
|
+
display: 'flex',
|
|
402
|
+
alignItems: 'baseline',
|
|
403
|
+
gap: 8,
|
|
404
|
+
marginBottom: 5,
|
|
405
|
+
}}
|
|
406
|
+
>
|
|
407
|
+
<span
|
|
408
|
+
style={{
|
|
409
|
+
flex: 'none',
|
|
410
|
+
minWidth: 24,
|
|
411
|
+
fontWeight: 600,
|
|
412
|
+
color: 'var(--od-accent)',
|
|
413
|
+
fontVariantNumeric: 'tabular-nums',
|
|
414
|
+
}}
|
|
415
|
+
>
|
|
416
|
+
[{row.number}]
|
|
417
|
+
</span>
|
|
418
|
+
<span style={{ flex: 1, color: 'var(--od-text)' }}>{row.content}</span>
|
|
419
|
+
{showPageNumbers && row.page !== undefined && (
|
|
420
|
+
<span
|
|
421
|
+
style={{
|
|
422
|
+
flex: 'none',
|
|
423
|
+
color: 'var(--od-muted)',
|
|
424
|
+
fontVariantNumeric: 'tabular-nums',
|
|
425
|
+
}}
|
|
426
|
+
>
|
|
427
|
+
p. {row.page}
|
|
428
|
+
</span>
|
|
429
|
+
)}
|
|
430
|
+
</li>
|
|
431
|
+
))}
|
|
432
|
+
</ol>
|
|
433
|
+
</div>
|
|
434
|
+
);
|
|
435
|
+
}
|