diff --git a/.changeset/soft-break-after-button.md b/.changeset/soft-break-after-button.md new file mode 100644 index 000000000..33fa72012 --- /dev/null +++ b/.changeset/soft-break-after-button.md @@ -0,0 +1,5 @@ +--- +"gitbook": patch +--- + +Render soft line breaks in paragraphs that contain buttons. diff --git a/packages/gitbook/src/components/DocumentView/Text.tsx b/packages/gitbook/src/components/DocumentView/Text.tsx index f70a69782..4685b5f6e 100644 --- a/packages/gitbook/src/components/DocumentView/Text.tsx +++ b/packages/gitbook/src/components/DocumentView/Text.tsx @@ -38,12 +38,21 @@ export function Text(props: { text: DocumentText }) { {children} ); - }, leaf.text)} + }, withSoftBreaks(leaf.text))} ); }); } +// Paragraphs with buttons are flex containers, where a `\n` alone can't move the next items to a new line. +function withSoftBreaks(text: string): React.ReactNode { + const lines = text.split('\n'); + // Keep `\n` in the same text node as a preceding `\r`: split apart, the HTML parser reads the lone `\r` as an extra line. + return lines.flatMap((line, index) => + index < lines.length - 1 ? [`${line}\n`, ] : line + ); +} + const MARK_STYLES = { bold: Bold, italic: Italic,