Skip to content

Commit

Permalink
markdown bugfix for the broken blockquotes
Browse files Browse the repository at this point in the history
  • Loading branch information
SebinSong committed Dec 14, 2024
1 parent e4e1f9b commit 9db4641
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions frontend/views/utils/markdown-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ export function renderMarkdown (str: string): any {
strSplitByCodeMarkdown.forEach((entry, index) => {
if (entry.type === 'plain' && strSplitByCodeMarkdown[index - 1]?.text !== '```') {
let entryText = entry.text
entryText = entryText.replace(/</g, '&lt;').replace(/>/g, '&gt;')
entryText = entryText.replace(/</g, '&lt;')
.replace(/(?<!(^|\n))>/g, '&gt;') // Replace all '>' with '&gt;' except for the ones that are not preceded by a line-break or start of the string (e.g. '> asdf' is a blockquote).

entryText = entryText.replace(/\n(?=\n)/g, '\n<br>')
.replace(/<br>\n(\s*)(\d+\.|-)/g, '\n\n$1$2') // [1] custom-handling the case where <br> is directly followed by the start of ordered/unordered lists
.replace(/(\d+\.|-)(\s.+)\n<br>/g, '$1$2\n\n') // [2] this is a custom-logic added so that the end of ordered/un-ordered lists are correctly detected by markedjs.

.replace(/<br>\n(\s*)(>|\d+\.|-)/g, '\n\n$1$2') // [1] custom-handling the case where <br> is directly followed by the start of ordered/unordered lists
.replace(/(>|\d+\.|-)(\s.+)\n<br>/g, '$1$2\n\n') // [2] this is a custom-logic added so that the end of ordered/un-ordered lists are correctly detected by markedjs.
.replace(/(>)(\s.+)\n<br>/gs, '$1$2\n\n') // [3] this is a custom-logic added so that the end of blockquotes are correctly detected by markedjs. ('s' flag is needed to account for multi-line strings)
entry.text = entryText
}
})
Expand Down

0 comments on commit 9db4641

Please sign in to comment.