Skip to content

Commit 75716ff

Browse files
committed
fix(fields): draw a markdown heading in a grid cell at the cell's text size
Claude-Session: https://claude.ai/code/session_01QsR2jUc3qp6bHKSWijbjds Co-authored-by: Claude <noreply@anthropic.com>
1 parent ed33f8a commit 75716ff

4 files changed

Lines changed: 149 additions & 7 deletions

File tree

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* ObjectUI
3+
* Copyright (c) 2024-present ObjectStack Inc.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
/**
10+
* objectstack#36344 — a markdown value that starts with a heading, drawn in a
11+
* grid column, keeps the row the height of every other row.
12+
*
13+
* The grid clamps a rich-content cell to one line, but the line was the
14+
* heading's: `prose-sm` draws an `h1` at heading size, so the cell showed a
15+
* truncated H1 and the row grew. In a grid column (`inGridColumn`) the cell
16+
* now draws its value in the `cell` face: no heading element, the heading as
17+
* a bold line, and the body at the cell's own font size and line height.
18+
* Every other caller keeps the `document` face and its heading elements.
19+
*
20+
* jsdom does no layout, so these pins read the element and class contract the
21+
* row height rests on; the pixel readings are in the pull request.
22+
*/
23+
24+
import { describe, it, expect, afterEach } from 'vitest';
25+
import React from 'react';
26+
import { render, waitFor, cleanup } from '@testing-library/react';
27+
import '@testing-library/jest-dom';
28+
// Module scope, with the SAME specifier `MarkdownCellRenderer`'s `React.lazy`
29+
// factory uses, so the lazy chunk is resolved before any assertion runs.
30+
import '../widgets/MarkdownContent.js';
31+
import { getCellRenderer, resolveCellRendererType } from '../index';
32+
33+
const HEADING_VALUE = '# Heading\n\nBody with **bold** text';
34+
const HEADINGS = 'h1, h2, h3, h4, h5, h6';
35+
36+
afterEach(() => cleanup());
37+
38+
/** Resolve the way every read surface does, then settle past the Suspense fallback. */
39+
async function renderMarkdown(value: string, props: { inGridColumn?: boolean } = {}) {
40+
const Renderer = getCellRenderer(resolveCellRendererType({ type: 'markdown' }) || 'markdown');
41+
const utils = render(<Renderer value={value} field={{ type: 'markdown' } as any} {...props} />);
42+
const face = await waitFor(() => {
43+
const el = utils.container.querySelector<HTMLElement>('[data-markdown-face]');
44+
if (!el) throw new Error('markdown body not rendered yet');
45+
return el;
46+
});
47+
return { ...utils, face };
48+
}
49+
50+
describe('objectstack#36344 — a markdown heading in a grid cell is drawn at the cell size', () => {
51+
it('in a grid column: no heading element, the heading is a bold line, the body inherits the cell size', async () => {
52+
const { face } = await renderMarkdown(HEADING_VALUE, { inGridColumn: true });
53+
54+
expect(face).toHaveAttribute('data-markdown-face', 'cell');
55+
expect(face.querySelector(HEADINGS)).toBeNull();
56+
57+
const heading = face.querySelector<HTMLElement>('[data-markdown-heading]');
58+
expect(heading?.tagName).toBe('P');
59+
expect(heading).toHaveTextContent('Heading');
60+
expect(heading).toHaveClass('font-semibold');
61+
62+
// The body takes the cell's own font size and line height, so the line
63+
// the grid's one-line clamp shows is a line of the cell's text.
64+
expect(face).toHaveClass('text-[length:inherit]', 'leading-[inherit]');
65+
66+
// The rest of the value is still formatted, not flattened to source text.
67+
expect(face.querySelector('strong')).toHaveTextContent('bold');
68+
expect(face.textContent).not.toContain('#');
69+
});
70+
71+
it('in a grid column: every heading level is drawn the same way', async () => {
72+
const value = ['# one', '## two', '### three', '#### four', '##### five', '###### six'].join('\n\n');
73+
const { face } = await renderMarkdown(value, { inGridColumn: true });
74+
expect(face.querySelector(HEADINGS)).toBeNull();
75+
expect(face.querySelectorAll('[data-markdown-heading]')).toHaveLength(6);
76+
});
77+
78+
it('CONTROL — outside a grid column (the record page) the heading element and its size stay', async () => {
79+
const { face } = await renderMarkdown(HEADING_VALUE);
80+
81+
expect(face).toHaveAttribute('data-markdown-face', 'document');
82+
expect(face.querySelector('h1')).toHaveTextContent('Heading');
83+
expect(face.querySelector('[data-markdown-heading]')).toBeNull();
84+
expect(face).toHaveClass('prose', 'prose-sm');
85+
expect(face).not.toHaveClass('text-[length:inherit]');
86+
expect(face).not.toHaveClass('leading-[inherit]');
87+
});
88+
});

‎ui/packages/fields/src/index.tsx‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,14 @@ export interface CellRendererProps {
440440
alwaysShowYear?: boolean;
441441
/**
442442
* The cell is one of a grid COLUMN's cells, read against the cells above
443-
* and below it (objectstack#25884). Read by the `date` cell only.
443+
* and below it (objectstack#25884). Read by the `date` cell and the
444+
* `markdown` cell.
445+
*
446+
* The `markdown` cell then draws its value in the cell face
447+
* (objectstack#36344): a heading is a bold line at the cell's own text size
448+
* instead of a heading element, so a value that starts with `# Heading`
449+
* keeps the row's height. The record page does not pass it and keeps the
450+
* heading sizes.
444451
*
445452
* A `date` field that declares no `format` then paints `formatDate`'s
446453
* absolute default face instead of `'relative'`, and the relative phrase

‎ui/packages/fields/src/widgets/MarkdownContent.tsx‎

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,60 @@
11
import React from 'react';
2-
import ReactMarkdown from 'react-markdown';
2+
import ReactMarkdown, { type Components } from 'react-markdown';
33
import remarkGfm from 'remark-gfm';
44
import rehypeSanitize from 'rehype-sanitize';
55
import { RICH_TEXT_SANITIZE_SCHEMA } from './sanitizedHtml.js';
66

7+
/**
8+
* The two faces a markdown value is drawn in.
9+
*
10+
* - `document` — a full-size display (the record page, a readonly form field):
11+
* headings keep their heading elements and the typography scale's sizes.
12+
* - `cell` — one cell of a grid column, whose row is one line of the cell's own
13+
* text (objectstack#36344). A heading is drawn as a bold line at the cell's
14+
* text size, not as a heading element, and the body inherits the cell's font
15+
* size and line height, so a value that starts with `# Heading` keeps the row
16+
* the height of every other row.
17+
*/
18+
export type MarkdownFace = 'document' | 'cell';
19+
20+
const DOCUMENT_CLASS = 'prose prose-sm max-w-none dark:prose-invert break-words';
21+
22+
/** The cell face: the same typography, at the size and line height of the cell around it. */
23+
export const MARKDOWN_CELL_FACE_CLASS = `${DOCUMENT_CLASS} text-[length:inherit] leading-[inherit]`;
24+
25+
function CellHeading({ children }: { children?: React.ReactNode }) {
26+
return <p className="font-semibold" data-markdown-heading="">{children}</p>;
27+
}
28+
29+
/**
30+
* In the cell face no heading element is drawn: a heading inside a table cell
31+
* is drawn at heading size by the typography scale, and it would also add one
32+
* entry per row to the page's heading outline.
33+
*/
34+
const CELL_COMPONENTS: Components = {
35+
h1: CellHeading,
36+
h2: CellHeading,
37+
h3: CellHeading,
38+
h4: CellHeading,
39+
h5: CellHeading,
40+
h6: CellHeading,
41+
};
42+
743
/**
844
* Heavy markdown renderer, split into its own module so it can be lazy-loaded
945
* by the cell/detail renderers (react-markdown pulls in ~100-200 KB). GitHub
1046
* Flavored Markdown + sanitization (defense against XSS in stored content),
1147
* against the same schema the `html` / `richtext` pipeline uses.
1248
*/
13-
export default function MarkdownContent({ value }: { value: string }) {
49+
export default function MarkdownContent({ value, face = 'document' }: { value: string; face?: MarkdownFace }) {
50+
const cell = face === 'cell';
1451
return (
15-
<div className="prose prose-sm max-w-none dark:prose-invert break-words">
16-
<ReactMarkdown remarkPlugins={[remarkGfm]} rehypePlugins={[[rehypeSanitize, RICH_TEXT_SANITIZE_SCHEMA]]}>
52+
<div className={cell ? MARKDOWN_CELL_FACE_CLASS : DOCUMENT_CLASS} data-markdown-face={face}>
53+
<ReactMarkdown
54+
remarkPlugins={[remarkGfm]}
55+
rehypePlugins={[[rehypeSanitize, RICH_TEXT_SANITIZE_SCHEMA]]}
56+
components={cell ? CELL_COMPONENTS : undefined}
57+
>
1758
{value}
1859
</ReactMarkdown>
1960
</div>

‎ui/packages/fields/src/widgets/richTextDisplay.tsx‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,19 @@ function richTextSource(value: unknown): string | undefined {
9090
* What the cell holds is decided by {@link richTextSource} (objectui#8580):
9191
* `[]` is the No-value affordance, never a childless container, and an
9292
* object is the text the string class prints for it, never `[object Object]`.
93+
*
94+
* In a grid column (`inGridColumn`) the value is drawn in the `cell` face
95+
* (objectstack#36344): a heading is a bold line at the cell's own text size,
96+
* so a value that starts with `# Heading` does not grow its row. Every other
97+
* caller — the record page, a readonly form field — keeps the `document`
98+
* face and its heading sizes.
9399
*/
94-
export function MarkdownCellRenderer({ value }: CellRendererProps): React.ReactElement {
100+
export function MarkdownCellRenderer({ value, inGridColumn }: CellRendererProps): React.ReactElement {
95101
const text = richTextSource(value);
96102
if (text === undefined) return <EmptyValue />;
97103
return (
98104
<React.Suspense fallback={<span className="text-sm text-muted-foreground">{text.slice(0, 80)}</span>}>
99-
<LazyMarkdownContent value={text} />
105+
<LazyMarkdownContent value={text} face={inGridColumn ? 'cell' : 'document'} />
100106
</React.Suspense>
101107
);
102108
}

0 commit comments

Comments
 (0)