From cfe9c42c54faca84e1f82ba4aa9e63fb942a0e8a Mon Sep 17 00:00:00 2001 From: yellowtailfan <36425185+yellowtailfan@users.noreply.github.com> Date: Thu, 1 May 2025 11:41:13 +1000 Subject: [PATCH 1/3] Add maxBodyLength parameter to htmlToMarkdown. --- lib/ExpensiMark.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/ExpensiMark.ts b/lib/ExpensiMark.ts index 04ac6023..ce1ab4a8 100644 --- a/lib/ExpensiMark.ts +++ b/lib/ExpensiMark.ts @@ -1224,7 +1224,7 @@ export default class ExpensiMark { /** * Replaces HTML with markdown */ - htmlToMarkdown(htmlString: string, extras: Extras = EXTRAS_DEFAULT): string { + htmlToMarkdown(htmlString: string, extras: Extras = EXTRAS_DEFAULT, maxBodyLength = 0): string { let generatedMarkdown = htmlString; const body = /<(body)(?:"[^"]*"|'[^']*'|[^'"><])*>(?:\n|\r\n)?([\s\S]*?)(?:\n|\r\n)?<\/\1>(?![^<]*(<\/pre>|<\/code>))/im; const parseBodyTag = generatedMarkdown.match(body); @@ -1233,6 +1233,11 @@ export default class ExpensiMark { if (parseBodyTag) { generatedMarkdown = parseBodyTag[2]; } + if (maxBodyLength > 0) { + // Truncate the text after extracting the body tag, as some HTML sources (such as Microsoft Word) + // have headers larger than the typical maxLength of 10K even for a small body text. + generatedMarkdown = generatedMarkdown.slice(0, maxBodyLength); + } generatedMarkdown = this.unpackNestedQuotes(generatedMarkdown); const processRule = (rule: RuleWithRegex) => { From 12ba7c4cf1e433518143728d92ea6dca401dccb3 Mon Sep 17 00:00:00 2001 From: yellowtailfan <36425185+yellowtailfan@users.noreply.github.com> Date: Fri, 2 May 2025 19:10:51 +1000 Subject: [PATCH 2/3] Add long header test. --- __tests__/ExpensiMark-Markdown-test.js | 759 +++++++++++++++++++++++++ 1 file changed, 759 insertions(+) diff --git a/__tests__/ExpensiMark-Markdown-test.js b/__tests__/ExpensiMark-Markdown-test.js index 0a70d3de..752a2c62 100644 --- a/__tests__/ExpensiMark-Markdown-test.js +++ b/__tests__/ExpensiMark-Markdown-test.js @@ -968,3 +968,762 @@ describe('Tag names starting with common charaters', () => { expect(parser.htmlToMarkdown(testString)).toBe(resultString); }); }); + +test('Test long HTML header', () => { + const testStringDoc = ` + + + + + + + + + + + + + + + + + + + +

Normal text body text

+ +

Line 2

+ + + + + + `; + const resultString = '\n\nNormal text *body text*\n\n\n*Line 2*\n\n\n'; + + expect(parser.htmlToMarkdown(testStringDoc, {}, 500)).toBe(resultString); +}); From 1caed11f6c02ae439bd2d3a7c8f5c3a765d588f6 Mon Sep 17 00:00:00 2001 From: yellowtailfan <36425185+yellowtailfan@users.noreply.github.com> Date: Mon, 5 May 2025 16:43:02 +1000 Subject: [PATCH 3/3] Revise comment. --- lib/ExpensiMark.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/ExpensiMark.ts b/lib/ExpensiMark.ts index ce1ab4a8..22bbc2fe 100644 --- a/lib/ExpensiMark.ts +++ b/lib/ExpensiMark.ts @@ -1234,8 +1234,14 @@ export default class ExpensiMark { generatedMarkdown = parseBodyTag[2]; } if (maxBodyLength > 0) { - // Truncate the text after extracting the body tag, as some HTML sources (such as Microsoft Word) - // have headers larger than the typical maxLength of 10K even for a small body text. + /* + * Some HTML sources (such as Microsoft Word) have headers larger than the typical maxLength of + * 10K even for a small body text. So the text is truncated after extracting the body element, to + * maximise the amount of body text that is included while still staying inside the length limit. + * + * The truncation happens before HTML to Markdown conversion, as the conversion is very slow for + * large input especially on mobile devices. + */ generatedMarkdown = generatedMarkdown.slice(0, maxBodyLength); } generatedMarkdown = this.unpackNestedQuotes(generatedMarkdown);