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
4 changes: 2 additions & 2 deletions packages/utils/src/internals/cheerio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type { Element } from 'domhandler';
// NOTE: We are skipping 'noscript' since it's content is evaluated as text, instead of HTML elements. That damages the results.
const SKIP_TAGS_REGEX = /^(script|style|canvas|svg|noscript)$/i;
const BLOCK_TAGS_REGEX =
/^(p|h1|h2|h3|h4|h5|h6|ol|ul|li|pre|address|blockquote|dl|div|fieldset|form|table|tr|select|option)$/i;
/^(p|h1|h2|h3|h4|h5|h6|ol|ul|li|pre|address|blockquote|dl|dt|dd|div|fieldset|legend|form|table|caption|tr|select|option|hr|article|aside|section|nav|header|footer|main|hgroup|figure|figcaption|details|summary|dialog|menu)$/i;

/**
* The function converts a HTML document to a plain text.
Expand Down Expand Up @@ -73,7 +73,7 @@ export async function htmlToText(htmlOrCheerioElement: string | CheerioAPI): Pro
// Skip comments and special elements
} else if (elem.tagName === 'br') {
push('\n');
} else if (elem.tagName === 'td') {
} else if (elem.tagName === 'td' || elem.tagName === 'th') {
process(elem.children, inPre);
push('\t');
} else {
Expand Down
22 changes: 22 additions & 0 deletions test/utils/cheerio.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,28 @@ describe('htmlToText()', () => {
);
});

test('separates HTML5 block elements and table header cells', async () => {
await checkHtmlToText(
'<section>Call 555-1234</section><section>9876 Main St</section>',
'Call 555-1234\n9876 Main St',
);
await checkHtmlToText(
'<header>Head</header><nav>Menu</nav><main><article>One</article><article>Two</article></main><aside>Side</aside><footer>Foot</footer>',
'Head\nMenu\nOne\nTwo\nSide\nFoot',
);
await checkHtmlToText('A<hr>B', 'A\nB');
await checkHtmlToText('<dl><dt>Term</dt><dd>Definition</dd></dl>', 'Term\nDefinition');
await checkHtmlToText(
'<figure><img src="x.png"><figcaption>Caption</figcaption></figure>After',
'Caption\nAfter',
);
await checkHtmlToText('<details><summary>Summary</summary>Details</details>', 'Summary\nDetails');
await checkHtmlToText(
'<table><tr><th>Name</th><th>Phone</th></tr><tr><td>Alice</td><td>555-1234</td></tr></table>',
'Name\tPhone\t\nAlice\t555-1234',
);
});

test('handles HTML entities correctly', async () => {
await checkHtmlToText('<span>&aacute; &eacute;</span>', 'á é');
});
Expand Down
Loading