Skip to content

Commit

Permalink
#2457 - Fix the broken blockquotes in the chat markdown (#2458)
Browse files Browse the repository at this point in the history
* markdown bugfix for the broken blockquotes

* add edge-case handling logic

* remove console.log

* fix the linter err

* comment update

* comment update for flaky test

* handle blank-line within the blockquotes
  • Loading branch information
SebinSong authored Dec 16, 2024
1 parent d4c870a commit 2223594
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
6 changes: 6 additions & 0 deletions frontend/assets/style/components/_custom-markdowns.scss
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
position: relative;
padding-left: 1rem;
white-space: pre-line;
margin: 0.25rem 0;

&::before {
content: "";
Expand All @@ -127,6 +128,11 @@
border-radius: 8px;
background-color: $general_0;
}

p:has(+ p, + pre),
pre:has(+ p, + pre) {
margin-bottom: 1rem;
}
}

.link {
Expand Down
11 changes: 7 additions & 4 deletions frontend/views/utils/markdown-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,21 @@ 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
}
})

str = combineMarkdownSegmentListIntoString(strSplitByCodeMarkdown)
str = str.replace(/(\d+\.|-)(\s.+)\n<br>/g, '$1$2\n\n') // Check for [2] above once more to resolve edge-cases (reference: https://github.com/okTurtles/group-income/issues/2356)
str = str.replace(/(\d+\.|-)(\s.+)\n<br>/g, '$1$2\n\n')
.replace(/(>)(\s.+)\n<br>/gs, '$1$2\n\n') // Check for [2], [3] above once more to resolve edge-cases (reference: https://github.com/okTurtles/group-income/issues/2356)

// STEP 2. convert the markdown into html DOM string.
let converted = marked.parse(str, { gfm: true })
Expand Down

0 comments on commit 2223594

Please sign in to comment.