Skip to content

Commit 7e8a783

Browse files
committed
fix: latex block on single line
1 parent 067d8b8 commit 7e8a783

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

frontend/src/lib/components/latex/Latex.svelte

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@
1313

1414
{#if html != null}
1515
{@html html}
16+
{:else}
17+
{text}
1618
{/if}

frontend/src/lib/components/markdown/lexer/latex.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,22 @@ describe('MarkdownParser - LaTeX Block Delimiters', () => {
6262
expect(latex.content).toBe('x^2');
6363
});
6464

65+
test('parses \\[ \\] with text before on same line', () => {
66+
const markdown = 'Some text \\[ H_3 \\] more text';
67+
const result = parse(markdown);
68+
const latex = result.tokens[0] as LatexBlockToken;
69+
expect(latex.type).toBe(TokenType.LatexBlock);
70+
expect(latex.content).toBe('H_3');
71+
});
72+
73+
test('parses \\[ \\] with complex math in middle of line', () => {
74+
const markdown = '\\[ H_4 = \\\\langle 1 \\\\rangle = \\\\{0, 1, 2, 3, 4, 5\\\\} = \\\\mathbb{Z}_6 \\]';
75+
const result = parse(markdown);
76+
const latex = result.tokens[0] as LatexBlockToken;
77+
expect(latex.type).toBe(TokenType.LatexBlock);
78+
expect(latex.content).toBe('H_4 = \\\\langle 1 \\\\rangle = \\\\{0, 1, 2, 3, 4, 5\\\\} = \\\\mathbb{Z}_6');
79+
});
80+
6581
test('parses $$ $$ block delimiter with newline', () => {
6682
const markdown = '$$\ny^2 + x^2\n$$';
6783
const result = parse(markdown);

frontend/src/lib/components/markdown/lexer/parsers/block-parser.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,15 @@ export function parseLatexBlock(ctx: ParseContext): BlockParseResult {
151151
delimiter = '$$';
152152
endDelimiter = '$$';
153153
} else {
154-
return { token: null, newPosition: ctx.position, regions: [] };
154+
const line = peekLine(ctx.source, ctx.position);
155+
const bracketPos = line.indexOf('\\[');
156+
if (bracketPos !== -1) {
157+
pos = ctx.position + bracketPos;
158+
delimiter = '\\[';
159+
endDelimiter = '\\]';
160+
} else {
161+
return { token: null, newPosition: ctx.position, regions: [] };
162+
}
155163
}
156164

157165
const afterDelimiter = peek(ctx.source, pos, delimiter.length + 1);
@@ -490,7 +498,7 @@ export function looksLikeBlockStart(line: string): boolean {
490498
line.startsWith('>') ||
491499
line.match(/^\d+\.\s/) !== null ||
492500
line.match(/^[-*+]\s/) !== null ||
493-
line.match(/^\\\[/) !== null ||
501+
line.match(/^\s*\\\[/) !== null ||
494502
line.match(/^\$\$/) !== null ||
495503
isTableRow(line)
496504
);

0 commit comments

Comments
 (0)