diff --git a/package.json b/package.json index 0f6c4d7..529272c 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.ts b/src/index.ts index 044e3b2..c9eaaeb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 : '' ) @@ -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; } diff --git a/src/utils/preserveFormat.test.ts b/src/utils/preserveFormat.test.ts index 27a3ebe..c395159 100644 --- a/src/utils/preserveFormat.test.ts +++ b/src/utils/preserveFormat.test.ts @@ -105,6 +105,11 @@ describe('preserveFormat', () => { ); }); + it('should handle nested tags', () => { + const html = 'text'; + expect(preserveFormat({ html })).toBe('***text***'); + }); + it('should handle complex nested content', () => { const html = `

Main Heading

Paragraph with bold and italic text

diff --git a/src/utils/preserveFormat.ts b/src/utils/preserveFormat.ts index 07524e3..e86ecc8 100644 --- a/src/utils/preserveFormat.ts +++ b/src/utils/preserveFormat.ts @@ -9,7 +9,7 @@ interface PreserveFormatOptions { * - Converts `
` to newline. * - Wraps bold (``, ``) in `**`. * - Wraps italic (``, ``) in `*`. - * - Converts links `text` to `text (url)`. + * - Converts links `text` to `[text] (url)`. * - Formats lists (`
    `, `
      `) and list items. * - Formats blockquotes (`
      `) with `> ` prefix. * - Converts tables to tab-delimited rows. diff --git a/src/utils/wrapByWords.ts b/src/utils/wrapByWords.ts index 1117671..1a5f73c 100644 --- a/src/utils/wrapByWords.ts +++ b/src/utils/wrapByWords.ts @@ -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[] = [];