Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ export default defineConfig({
`styles.css` draws: end a line with `// [!code highlight]` for a quiet tint
and a bar on the left, or `// [!code ++]` and `// [!code --]` for lines a
change adds and removes. Use the comment syntax of the block's language. The
Markdown copy of the page drops the comments and keeps the code. `rehypeTableLabels` gives each table cell a
`data-label` so that tables stack on a phone.
Markdown copy of the page drops the comments and keeps the code. `rehypeTableLabels` gives each table cell after the
first a `data-label` so that tables stack on a phone, and marks short inline code
`data-short` so that it stays on one line.

### Configure Vite

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@withmarfa/docs-kit",
"version": "0.0.1-alpha.5",
"version": "0.0.1-alpha.6",
"description": "The shared look and plumbing of WithMarfa's documentation sites: Fumadocs layout, components, styles, agent surfaces and search.",
"license": "Apache-2.0",
"type": "module",
Expand Down
43 changes: 39 additions & 4 deletions src/lib/table-labels.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
import { labelTable, type HastNode } from './table-labels';
import { labelTable, rehypeTableLabels, type HastNode } from './table-labels';

const cell = (tagName: string, value: string): HastNode => ({
type: 'element',
Expand Down Expand Up @@ -30,10 +30,10 @@ describe('labelTable', () => {
expect(labels(node)).toEqual([[undefined, 'What it does', 'Default']]);
});

it('leaves a two-column table unlabelled', () => {
const node = table(['Code', 'Meaning'], ['0', 'Success']);
it('labels the second cell of a two-column table, and never the first', () => {
const node = table(['InkKit does', 'Your app does, through'], ['Edits text', 'The `load` call']);
labelTable(node);
expect(labels(node)).toEqual([[undefined, undefined]]);
expect(labels(node)).toEqual([[undefined, 'Your app does, through']]);
});

it('skips a column with an empty header', () => {
Expand All @@ -55,3 +55,38 @@ describe('labelTable', () => {
expect(body?.children?.[0]?.children?.map(role)).toEqual(['cell', 'cell']);
});
});

describe('rehypeTableLabels', () => {
const code = (value: string): HastNode => ({
type: 'element',
tagName: 'code',
properties: {},
children: [{ type: 'text', value }],
});
const element = (tagName: string, ...children: HastNode[]): HastNode => ({
type: 'element',
tagName,
properties: {},
children,
});
const marked = (node: HastNode) => node.properties?.dataShort === '';

it('marks short inline code, in prose and in a table cell, and leaves long code to wrap', () => {
const short = code('invalid-argument');
const inCell = code('stale-document');
const long = code('color-mix(in srgb, CanvasText 18%, transparent)');
const tree = element(
'root',
element('p', short, long),
{ ...table(['A', 'B'], ['x', 'y']), children: [element('tbody', element('tr', element('td', inCell)))] },
);
rehypeTableLabels()(tree);
expect([short, inCell, long].map(marked)).toEqual([true, true, false]);
});

it('leaves the code of a code block alone', () => {
const block = code('pnpm add');
rehypeTableLabels()(element('root', element('pre', block)));
expect(marked(block)).toBe(false);
});
});
29 changes: 25 additions & 4 deletions src/lib/table-labels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
* A rehype plugin that names each table cell's column on the cell, as
* `data-label`. On a phone a table stacks into one entry per row, its header
* row hidden, and the stylesheet writes the label before a cell so the entry
* still says what each value is. A two-column table needs no label: its
* second cell is plainly what the first means. The first cell of a row is the
* entry's name and is never labelled.
* still says what each value is. The first cell of a row is the entry's title
* and is never labelled; every later cell is, in a two-column table too, where
* the label is the only thing saying what the second cell is to the first.
*
* It also marks short inline code `data-short`, which the stylesheet keeps on
* one line so a name such as `invalid-argument` does not split at its hyphen.
* Longer code, such as a URL or a default value, is left to wrap.
*
* Stacking sets `display: block` on the table's parts, which drops their
* table semantics in some browsers, so every part also gets its ARIA role and
Expand Down Expand Up @@ -59,14 +63,30 @@ function giveRoles(table: HastNode): void {
visit(table, false);
}

const shortCodeLength = 24;

/** Marks each inline code element of at most `shortCodeLength` characters. */
function markShortCode(tree: HastNode): void {
const visit = (node: HastNode) => {
for (const child of node.children ?? []) {
if (child.type !== 'element') continue;
if (child.tagName === 'pre') continue;
if (child.tagName === 'code' && text(child).trim().length <= shortCodeLength) {
child.properties = { ...child.properties, dataShort: '' };
}
visit(child);
}
};
visit(tree);
}

/** Gives one table's parts their roles and labels its cells from its header row. */
export function labelTable(table: HastNode): void {
giveRoles(table);
const [head] = elements(table, 'thead');
const [headRow] = head ? elements(head, 'tr') : [];
if (!headRow) return;
const names = elements(headRow, 'th').map((cell) => text(cell).trim());
if (names.length < 3) return;
for (const body of elements(table, 'tbody')) {
for (const row of elements(body, 'tr')) {
elements(row, 'td').forEach((cell, index) => {
Expand All @@ -79,6 +99,7 @@ export function labelTable(table: HastNode): void {

export function rehypeTableLabels() {
return (tree: HastNode) => {
markShortCode(tree);
const visit = (node: HastNode) => {
if (node.type === 'element' && node.tagName === 'table') labelTable(node);
for (const child of node.children ?? []) visit(child);
Expand Down
6 changes: 6 additions & 0 deletions src/styles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ describe('styles.css', () => {
expect(css).not.toMatch(/@media \(max-width: 639px\) \{\s*\.prose table/);
});

it('keeps short inline code on one line, and long code wrapping', () => {
expect(rule(`.prose :where(code[data-short])${guard}`)).toContain('white-space: nowrap');
expect(rule(`.prose :where(code)${guard}`)).toContain('overflow-wrap: anywhere');
expect(css).toContain('& td code:not([data-short])');
});

it('shows a previous or next card whole, with room under the last one', () => {
const description = rule('.docs-kit-page-footer > a > p:last-child');
expect(description).toContain('white-space: normal');
Expand Down
17 changes: 11 additions & 6 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,19 @@

/* Code in a sentence is a soft fill with no border, so several in one
paragraph read as part of the text rather than a row of buttons. A long
value, such as a URL, breaks anywhere rather than running off a phone. */
value, such as a URL, breaks anywhere rather than running off a phone; a
short one, which `rehypeTableLabels` marks `data-short`, stays whole, where
a break would split a name such as `invalid-argument` at its hyphen. */
.prose :where(code):not(:where([class~='not-prose'], [class~='not-prose'] *)) {
overflow-wrap: anywhere;
padding: 0.125em 0.375em;
border: 0;
border-radius: var(--radius-sm);
background-color: var(--color-fd-muted);
}
.prose :where(code[data-short]):not(:where([class~='not-prose'], [class~='not-prose'] *)) {
white-space: nowrap;
}
/* A heading that is only a name in code, such as a reference page's member,
reads as monospace text, not as a chip above the signature. */
.prose :is(h2, h3, h4):not(:where([class~='not-prose'], [class~='not-prose'] *)) > code:only-child,
Expand Down Expand Up @@ -394,10 +399,10 @@ html > body[data-scroll-locked] {
screen: beside a sidebar a tablet's column is as narrow as a phone's, and
three columns of it leave one a sliver. The wrapper `getDocsKitMDXComponents`
draws around a table is the container. The header row is hidden from sight,
so a cell after the second writes its column's name before its value, from
the `data-label` that `rehypeTableLabels` sets (a two-column table needs
none: its second cell is plainly what the first means). An empty cell is left
out. A screen reader still meets a table: the plugin gives each part its
so every cell after the first writes its column's name before its value,
from the `data-label` that `rehypeTableLabels` sets: the first cell is the
entry's title, and the label is what says what the others are to it, in a
two-column table as in a wider one. An empty cell is left out. A screen reader still meets a table: the plugin gives each part its
role, the header row stays in the accessibility tree, and the written label
has empty alternative text, so a header is heard once, from the table. */
.docs-kit-table {
Expand Down Expand Up @@ -447,7 +452,7 @@ html > body[data-scroll-locked] {
font-size: 0.75rem;
font-weight: 500;
}
& td code {
& td code:not([data-short]) {
white-space: normal;
overflow-wrap: anywhere;
}
Expand Down