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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "html-textify",
"version": "1.0.1",
"version": "1.0.2",
"description": "Convert html to plain text",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down
14 changes: 8 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,17 @@ export function textify({
}: TextifyOptions): string {
if (!html) return '';

let output = html;

// Strip or preserve HTML formatting
if (preserveFormatting) {
html = preserveFormat({ html, ignoreTags });
output = preserveFormat({ html: output, ignoreTags });
} else {
if (ignoreTags.length === 0) {
html = html.replace(/<[^>]+>/g, '').trim();
output = output.replace(/<[^>]+>/g, '').trim();
} else {
const IG = new Set(ignoreTags.map((t) => t.toLowerCase()));
html = html
output = output
.replace(/<\/?([a-z][a-z0-9-]*)\b[^>]*>/gi, (match, tag) =>
IG.has(tag.toLowerCase()) ? match : ''
)
Expand All @@ -60,10 +62,10 @@ export function textify({

// Wrap output text (word-based wrapping takes priority)
if (wrapWords && wrapWords > 0) {
html = wrapByWords(html, wrapWords);
output = wrapByWords(output, wrapWords);
} else if (wrapLength && wrapLength > 0) {
html = wrapByLength(html, wrapLength);
output = wrapByLength(output, wrapLength);
}

return html;
return output;
}
5 changes: 5 additions & 0 deletions src/utils/preserveFormat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ describe('preserveFormat', () => {
);
});

it('should handle nested tags', () => {
const html = '<b><i>text</i></b>';
expect(preserveFormat({ html })).toBe('***text***');
});

it('should handle complex nested content', () => {
const html = `<h1>Main Heading</h1>
<p>Paragraph with <b>bold</b> and <i>italic</i> text</p>
Expand Down
2 changes: 1 addition & 1 deletion src/utils/preserveFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface PreserveFormatOptions {
* - Converts `<br>` to newline.
* - Wraps bold (`<b>`, `<strong>`) in `**`.
* - Wraps italic (`<i>`, `<em>`) in `*`.
* - Converts links `<a href="...">text</a>` to `text (url)`.
* - Converts links `<a href="...">text</a>` to `[text] (url)`.
* - Formats lists (`<ol>`, `<ul>`) and list items.
* - Formats blockquotes (`<blockquote>`) with `> ` prefix.
* - Converts tables to tab-delimited rows.
Expand Down
3 changes: 3 additions & 0 deletions src/utils/wrapByWords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
* // => "one two\nthree four\nfive"
*/
export function wrapByWords(text: string, count: number): string {
if (count <= 0) {
throw new Error('wrap count must be greater than 0');
}
const words = text.trim().split(/\s+/);
const lines: string[] = [];

Expand Down