Skip to content
Draft
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
72 changes: 72 additions & 0 deletions __tests__/transformers/component-body-html-indent.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import type { Html, List } from 'mdast';

import { collectNodes, parseMdxishWithSlices, roundTripMdxish } from '../helpers';

describe('component body HTML indentation (RM-19815)', () => {
it.each([
[
'first-line comment',
'<Callout icon="📘" theme="info">\n <!-- note --> Some text\n</Callout>',
'<!-- note --> Some text',
],
['HTML after a paragraph', '<Callout>\n Hello\n\n <div>x</div>\n</Callout>', '<div>x</div>'],
[
'nested component',
'<Tabs>\n <Tab title="One">\n <!-- note --> Text\n </Tab>\n</Tabs>',
'<!-- note --> Text',
],
['component in a list', '- <Callout>\n <!-- note --> Text\n </Callout>', '<!-- note --> Text'],
['HTML in a list', '<Callout>\n - item\n\n <!-- note --> Text\n</Callout>', '<!-- note --> Text'],
['one space', '<Tab>\n <!-- note -->\n</Tab>', '<!-- note -->'],
['three spaces', '<Tab>\n <!-- note -->\n</Tab>', '<!-- note -->'],
['opener line', '<Callout> <!-- note --> Text\n</Callout>', '<!-- note --> Text'],
])('normalizes %s and keeps its page position', (_name, markdown, expected) => {
for (const newEditorTypes of [false, true]) {
const { tree, sliceOf } = parseMdxishWithSlices(markdown, { newEditorTypes });
const [html] = collectNodes<Html>(tree, 'html');

expect(html?.value).toBe(expected);
expect(sliceOf(html)).toBe(expected);
}
});

it('preserves indentation inside multiline HTML', () => {
const markdown = '<Callout>\n <div>\n <span>x</span>\n </div>\n</Callout>';
const { tree } = parseMdxishWithSlices(markdown);
const [html] = collectNodes<Html>(tree, 'html');

expect(html?.value).toBe('<div>\n <span>x</span>\n</div>');
});

it('preserves internal HTML whitespace below the deindent gate', () => {
const markdown = '<Callout>\n <pre>\n x\n </pre>\n</Callout>';
const { tree } = parseMdxishWithSlices(markdown);
const [html] = collectNodes<Html>(tree, 'html');

expect(html?.value).toBe('<pre>\n x\n </pre>');
});

it('does not normalize HTML outside the component body', () => {
const markdown = '<Callout>Text</Callout>\n\n <div>x</div>';
const { tree } = parseMdxishWithSlices(markdown);
const [html] = collectNodes<Html>(tree, 'html');

expect(html?.value).toBe(' <div>x</div>');
});

it('does not accumulate indentation across saves', () => {
const markdown = '<Callout>\n Hello\n\n <div>x</div>\n</Callout>\n';
const once = roundTripMdxish(markdown);

expect(once).toBe(markdown);
expect(roundTripMdxish(once)).toBe(once);
});

it('preserves genuinely nested list items', () => {
const markdown = '<Callout>\n - one\n - nested\n - two\n\n <!-- note --> Text\n</Callout>';
const { tree } = parseMdxishWithSlices(markdown);
const lists = collectNodes<List>(tree, 'list');

expect(lists.map(list => list.children.length)).toStrictEqual([2, 1]);
});
});
10 changes: 9 additions & 1 deletion processor/plugin/mdxish-handlers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { HTMLBlock, MdxJsx } from '../../types';
import type { Properties } from 'hast';
import type { ListItem } from 'mdast';
import type { Html, ListItem } from 'mdast';
import type { MdxJsxAttribute, MdxJsxAttributeValueExpression } from 'mdast-util-mdx-jsx';
import type { Handler, Handlers } from 'mdast-util-to-hast';

Expand All @@ -11,6 +11,13 @@ import { NodeTypes } from '../../enums';

const HTML_COMMENT_REGEX = /^<!--[\s\S]*-->$/;

// Restore the source indent for rendering, where mixed HTML/component children use it as whitespace.
const htmlHandler: Handler = (state, node) => {
const html = node as Html;
const indent = html.data?.componentBodyHtmlIndent;
return defaultHandlers.html(state, typeof indent === 'string' ? { ...html, value: indent + html.value } : html);
};

// If hChildren is populated, it means the node holds a renderable value (e.g. React)
// See evaluate-expressions.ts for more details
// Otherwise, it's a simple value and we fall back to text
Expand Down Expand Up @@ -137,6 +144,7 @@ const listItemHandler: Handler = (state, node, parent) => {

export const mdxComponentHandlers: Handlers = {
embed: embedHandler,
html: htmlHandler,
listItem: listItemHandler,
mdxFlowExpression: mdxExpressionHandler,
mdxJsxFlowElement: mdxJsxElementHandler,
Expand Down
21 changes: 20 additions & 1 deletion processor/transform/mdxish/components/mdx-blocks.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import type { Node, Parent, RootContent } from 'mdast';
import type { Html, Node, Parent, RootContent } from 'mdast';
import type { MdxJsxAttribute, MdxJsxFlowElement } from 'mdast-util-mdx-jsx';
import type { Plugin } from 'unified';

import { visit } from 'unist-util-visit';

import { GENERIC_MDX_COMPONENT_EXCLUDED_TAGS, INLINE_ONLY_PARENT_TYPES } from '../../../../lib/constants';
import { type ParseAttributesOptions, parseTag } from '../../../../lib/utils/mdxish/mdxish-component-tag-parser';
import { expandIndentToColumns, leadingIndent } from '../indentation';
import { containsRawHtmlBlock } from '../mdxish-html-blocks';
import {
createNodeValueToDocumentMapper,
getDocumentSource,
Expand Down Expand Up @@ -107,6 +110,21 @@ const parseMdChildren = (fragment: MarkdownFragment, safeMode: boolean, document
return children;
};

/** Remove the Markdown block indent from HTML without changing its interior whitespace. */
const normalizeBodyHtmlIndent = (children: MdxJsxFlowElement['children'], documentSource: string) => {
children.forEach(child =>
visit(child, 'html', (node: Html) => {
const indent = leadingIndent(node.value).length;
// HTMLBlock's raw wrapper and its indentation are consumed by its own transformer.
if (!indent || containsRawHtmlBlock(node.value)) return;
const position = createNodeValueToDocumentMapper(node, documentSource)?.(indent, node.value.length);
node.data = { ...node.data, componentBodyHtmlIndent: node.value.slice(0, indent) };
node.value = node.value.slice(indent);
if (position) node.position = position;
}),
);
};

// Splices trailing content in as sibling nodes. parseMdChildren has already
// promoted any components nested among them (bottom-up); the main loop's
// index-based walk then reaches these spliced siblings and the original children
Expand Down Expand Up @@ -299,6 +317,7 @@ function promoteComponentBlocks(tree: Parent, safeMode: boolean, documentSource:
try {
const body = { parentNode, offsetInParentNode: bodyStart, rawText: componentInnerContent };
parsedChildren = parseMdChildren(body, safeMode, documentSource) as MdxJsxFlowElement['children'];
normalizeBodyHtmlIndent(parsedChildren, documentSource);
} catch (error) {
// Plain HTML bodies can hold anything (e.g. stray braces the strict
// expression parser rejects) — keep the node raw instead of throwing.
Expand Down
3 changes: 3 additions & 0 deletions processor/transform/mdxish/mdxish-html-blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ type HtmlBlockJsx = MdxJsxFlowElement | MdxJsxTextElement;
// node, so the tokenizer never sees the HTMLBlock — we recover it here.
const RAW_HTML_BLOCK_RE = /<HTMLBlock\b([^>]*)>\s*\{\s*`((?:[^`\\]|\\.)*)`\s*\}\s*<\/HTMLBlock>/g;

/** Whether the raw HTML transformer will consume an embedded HTMLBlock. */
export const containsRawHtmlBlock = (value: string): boolean => value.search(RAW_HTML_BLOCK_RE) !== -1;

/**
* Builds the canonical `html-block` MDAST node the renderer expects.
*/
Expand Down
4 changes: 4 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ export interface NoTranslate extends Node {
}

declare module 'mdast' {
interface HtmlData {
componentBodyHtmlIndent?: string;
}

interface Data {
/** Set by a dedicated tokenizer (`lib/micromark/no-translate`) on an html node that holds a whole tag. */
tokenized?: boolean;
Expand Down