Skip to content

Preserve line breaks when copying table cells containing <br> elements#554

Open
aradhyacp wants to merge 6 commits into
vercel:mainfrom
aradhyacp:fix/table-copy
Open

Preserve line breaks when copying table cells containing <br> elements#554
aradhyacp wants to merge 6 commits into
vercel:mainfrom
aradhyacp:fix/table-copy

Conversation

@aradhyacp

Copy link
Copy Markdown
Contributor

Description

Fixes an issue where copying rendered Markdown tables lost line breaks inside cells containing <br> elements.

Previously, table extraction relied on textContent, which removed <br> elements without preserving them as newline characters. This caused multiline cell content to be merged together when copying tables as Markdown, CSV, or TSV.

This change preserves line breaks during table extraction so copied table content maintains the original formatting.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Performance improvement
  • Refactoring (no functional changes)

Related Issues

Fixes #553
Closes #553
Related to #553

Changes Made

  • Replaced textContent based table cell extraction with a recursive text extraction helper.
  • Preserved <br> elements by converting them into newline characters during extraction.
  • Ensured nested elements containing line breaks are handled correctly.
  • Improved copied table output consistency across Markdown, CSV, and TSV formats.
  • Added test coverage for single, multiple, and nested <br> elements.

Testing

  • All existing tests pass
  • Added new tests for the changes
  • Manually tested the changes

Test Coverage

Added tests covering:

  • Converting a single <br> element into a newline.
  • Converting multiple <br> elements into newlines.
  • Preserving <br> elements inside nested elements.

Manually tested the copy functionality using a local React app.

How I tested it locally:

I used the following test component to verify table rendering and copying behavior:

import { Streamdown } from "streamdown";
import "streamdown/styles.css";

const App = () => {
  const markdown = `
  | Name | Description |
  | --- | --- |
  | Example | Paragraph one.<br>Paragraph two. |
  | Example | Paragraph one.<br>Paragraph two. |
  | Example | Paragraph one.<br>Paragraph two. |
  | Example | Paragraph one.<br>Paragraph two. |
  | Example | Paragraph one.<br>Paragraph two. |
  `;

  return (
    <div className="App">
      <div className="mx-auto mt-5 w-200">
        <Streamdown>{markdown}</Streamdown>
      </div>
    </div>
  );
};

export default App;

Screenshots/Demos

Before Markdown Copy

image

After Markdown Copy

image

Before CSV Copy

image

After CSV Copy

image

Before TSV Copy

image

After TSV Copy

image

Checklist

  • My code follows the project's code style
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings or errors
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have created a changeset (pnpm changeset)

Changeset

  • I have created a changeset for these changes

Additional Notes

This change only affects table text extraction behavior. The existing Markdown, CSV, and TSV serializers continue to handle formatting based on the extracted table data.

- Implemented a new function `extractCellText` to accurately extract text from table cells, handling different node types and line breaks.
- Updated `extractTableDataFromElement` to utilize `extractCellText` for header and row cell data extraction.
- Modified `escapeMarkdownTableCell` to escape newline characters by converting them to `<br>` tags for proper markdown formatting.
- Introduced a new test suite to verify the preservation of line breaks when copying table data as Markdown, CSV, and TSV.
- Ensured that `<br>` tags are correctly handled and do not merge cell text across line breaks.
- Mocked clipboard functionality to validate the output format for different copy options.
…ells

- Added tests to verify that <br> elements in table cells are correctly converted to newlines.
- Included scenarios for handling <br> in nested elements and multiple <br> tags.
- Ensured that newlines in table data are converted back to <br> when generating Markdown output.
@vercel

vercel Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

@aradhyacp is attempting to deploy a commit to the Vercel Team on Vercel.

A member of the Team first needs to authorize it.

- Streamlined the `extractCellText` function by removing unnecessary line breaks in the return statement, improving code readability.
- Added checks to ensure that the `blobPartsByType["text/plain"]` is defined in multiple test cases.
- Improved the `getClipboardPlainText` function to handle optional chaining for better safety.
- Reformatted the HTML structure in the test setup for clarity and consistency.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Table copy drops line breaks from cells containing <br>

1 participant