Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: HTML to markdown parser #381

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions __tests__/ExpensiMark-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,30 @@ test('Test markdown and url links with inconsistent starting and closing parens'

expect(parser.replace(testString)).toBe(resultString);
});

test('Test HTML string with <br/> tags to markdown ', () => {
const testString = 'Hello<br/>World,<br/>Welcome<br/>To<br/>Expensify';
const resultString = 'Hello\nWorld,\nWelcome\nTo\nExpensify';

expect(parser.htmlToMarkdown(testString)).toBe(resultString);
});

test('Test HTML string with inconsistent <br/> closing tags to markdown ', () => {
const testString = 'Hello<br>World,<br/>Welcome<br>To<br/>Expensify';
const resultString = 'Hello\nWorld,\nWelcome\nTo\nExpensify';

expect(parser.htmlToMarkdown(testString)).toBe(resultString);
});

test('Test HTML string with seperate closing tags (<br><br/>) to markdown ', () => {
const testString = 'Hello<br>World,<br><br/>Welcome<br/>To<br/>Expensify';
const resultString = 'Hello\nWorld,\nWelcome\nTo\nExpensify';

expect(parser.htmlToMarkdown(testString)).toBe(resultString);
});

test('Test HTML string with seperate closing tags (<br></br>) to markdown ', () => {
const testString = 'Hello<br>World,<br></br>Welcome<br/>To<br/>Expensify';
const resultString = 'Hello\nWorld,\nWelcome\nTo\nExpensify';
expect(parser.htmlToMarkdown(testString)).toBe(resultString);
});
35 changes: 35 additions & 0 deletions lib/ExpensiMark.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,22 @@ export default class ExpensiMark {
replacement: '<br>',
},
];

/**
* The list of regex replacements to do on a HTML comment for converting it to markdown.
*
* @type {Object[]}
*/
this.htmlToMarkdownRules = [
{
name: 'newline',

// Replaces open and closing <br><br/> tags with a single <br/>
pre: inputString => inputString.replace('<br></br>', '<br/>').replace('<br><br/>', '<br/>'),
regex: /<br\s*[/]?>/gi,
replacement: '\n'
},
];
}

/**
Expand Down Expand Up @@ -250,4 +266,23 @@ export default class ExpensiMark {

return replacedText;
}

/**
* Replaces HTML with markdown
*
* @param {String} htmlString
*
* @returns {String}
*/
htmlToMarkdown(htmlString) {
let generatedMarkdown = htmlString;
this.htmlToMarkdownRules.forEach((rule) => {
// Pre-processes input HTML before applying regex
Luke9389 marked this conversation as resolved.
Show resolved Hide resolved
if (rule.pre) {
generatedMarkdown = rule.pre(generatedMarkdown);
}
generatedMarkdown = generatedMarkdown.replace(rule.regex, rule.replacement);
});
return generatedMarkdown;
}
}