Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/quarto-core/src/markdown/cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function isLatexCodeBlock(token: Token) {
}

const kInlineMathPattern = /\$([^ ].*?[^ ]?)\$/g;
const kSingleLineDisplayMathPattern = /\$\$([^\n]+?)\$\$/;
const kSingleLineDisplayMathPattern = /\$\$([^\n]+?)\$\$/g;

function inlineMathRange(pos: Position, line: string, pattern: RegExp) {
pattern.lastIndex = 0;
Expand Down
61 changes: 61 additions & 0 deletions packages/quarto-core/test/cursor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* cursor.test.ts
*
* Copyright (C) 2026 by Posit Software, PBC
*/

import assert from "node:assert/strict";
import test from "node:test";
import { runInNewContext } from "node:vm";

import { Position, Range } from "vscode-languageserver-types";

import { Document } from "../src/document";
import { mathRange } from "../src/markdown/cursor";
import { markdownitParser } from "../src/markdown/parsers/markdownit";

function document(text: string): Document {
return {
uri: "file:///math.qmd",
languageId: "quarto",
version: 1,
lineCount: 1,
getText: () => text,
positionAt: (offset) => Position.create(0, offset),
};
}

for (const [text, character] of [
["Before $$x$$ after", 0],
["Before $$x$$ after", 16],
["Before $$x$$ and $$y$$ after", 14],
["Before $$x$$ and $$y$$ after", 26],
] as const) {
test(`returns no math at character ${character} in ${JSON.stringify(text)}`, () => {
assert.equal(
runInNewContext(
"mathRange(parser, doc, position)",
{ mathRange, parser: markdownitParser(), doc: document(text), position: Position.create(0, character) },
{ timeout: 1000 }
),
null
);
});
}

test("finds later display math and resets the search for repeated requests", () => {
const parser = markdownitParser();
const doc = document("Before $$x$$ and $$y$$ after");
for (const character of [22, 12, 22, 12]) {
assert.deepEqual(
runInNewContext(
"mathRange(parser, doc, position)",
{ mathRange, parser, doc, position: Position.create(0, character) },
{ timeout: 1000 }
),
character === 22
? { math: "y", range: Range.create(0, 17, 0, 22) }
: { math: "x", range: Range.create(0, 7, 0, 12) }
);
}
});
Loading