Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(markdown): render bold/italic glued to punctuation

Akarshan Biswasc22387aae938
diff --git a/web-app/src/containers/RenderMarkdown.tsx b/web-app/src/containers/RenderMarkdown.tsx
index 3ad23fa22..5d21e11bb 100644
--- a/web-app/src/containers/RenderMarkdown.tsx
+++ b/web-app/src/containers/RenderMarkdown.tsx
@@ -122,6 +122,15 @@ const normalizeLatex = (input: string): string => {
(_, pre, inner) => `${pre}$${inner.trim()}$`
)
+ // Emphasis glued between a word char and punctuation (e.g. "word**,**")
+ // is neither left- nor right-flanking per CommonMark, so the markers render
+ // literally. Inject a zero-width space just inside the delimiter to restore
+ // flanking — invisible, and CommonMark treats U+200B as non-punctuation.
+ if (s.includes('*') || s.includes('_'))
+ s = s
+ .replace(/(?<=[\p{L}\p{N}])(\*\*?|__?)(?=[^\s\p{L}\p{N}*_])/gu, '$1​')
+ .replace(/(?<=[^\s\p{L}\p{N}*_])(\*\*?|__?)(?=[\p{L}\p{N}])/gu, '​$1')
+
result += s;
}
diff --git a/web-app/src/containers/__tests__/RenderMarkdown.test.tsx b/web-app/src/containers/__tests__/RenderMarkdown.test.tsx
index 850cbd955..d73bf1d15 100644
--- a/web-app/src/containers/__tests__/RenderMarkdown.test.tsx
+++ b/web-app/src/containers/__tests__/RenderMarkdown.test.tsx
@@ -358,6 +358,40 @@ describe('RenderMarkdown', () => {
})
})
+ describe('emphasis glued to punctuation (CommonMark flanking)', () => {
+ const strong = () =>
+ document.querySelector('.markdown [data-streamdown="strong"]')
+
+ it('renders bold punctuation glued to a word as strong', async () => {
+ render(
+ <RenderMarkdown content={'I went home**,** and slept'} isAnimating={false} />
+ )
+ await waitFor(() => expect(strong()).toBeTruthy())
+ expect(strong()?.textContent?.replace(/​/g, '')).toBe(',')
+ expect(document.querySelector('.markdown')?.textContent).not.toContain('**')
+ })
+
+ it('handles glued bold around CJK punctuation', async () => {
+ render(<RenderMarkdown content={'中文**,**测试'} isAnimating={false} />)
+ await waitFor(() => expect(strong()).toBeTruthy())
+ expect(strong()?.textContent?.replace(/​/g, '')).toBe(',')
+ })
+
+ it('leaves stray asterisks alone (no false emphasis)', async () => {
+ render(<RenderMarkdown content={'use 2 ** 3 maybe'} isAnimating={false} />)
+ await waitFor(() =>
+ expect(document.querySelector('.markdown p')).toBeTruthy()
+ )
+ expect(strong()).toBeNull()
+ })
+
+ it('does not touch normal bold spans', async () => {
+ render(<RenderMarkdown content={'a **bold** word'} isAnimating={false} />)
+ await waitFor(() => expect(strong()).toBeTruthy())
+ expect(strong()?.textContent).toBe('bold')
+ })
+ })
+
describe('LaTeX normalization - HTML tag recognition', () => {
it('does not treat invalid ("<",">") pairs as HTML tag', () => {
const content = '$1 < $2, So choose the $1 one.\n\n> quoted content'