-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(textversion): migrate from textversionjs to html-to-text (#86)
* feat(textversion): migrate from textversion to html-to-text Resolves #85 Signed-off-by: Niloy Sikdar <niloysikdar30@gmail.com> * refactor(textemail): refactor the headingFormatter for genearting the plain text Create a headingFormatter function and pass the level for each heading type for formatting Resolves #85 Signed-off-by: Niloy Sikdar <niloysikdar30@gmail.com> Signed-off-by: Niloy Sikdar <niloysikdar30@gmail.com>
- Loading branch information
1 parent
b124cd2
commit 18c4b11
Showing
8 changed files
with
133 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/utils/generateTextEmailFromHTML/generateTextEmailFromHTML.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 50 additions & 2 deletions
52
src/utils/generateTextEmailFromHTML/generateTextEmailFromHTML.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,54 @@ | ||
import htmlToPlainText from 'textversionjs'; | ||
import { convert, DomNode, FormatOptions, RecursiveCallback } from 'html-to-text'; | ||
import { BlockTextBuilder } from 'html-to-text/lib/block-text-builder'; | ||
|
||
const headingFormatter = | ||
(level: number) => | ||
( | ||
elem: DomNode, | ||
walk: RecursiveCallback, | ||
builder: BlockTextBuilder, | ||
formatOptions: FormatOptions, | ||
) => { | ||
builder.openBlock({ leadingLineBreaks: formatOptions.leadingLineBreaks || 1 }); | ||
builder.addInline('#'.repeat(level) + ' '); | ||
walk(elem.children, builder); | ||
builder.closeBlock({ trailingLineBreaks: formatOptions.trailingLineBreaks || 1 }); | ||
}; | ||
|
||
export const generateTextEmailFromHTML = (html: string): string => { | ||
const plainText = htmlToPlainText(html, { headingStyle: 'hashify' }); | ||
const plainText = convert(html, { | ||
formatters: { | ||
h1Formatter: headingFormatter(1), | ||
h2Formatter: headingFormatter(2), | ||
h3Formatter: headingFormatter(3), | ||
h4Formatter: headingFormatter(4), | ||
h5Formatter: headingFormatter(5), | ||
h6Formatter: headingFormatter(6), | ||
captionFormatter: function (elem, walk, builder) { | ||
builder.openBlock({ leadingLineBreaks: 1 }); | ||
walk(elem.children, builder); | ||
builder.closeBlock({ trailingLineBreaks: 1 }); | ||
}, | ||
quoteFormatter: function (elem, walk, builder, formatOptions) { | ||
builder.openBlock({ leadingLineBreaks: formatOptions.leadingLineBreaks || 1 }); | ||
builder.addInline('> '); | ||
walk(elem.children, builder); | ||
builder.closeBlock({ trailingLineBreaks: formatOptions.trailingLineBreaks || 1 }); | ||
}, | ||
}, | ||
selectors: [ | ||
{ selector: '#preheader', format: 'skip' }, | ||
{ selector: 'h1', format: 'h1Formatter', options: { uppercase: false } }, | ||
{ selector: 'h2', format: 'h2Formatter', options: { uppercase: false } }, | ||
{ selector: 'h3', format: 'h3Formatter', options: { uppercase: false } }, | ||
{ selector: 'h4', format: 'h4Formatter', options: { uppercase: false } }, | ||
{ selector: 'h5', format: 'h5Formatter', options: { uppercase: false } }, | ||
{ selector: 'h6', format: 'h6Formatter', options: { uppercase: false } }, | ||
{ selector: 'table', options: { uppercaseHeaderCells: false } }, | ||
{ selector: '#imageCaption', format: 'captionFormatter' }, | ||
{ selector: '#quote', format: 'quoteFormatter' }, | ||
], | ||
}); | ||
|
||
return plainText; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters