Skip to content

Commit 1b10ee9

Browse files
committed
fix(markdown): keep inline HTML after a line break inside its paragraph
A line break followed by inline HTML that can start an HTML block, such as a comment or <div>, was saved with that HTML at the start of a line, so it read back as a separate raw HTML block. Such a line is now indented by four spaces: an indented line cannot start an HTML block, indented code cannot interrupt a paragraph, and the spaces are stripped. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01M15YF742ggoWPyh9ZzZ1Jm
1 parent 2915ea4 commit 1b10ee9

4 files changed

Lines changed: 30 additions & 1 deletion

File tree

‎docs/syntax.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,8 @@ The serializer escapes only characters that would otherwise change meaning:
353353
- characters an extension lists in `escape` (`math()` lists `$`);
354354
- at the start of a line, what would start a block: `#` followed by a space or ending the text, `>`, `-` or `+` followed by a space, `1.`, and so on.
355355

356+
Inline HTML cannot be escaped. When a line break puts HTML that would start an HTML block (a comment, or a tag such as `<div>`) at the start of a line, that line is indented by four spaces, which keeps it in the paragraph. The spaces are not part of the text.
357+
356358
`#`, `@`, `:` and `=` in running text are not escaped. Where an extension reads a backslash escape as its own token (`tags()` reads `\#`), the serializer writes a character reference instead, such as `&#35;`.
357359

358360
### Inline typing shortcuts

‎packages/editor/src/markdown/blocks.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ const reHtmlBlockOpen: readonly RegExp[] = [
4242
new RegExp(`^(?:${OPENTAG}|${CLOSETAG})[ \\t]*$`, 'i'),
4343
];
4444

45+
/** True when `text` at the start of a line would start an HTML block that can interrupt a paragraph (types 1 to 6). */
46+
export function interruptsParagraph(text: string): boolean {
47+
for (let type = 1; type <= 6; type++) if (reHtmlBlockOpen[type]!.test(text)) return true;
48+
return false;
49+
}
50+
4551
const reHtmlBlockClose: readonly RegExp[] = [
4652
/^$/,
4753
/<\/(?:script|pre|textarea|style)>/i,

‎packages/editor/src/markdown/serialize.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import {
4242
deleteInline,
4343
} from '../model';
4444
import { imageSuffix, imageSuffixText } from './ast';
45+
import { interruptsParagraph } from './blocks';
4546
import { AUTOLINK_EMAIL, AUTOLINK_URI, isUnicodePunctuation, isUnicodeWhitespace, parseInline } from './inline';
4647
import { emailLiterals, isLiteralBoundary, literalHref, urlLiteralAt } from './autolink';
4748
import { type InlineSyntax, type MarkdownOptions, type ResolvedMarkdown, resolveMarkdown } from './options';
@@ -630,7 +631,13 @@ function render(spans: readonly Span[], ctx: InlineContext, strategy: Strategy,
630631
let out = '';
631632
tokens.forEach((tok, i) => {
632633
if (tok.kind === 'delim') out += tok.ch.repeat(tok.count);
633-
else if (tok.kind === 'raw') out += tok.text;
634+
else if (tok.kind === 'raw') {
635+
// Inline HTML such as a comment, first on a line after a line break, would start an HTML
636+
// block there. Four spaces keep the line in the paragraph: an indented line cannot start an
637+
// HTML block, indented code cannot interrupt a paragraph, and the spaces are stripped.
638+
if (ctx.lineStart && out.endsWith('\n') && interruptsParagraph(tok.text)) out += ' ';
639+
out += tok.text;
640+
}
634641
else {
635642
out += escapeText(tok, ctx, {
636643
before: out ? out[out.length - 1] : undefined,

‎packages/editor/test/roundtrip.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,20 @@ describe('serializer conventions', () => {
348348
expect(md(table([null], [[[t('a|b', 'code')]], [[t(' x ')]]]))).toBe('| `a\\|b` |\n| --- |\n| x |\n');
349349
});
350350

351+
it('indents inline HTML that would start an HTML block after a line break, so it stays inline', () => {
352+
const content = [t('a\n'), t('<!-- c -->', 'html'), t(' b\n'), t('<div>', 'html'), t('\n'), t('<b>', 'html')];
353+
for (const options of [{}, { breaks: true }]) {
354+
const text = serializeMarkdown({ blocks: [paragraph(content)] }, options);
355+
expect(text).toContain('\n <!-- c --> b');
356+
expect(text).toContain('\n <div>');
357+
// A tag that cannot interrupt a paragraph is left at the start of its line.
358+
expect(text).toContain('\n<b>');
359+
expect(shape(parseMarkdown(text, options))).toEqual([{ type: 'paragraph', content }]);
360+
}
361+
const item = serializeMarkdown({ blocks: [listItem('bullet', content)] });
362+
expect(shape(parseMarkdown(item))).toMatchObject([{ type: 'listItem', listType: 'bullet', indent: 0, content }]);
363+
});
364+
351365
it('writes a pipe after an odd number of backslashes in a code span outside it, in a table cell', () => {
352366
// GFM splits cells first: `\\|` would end the cell and `\\\|` would read back as two backslashes.
353367
const cell = (text: string) => parseMarkdown(md(table([null], [[[t(text, 'code')]]])), {}).blocks[0];

0 commit comments

Comments
 (0)