Skip to content

Commit 4365287

Browse files
Merge pull request NousResearch#17175 from NousResearch/fix/markdown
feat(latex): latex in tui
2 parents ad59f97 + 084499a commit 4365287

7 files changed

Lines changed: 1391 additions & 50 deletions

File tree

ui-tui/package-lock.json

Lines changed: 16 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ui-tui/src/__tests__/markdown.test.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,66 @@ describe('stripInlineMarkup', () => {
6161
expect(stripInlineMarkup('Yay ~! nice work ~!')).toBe('Yay ~! nice work ~!')
6262
expect(stripInlineMarkup('H~2~O and CO~2~')).toBe('H_2O and CO_2')
6363
})
64+
65+
it('strips inline math delimiters but keeps the formula text', () => {
66+
expect(stripInlineMarkup('$\\mathbb{Z}$ is a ring')).toBe('\\mathbb{Z} is a ring')
67+
expect(stripInlineMarkup('see \\(a + b\\) ok')).toBe('see a + b ok')
68+
})
69+
})
70+
71+
describe('INLINE_RE inline math', () => {
72+
it('matches single-dollar math and beats emphasis at the same start', () => {
73+
// Without math handling, `*b*` would have matched as italics and
74+
// corrupted the formula. With math added to INLINE_RE, the leftmost
75+
// match at column 0 (`$P=a*b*c$`) wins.
76+
expect(matches('$P=a*b*c$')).toEqual(['$P=a*b*c$'])
77+
expect(matches('see $\\mathbb{Z}$ here')).toEqual(['$\\mathbb{Z}$'])
78+
})
79+
80+
it('does not match currency-style prose', () => {
81+
expect(matches('it costs $5 and $10')).toEqual([])
82+
expect(matches('paid $5')).toEqual([])
83+
})
84+
85+
it('does not let inline math swallow a $$ display fence', () => {
86+
// `$$x$$` is a display block, not two abutting inline-math spans.
87+
expect(matches('$$x$$')).toEqual([])
88+
})
89+
90+
it('matches \\(...\\) inline math', () => {
91+
expect(matches('foo \\(x + y\\) bar')).toEqual(['\\(x + y\\)'])
92+
})
93+
94+
it('does not corrupt subscripts/superscripts inside math', () => {
95+
// `_n` and `^r` are markdown emphasis/superscript markers in prose, but
96+
// inside a `$...$` span the entire formula is captured as a single
97+
// inline-math token so the inner regexes never see those characters.
98+
expect(matches('$P=a_n x^n + a_0$')).toEqual(['$P=a_n x^n + a_0$'])
99+
expect(matches('$\\beta_1,\\dots,\\beta_r$')).toEqual(['$\\beta_1,\\dots,\\beta_r$'])
100+
})
101+
102+
it('places math content in the correct capture group (regression: m[16] is bare URL)', () => {
103+
// When `m[16]` was the bare URL group AND the inline-math `$...$`
104+
// group simultaneously (because the bare URL pattern lacked its own
105+
// capturing parens), MdInline rendered `$\\mathbb{R}$` as an
106+
// underlined autolink instead of italic amber math. Lock down the
107+
// numbering: math goes in m[17] / m[18], URLs go in m[16].
108+
const url = [...'see https://example.com here'.matchAll(INLINE_RE)][0]!
109+
const dollarMath = [...'$\\mathbb{R}$'.matchAll(INLINE_RE)][0]!
110+
const parenMath = [...'\\(\\pi\\)'.matchAll(INLINE_RE)][0]!
111+
112+
expect(url[16]).toBe('https://example.com')
113+
expect(url[17]).toBeUndefined()
114+
expect(url[18]).toBeUndefined()
115+
116+
expect(dollarMath[16]).toBeUndefined()
117+
expect(dollarMath[17]).toBe('\\mathbb{R}')
118+
expect(dollarMath[18]).toBeUndefined()
119+
120+
expect(parenMath[16]).toBeUndefined()
121+
expect(parenMath[17]).toBeUndefined()
122+
expect(parenMath[18]).toBe('\\pi')
123+
})
64124
})
65125

66126
describe('protocol sentinels', () => {

0 commit comments

Comments
 (0)