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