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
10 changes: 10 additions & 0 deletions .changeset/36344-markdown-cell-heading.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@object-ui/fields': patch
---

fix(fields): a markdown value that starts with a heading no longer draws at heading size in a list cell

Clause-②: no
Metadata-protocol: none

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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* ObjectUI
* Copyright (c) 2024-present ObjectStack Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

/**
* objectstack#36344 — a markdown value that starts with a heading, drawn in a
* grid column, keeps the row the height of every other row.
*
* The grid clamps a rich-content cell to one line, but the line was the
* heading's: `prose-sm` draws an `h1` at heading size, so the cell showed a
* truncated H1 and the row grew. In a grid column (`inGridColumn`) the cell
* now draws its value in the `cell` face: no heading element, the heading as
* a bold line, and the body at the cell's own font size and line height.
* Every other caller keeps the `document` face and its heading elements.
*
* jsdom does no layout, so these pins read the element and class contract the
* row height rests on; the pixel readings are in the pull request.
*/

import { describe, it, expect, afterEach } from 'vitest';
import React from 'react';
import { render, waitFor, cleanup } from '@testing-library/react';
import '@testing-library/jest-dom';
// Module scope, with the SAME specifier `MarkdownCellRenderer`'s `React.lazy`
// factory uses, so the lazy chunk is resolved before any assertion runs.
import '../widgets/MarkdownContent.js';
import { getCellRenderer, resolveCellRendererType } from '../index';

const HEADING_VALUE = '# Heading\n\nBody with **bold** text';
const HEADINGS = 'h1, h2, h3, h4, h5, h6';

afterEach(() => cleanup());

/** Resolve the way every read surface does, then settle past the Suspense fallback. */
async function renderMarkdown(value: string, props: { inGridColumn?: boolean } = {}) {
const Renderer = getCellRenderer(resolveCellRendererType({ type: 'markdown' }) || 'markdown');
const utils = render(<Renderer value={value} field={{ type: 'markdown' } as any} {...props} />);
const face = await waitFor(() => {
const el = utils.container.querySelector<HTMLElement>('[data-markdown-face]');
if (!el) throw new Error('markdown body not rendered yet');
return el;
});
return { ...utils, face };
}

describe('objectstack#36344 — a markdown heading in a grid cell is drawn at the cell size', () => {
it('in a grid column: no heading element, the heading is a bold line, the body inherits the cell size', async () => {
const { face } = await renderMarkdown(HEADING_VALUE, { inGridColumn: true });

expect(face).toHaveAttribute('data-markdown-face', 'cell');
expect(face.querySelector(HEADINGS)).toBeNull();

const heading = face.querySelector<HTMLElement>('[data-markdown-heading]');
expect(heading?.tagName).toBe('P');
expect(heading).toHaveTextContent('Heading');
expect(heading).toHaveClass('font-semibold');

// The body takes the cell's own font size and line height, so the line
// the grid's one-line clamp shows is a line of the cell's text.
expect(face).toHaveClass('text-[length:inherit]', 'leading-[inherit]');

// The rest of the value is still formatted, not flattened to source text.
expect(face.querySelector('strong')).toHaveTextContent('bold');
expect(face.textContent).not.toContain('#');
});

it('in a grid column: every heading level is drawn the same way', async () => {
const value = ['# one', '## two', '### three', '#### four', '##### five', '###### six'].join('\n\n');
const { face } = await renderMarkdown(value, { inGridColumn: true });
expect(face.querySelector(HEADINGS)).toBeNull();
expect(face.querySelectorAll('[data-markdown-heading]')).toHaveLength(6);
});

it('CONTROL — outside a grid column (the record page) the heading element and its size stay', async () => {
const { face } = await renderMarkdown(HEADING_VALUE);

expect(face).toHaveAttribute('data-markdown-face', 'document');
expect(face.querySelector('h1')).toHaveTextContent('Heading');
expect(face.querySelector('[data-markdown-heading]')).toBeNull();
expect(face).toHaveClass('prose', 'prose-sm');
expect(face).not.toHaveClass('text-[length:inherit]');
expect(face).not.toHaveClass('leading-[inherit]');
});
});
9 changes: 8 additions & 1 deletion ui/packages/fields/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,14 @@ export interface CellRendererProps {
alwaysShowYear?: boolean;
/**
* The cell is one of a grid COLUMN's cells, read against the cells above
* and below it (objectstack#25884). Read by the `date` cell only.
* and below it (objectstack#25884). Read by the `date` cell and the
* `markdown` cell.
*
* The `markdown` cell then draws its value in the cell face
* (objectstack#36344): a heading is a bold line at the cell's own text size
* instead of a heading element, so a value that starts with `# Heading`
* keeps the row's height. The record page does not pass it and keeps the
* heading sizes.
*
* A `date` field that declares no `format` then paints `formatDate`'s
* absolute default face instead of `'relative'`, and the relative phrase
Expand Down
49 changes: 45 additions & 4 deletions ui/packages/fields/src/widgets/MarkdownContent.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,60 @@
import React from 'react';
import ReactMarkdown from 'react-markdown';
import ReactMarkdown, { type Components } from 'react-markdown';
import remarkGfm from 'remark-gfm';
import rehypeSanitize from 'rehype-sanitize';
import { RICH_TEXT_SANITIZE_SCHEMA } from './sanitizedHtml.js';

/**
* The two faces a markdown value is drawn in.
*
* - `document` — a full-size display (the record page, a readonly form field):
* headings keep their heading elements and the typography scale's sizes.
* - `cell` — one cell of a grid column, whose row is one line of the cell's own
* text (objectstack#36344). A heading is drawn as a bold line at the cell's
* text size, not as a heading element, and the body inherits the cell's font
* size and line height, so a value that starts with `# Heading` keeps the row
* the height of every other row.
*/
export type MarkdownFace = 'document' | 'cell';

const DOCUMENT_CLASS = 'prose prose-sm max-w-none dark:prose-invert break-words';

/** The cell face: the same typography, at the size and line height of the cell around it. */
export const MARKDOWN_CELL_FACE_CLASS = `${DOCUMENT_CLASS} text-[length:inherit] leading-[inherit]`;

function CellHeading({ children }: { children?: React.ReactNode }) {
return <p className="font-semibold" data-markdown-heading="">{children}</p>;
}

/**
* In the cell face no heading element is drawn: a heading inside a table cell
* is drawn at heading size by the typography scale, and it would also add one
* entry per row to the page's heading outline.
*/
const CELL_COMPONENTS: Components = {
h1: CellHeading,
h2: CellHeading,
h3: CellHeading,
h4: CellHeading,
h5: CellHeading,
h6: CellHeading,
};

/**
* Heavy markdown renderer, split into its own module so it can be lazy-loaded
* by the cell/detail renderers (react-markdown pulls in ~100-200 KB). GitHub
* Flavored Markdown + sanitization (defense against XSS in stored content),
* against the same schema the `html` / `richtext` pipeline uses.
*/
export default function MarkdownContent({ value }: { value: string }) {
export default function MarkdownContent({ value, face = 'document' }: { value: string; face?: MarkdownFace }) {
const cell = face === 'cell';
return (
<div className="prose prose-sm max-w-none dark:prose-invert break-words">
<ReactMarkdown remarkPlugins={[remarkGfm]} rehypePlugins={[[rehypeSanitize, RICH_TEXT_SANITIZE_SCHEMA]]}>
<div className={cell ? MARKDOWN_CELL_FACE_CLASS : DOCUMENT_CLASS} data-markdown-face={face}>
<ReactMarkdown
remarkPlugins={[remarkGfm]}
rehypePlugins={[[rehypeSanitize, RICH_TEXT_SANITIZE_SCHEMA]]}
components={cell ? CELL_COMPONENTS : undefined}
>
{value}
</ReactMarkdown>
</div>
Expand Down
10 changes: 8 additions & 2 deletions ui/packages/fields/src/widgets/richTextDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,19 @@ function richTextSource(value: unknown): string | undefined {
* What the cell holds is decided by {@link richTextSource} (objectui#8580):
* `[]` is the No-value affordance, never a childless container, and an
* object is the text the string class prints for it, never `[object Object]`.
*
* In a grid column (`inGridColumn`) the value is drawn in the `cell` face
* (objectstack#36344): a heading is a bold line at the cell's own text size,
* so a value that starts with `# Heading` does not grow its row. Every other
* caller — the record page, a readonly form field — keeps the `document`
* face and its heading sizes.
*/
export function MarkdownCellRenderer({ value }: CellRendererProps): React.ReactElement {
export function MarkdownCellRenderer({ value, inGridColumn }: CellRendererProps): React.ReactElement {
const text = richTextSource(value);
if (text === undefined) return <EmptyValue />;
return (
<React.Suspense fallback={<span className="text-sm text-muted-foreground">{text.slice(0, 80)}</span>}>
<LazyMarkdownContent value={text} />
<LazyMarkdownContent value={text} face={inGridColumn ? 'cell' : 'document'} />
</React.Suspense>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,36 @@ describe('objectui#12480 — a rich-content cell keeps the one-line row', () =>
expect(screen.getByText('Account 8').closest('td')?.querySelector('.truncate')).not.toBeNull();
});
});

/**
* objectstack#36344 — the one line the clamp shows is a line of the cell's
* text. A markdown value that starts with `# Heading` filled the clamp's one
* line with an H1, so the row grew to the heading's line height. The grid
* passes `inGridColumn` to every column cell, and the markdown cell then draws
* its value in the `cell` face: no heading element, the cell's own font size
* and line height. jsdom does no layout; the pixel readings are in the PR.
*/
describe('objectstack#36344 — a markdown heading in a grid cell does not draw at heading size', () => {
it('a markdown cell whose value starts with a heading draws no heading element inside the clamp', async () => {
desktop();
const rows = [
{ id: 'r1', name: 'Account 1', description: '# Heading\n\nBody' },
{ id: 'r2', name: 'Account 2' },
];
renderGrid(rows, [
{ field: 'name', label: 'Name' },
{ field: 'description', label: 'Description' },
]);
await waitFor(() => expect(screen.getByText('Account 2')).toBeInTheDocument());
const row1 = bodyRows()[0] as HTMLElement;
const face = await waitFor(() => {
const el = row1.querySelector<HTMLElement>('[data-testid="grid-rich-text-one-line"] [data-markdown-face]');
if (!el) throw new Error('markdown cell not rendered yet');
return el;
});
expect(face).toHaveAttribute('data-markdown-face', 'cell');
expect(face).toHaveClass('text-[length:inherit]', 'leading-[inherit]');
expect(face.querySelector('[data-markdown-heading]')).toHaveTextContent('Heading');
expect(document.querySelector('tbody h1, tbody h2, tbody h3, tbody h4, tbody h5, tbody h6')).toBeNull();
});
});
Loading