Skip to content

Commit 24366ff

Browse files
fix(fields): a markdown heading in a list cell draws at the cell's text size (#36428)
Fixes #36344 Clause-②: no Metadata-protocol: none ## 维护者速读 - 用户在列表里看到的变化:含 Markdown 字段的列表单元格,内容以标题开头时不再以大标题字号显示、也不再把这一行撑高,行高与其他行一致;记录详情页仍按原样显示大标题。 - 为什么做:列表视图体验修复,替在列表中显示富文本列的用户解决「标题被截断、行变高」的问题。 - 出错会怎样:最坏情况是列表里 Markdown 单元格的样式不对(只影响显示,不影响数据),可以直接撤回本次改动。 - 要你做什么:无。 ## What changes - `@object-ui/fields` `MarkdownContent` gains a `face` prop: `document` (the default, unchanged) and `cell`. The cell face draws every heading level as a `p.font-semibold` (no heading element) and the body takes the surrounding cell's font size and line height (`text-[length:inherit] leading-[inherit]` on top of the same `prose prose-sm` typography). - `MarkdownCellRenderer` picks the cell face when `inGridColumn` is set. `ObjectGrid` already passes `inGridColumn` on every column cell, and `RelatedList` on every table cell, so no host changes. The record page, readonly form fields and every other caller pass nothing and keep the `document` face with its heading sizes. - `CellRendererProps.inGridColumn` JSDoc now names the `markdown` cell as its second reader. - Changeset: `@object-ui/fields` patch. Read first (by risk): 1. `ui/packages/fields/src/widgets/MarkdownContent.tsx` — the cell face: `CELL_COMPONENTS` (heading levels mapped to a paragraph) and `MARKDOWN_CELL_FACE_CLASS`. Sanitisation is unchanged: the same `rehype-sanitize` schema runs in both faces; the component map only renames the element of a node the sanitiser already passed. 2. `ui/packages/fields/src/widgets/richTextDisplay.tsx` — the one line that chooses the face from `inGridColumn`. 3. `ui/packages/fields/src/__tests__/markdownCellHeading-36344.test.tsx` — the pins and the record-page control. Two-minute check: `pnpm --dir ui exec vitest run packages/fields/src/__tests__/markdownCellHeading-36344.test.tsx packages/plugin-grid/src/__tests__/ObjectGrid.richTextOneLine-12480.test.tsx` from the repository root — expect 2 files, 8 tests passed. ## Tests - New `ui/packages/fields/src/__tests__/markdownCellHeading-36344.test.tsx` (3 cases): in a grid column, a `# Heading` value draws no `h1`-`h6`, the heading is a `p.font-semibold`, the body carries the two inherit classes and stays formatted; all six heading levels are drawn the same way; CONTROL — without `inGridColumn` (the record page path) the `h1` stays and the inherit classes are absent. - New top-level `describe` in `ui/packages/plugin-grid/src/__tests__/ObjectGrid.richTextOneLine-12480.test.tsx` (the existing one-line-clamp harness): through the real `ObjectGrid`, the markdown cell inside the clamp is the cell face and `tbody` holds no heading element. This is the row-height contract the grid relies on (one line inside `line-clamp-1`, at the cell's line height). - The existing readonly form pin (`readonly-richtext-display-5498.test.tsx`, a readonly markdown field keeps its `h1`) stays green: full-size displays keep their heading elements. <details><summary>Ablation (one, the behaviour the pins hold)</summary> The fix committed first; mutation through `scripts/ablation-replace.mjs` on `richTextDisplay.tsx`, anchor `face={inGridColumn ? 'cell' : 'document'}` replaced by `face={'document'}` (anchor x1 to x0, blob c327713 to a365cf70). No build step: vitest resolves `@object-ui/fields` to `src` through `ui/vitest.config.mts`'s alias. - Mutated: `Test Files 2 failed | 1 passed (3)`, `Tests 3 failed | 5 passed (8)`. The three cell-face cases went red (`AssertionError: expected` an empty `h1` element `to be null`), the record-page control and the original objectui#12480 cases stayed green. - Restored: blob after restore c327713 == blob at HEAD, `git diff HEAD` empty. </details> <details><summary>Pixel reading (Chromium, not the live app)</summary> Harness: the DOM of a real `ObjectGrid` render (jsdom, 4 rows: plain text, `# Heading` + paragraph, `**bold** and _em_`, `## Second level`) and of the record-page `MarkdownCellRenderer`, laid into a page with the Console's own stylesheet compiled from `ui/apps/console/src/index.css` (Tailwind 4 + typography), opened in Chromium at 1280 px. A temporary dump test, not committed. | row | before (ablated) | after | |---|---|---| | plain text | 36 px | 36 px | | `# Heading` + paragraph | 45 px (H1 30px / 36px line) | 36 px (P 14px / 17.5px line) | | `**bold** and _em_` | 36 px | 36 px | | `## Second level` | 36.5 px (H2 20px / 28px line) | 36 px | | record page `h1` | 30 px | 30 px | The 36 to 45 px reading matches the card's. NOT MEASURED in the running Console against a backend (the harness uses the real component DOM and the real stylesheet, but no live list page). </details> ## Acceptance notes - Scope edge: `wrap: true` columns also pass `inGridColumn`, so a wrapped markdown cell draws headings as bold lines at the cell size too. The block structure (paragraphs, lists) is kept, so a wrapped cell still shows the whole document, just without heading sizes. A list cell is not a full-size display, so this reads as within the card. - Observation, not filed: the `html` / `richtext` cell (`HtmlCellRenderer`, a different pipeline, `renderSanitizedHtml`) keeps its heading elements in a grid cell under the same `prose prose-sm`, so a stored `h1` tag should draw the same way. Read from the code only; not measured at a public entry point, so it is noted here, not filed. - Out of scope per the card: in-cell editing of rich-text fields (#36023, PR #36278). `overlap-check` before push: no open PR head shares a file with this branch, and the branch merges cleanly with `origin/main`. ## Verification All readings on head `e241a872` (branch merged with `origin/main` at 97b9d1e before the runs), each under `scripts/pm/os-verify-lock.sh`, unless noted. <details><summary>Tests</summary> - `pnpm --dir ui exec vitest run --maxWorkers=2` over the six rich-text files (the two new pins, `readonly-richtext-display-5498`, `richtext-cell-renderer-5452`, `cellRenderers.childlessContainer-8580`, `RelatedList.longFormColumns`): `Test Files 6 passed (6)`, `Tests 55 passed (55)`. - `pnpm --dir ui exec vitest run --maxWorkers=2 packages/fields/`: `Test Files 322 passed | 1 skipped (323)`, `Tests 4846 passed | 7 skipped (4853)`. - `pnpm --filter @object-ui/fields run type-check` exit 0; `pnpm --filter @object-ui/plugin-grid run type-check` exit 0 (both run `tsc --noEmit && tsc -p tsconfig.test.json`, after building the `@object-ui/plugin-grid` dependency closure). - The ablation above ran on `919a2f4f`, the commit before the `origin/main` merge; the merge brought no change to the files it reads. </details> <details><summary>Gates</summary> - `node scripts/pm/dispatch-gates.mjs --commands` (no paths): 39 families for the 6 changed paths. All 39 run, each exit 0, including `pnpm --dir ui exec vitest run scripts/` (`Test Files 172 passed | 2 skipped (174)`). `pnpm check:type-check-debt` answered exit 3 (PREREQUISITE NOT MET, no built closure) in the first sweep and exit 0 on the re-run (`check-type-check-coverage --re-measure: OK`). - `--ran` reconciliation: `39 derived famil(ies) accounted for — 39 run, 0 NOT-MEASURED (a DERIVED zero — all 39 recorded an exit code and none of them is 3)`. - `node scripts/pm/overlap-check.mjs`: `VERDICT clean exit 0` (origin/main merges cleanly; 0 open PR heads share a file). - Repo-wide families (lint, the full UI test shards, dogfood) are CI's. </details> --- _Generated by [Claude Code](https://claude.ai/code/session_01QsR2jUc3qp6bHKSWijbjds)_ --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent d8628ac commit 24366ff

6 files changed

Lines changed: 192 additions & 7 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@object-ui/fields': patch
3+
---
4+
5+
fix(fields): a markdown value that starts with a heading no longer draws at heading size in a list cell
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
In a grid column (list views and related lists pass `inGridColumn`), the `markdown` cell now draws its value in a cell face: a heading is a bold line at the cell's own text size instead of a heading element, and the body takes the cell's font size and line height. A value such as `# Heading` showed a truncated H1 and grew its row; the row now keeps the height of every other row. The record page and readonly form fields keep the full document face and its heading sizes. Nothing changes in what an author writes, and no export changes.
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
}

‎ui/packages/plugin-grid/src/__tests__/ObjectGrid.richTextOneLine-12480.test.tsx‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,36 @@ describe('objectui#12480 — a rich-content cell keeps the one-line row', () =>
157157
expect(screen.getByText('Account 8').closest('td')?.querySelector('.truncate')).not.toBeNull();
158158
});
159159
});
160+
161+
/**
162+
* objectstack#36344 — the one line the clamp shows is a line of the cell's
163+
* text. A markdown value that starts with `# Heading` filled the clamp's one
164+
* line with an H1, so the row grew to the heading's line height. The grid
165+
* passes `inGridColumn` to every column cell, and the markdown cell then draws
166+
* its value in the `cell` face: no heading element, the cell's own font size
167+
* and line height. jsdom does no layout; the pixel readings are in the PR.
168+
*/
169+
describe('objectstack#36344 — a markdown heading in a grid cell does not draw at heading size', () => {
170+
it('a markdown cell whose value starts with a heading draws no heading element inside the clamp', async () => {
171+
desktop();
172+
const rows = [
173+
{ id: 'r1', name: 'Account 1', description: '# Heading\n\nBody' },
174+
{ id: 'r2', name: 'Account 2' },
175+
];
176+
renderGrid(rows, [
177+
{ field: 'name', label: 'Name' },
178+
{ field: 'description', label: 'Description' },
179+
]);
180+
await waitFor(() => expect(screen.getByText('Account 2')).toBeInTheDocument());
181+
const row1 = bodyRows()[0] as HTMLElement;
182+
const face = await waitFor(() => {
183+
const el = row1.querySelector<HTMLElement>('[data-testid="grid-rich-text-one-line"] [data-markdown-face]');
184+
if (!el) throw new Error('markdown cell not rendered yet');
185+
return el;
186+
});
187+
expect(face).toHaveAttribute('data-markdown-face', 'cell');
188+
expect(face).toHaveClass('text-[length:inherit]', 'leading-[inherit]');
189+
expect(face.querySelector('[data-markdown-heading]')).toHaveTextContent('Heading');
190+
expect(document.querySelector('tbody h1, tbody h2, tbody h3, tbody h4, tbody h5, tbody h6')).toBeNull();
191+
});
192+
});

0 commit comments

Comments
 (0)