-
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(utils): add text email generation utility methods (#65)
* feat(utils): add generateTextEmailFromHTML Add generateTextEmailFromHTML utility function to convert HTML to plaintext Resolves #60 Signed-off-by: Niloy Sikdar <niloysikdar30@gmail.com> * feat(utils): add generateTextEmail Add generateTextEmail utility function to convert JSX Layout to plaintext Resolves #60 Signed-off-by: Niloy Sikdar <niloysikdar30@gmail.com> * test(utils): add tests for generateTextEmail Add some more missing tests for all of the components for generateTextEmail method Resolves #60 Signed-off-by: Niloy Sikdar <niloysikdar30@gmail.com>
- Loading branch information
1 parent
e8ee676
commit 740c089
Showing
9 changed files
with
96 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { generateTextEmail } from './generateTextEmail'; | ||
import { Email, Section, Column, Typography, Button, Image } from '../../components'; | ||
|
||
describe('generateTextEmail', () => { | ||
test('should return expected text from the JSX Layout', () => { | ||
const text = generateTextEmail( | ||
<Email> | ||
<Section> | ||
<Column> | ||
<Typography variant="h1">Hello World</Typography> | ||
</Column> | ||
</Section> | ||
</Email>, | ||
); | ||
|
||
expect(text.trim()).toBe('# Hello World'); | ||
}); | ||
|
||
test('should return expected text from the Button component', () => { | ||
const text = generateTextEmail( | ||
<Email> | ||
<Section> | ||
<Column> | ||
<Button href="https://github.com/leopardslab/react-email">Click Here</Button> | ||
</Column> | ||
</Section> | ||
</Email>, | ||
); | ||
|
||
expect(text.trim()).toBe('[Click Here] (https://github.com/leopardslab/react-email)'); | ||
}); | ||
|
||
test('should return expected text from the Image component', () => { | ||
const text = generateTextEmail( | ||
<Email> | ||
<Section> | ||
<Column> | ||
<Image | ||
src="https://images.unsplash.com/photo-1453728013993-6d66e9c9123a" | ||
alt="Alt text" | ||
width="500px" | ||
caption="This is a caption" | ||
/> | ||
</Column> | ||
</Section> | ||
</Email>, | ||
); | ||
|
||
const expectedText = `![Alt text] (https://images.unsplash.com/photo-1453728013993-6d66e9c9123a)\nThis is a caption`; | ||
|
||
expect(text.trim()).toBe(expectedText); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { generateEmail } from '../generateEmail'; | ||
import { generateTextEmailFromHTML } from '../generateTextEmailFromHTML'; | ||
|
||
export const generateTextEmail = (jsxElement: JSX.Element): string => { | ||
const HTML = generateEmail(jsxElement); | ||
const plainText = generateTextEmailFromHTML(HTML); | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { generateTextEmail } from './generateTextEmail'; |
11 changes: 11 additions & 0 deletions
11
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { generateTextEmailFromHTML } from './generateTextEmailFromHTML'; | ||
|
||
const testHTML = `<p>To know more, <a href="https://github.com/leopardslab/react-email">Click Here</a>`; | ||
const expectedText = `To know more, [Click Here] (https://github.com/leopardslab/react-email)`; | ||
|
||
describe('generateTextEmailFromHTML', () => { | ||
test('should return expectedText if we pass testHTML', () => { | ||
const text = generateTextEmailFromHTML(testHTML); | ||
expect(text.trim()).toEqual(expectedText.trim()); | ||
}); | ||
}); |
6 changes: 6 additions & 0 deletions
6
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import htmlToPlainText from 'textversionjs'; | ||
|
||
export const generateTextEmailFromHTML = (html: string): string => { | ||
const plainText = htmlToPlainText(html, { headingStyle: 'hashify' }); | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { generateTextEmailFromHTML } from './generateTextEmailFromHTML'; |
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,3 +1,5 @@ | ||
export { sx } from './sx'; | ||
export { makeStyles } from './makeStyles'; | ||
export { generateEmail } from './generateEmail'; | ||
export { generateTextEmailFromHTML } from './generateTextEmailFromHTML'; | ||
export { generateTextEmail } from './generateTextEmail'; |
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