Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,28 @@
*/

export function convertMarkdownToHTMLForStrongTags(markdown) {
// first, remove any HTML tags
markdown = escapeXML(markdown);

// Use a regular expression to find all the words wrapped in **
const regex = /\*\*(.*?)\*\*/g;

// Replace the matched text with the HTML <strong> tags
const result = markdown.replace(regex, '<strong>$1</strong>');
return result;
}

/**
* Escapes any occurrences of &, ", <, > or / with XML entities.
*/
function escapeXML(str) {
const replacements = {
'&': '&amp;',
'"': '&quot;',
"'": '&apos;',
'<': '&lt;',
'>': '&gt;',
'/': '&#x2F;',
};
return String(str).replace(/[&"'<>/]/g, (m) => replacements[m]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,11 @@ describe('convertMarkdownToHTMLForStrongTags', () => {
const actual = convertMarkdownToHTMLForStrongTags(str);
equal(actual, expected);
});

it('ignores HTML', () => {
const str = 'abc **def** <script>alert(document.cookie)</script>';
const expected = 'abc <strong>def</strong> &lt;script&gt;alert(document.cookie)&lt;&#x2F;script&gt;';
const actual = convertMarkdownToHTMLForStrongTags(str);
equal(actual, expected);
});
});
Loading