feat: improve regex matcher; all tests run now!

This commit is contained in:
Daniel Bauer
2024-12-14 11:29:50 +01:00
parent 294e76ffc8
commit 1d2a860df2
2 changed files with 35 additions and 16 deletions

View File

@@ -138,6 +138,9 @@ describe("trigger", () => {
13, 13,
]); ]);
}); });
test("nested parantheses", () => {
expect(getTriggerText("((test))", 1)).toEqual(["(test)", 1, 7]);
});
}); });
describe("text in square brackets", () => { describe("text in square brackets", () => {
@@ -260,6 +263,9 @@ describe("trigger", () => {
13, 13,
]); ]);
}); });
test("nested parantheses", () => {
expect(getTriggerText("[(test)]", 1)).toEqual(["(test)", 1, 7]);
});
}); });
describe("cursor position", () => { describe("cursor position", () => {

View File

@@ -1,21 +1,33 @@
/** /**
* Maches single square brackets or parantheses with some text in them * Maches parantheses or single square brackets or parantheses
* and captures the text * and captures the enclosed text
* If the brackets start a wiki link or markdown link, they are ignored * If the brackets start a markdown link, they are ignored.
* but links inside another layer of brackets are returned: * Wiki links don't need to be ignored since Obsidian overwrites suggestions for them.
*
* (
* (?<=^\(|\s\() # Pattern for parantheses
* .+?:{0,2}.*?
* (?=\)$|\)\s)
* |
* (?<=^\[|\s\[) # Pattern for square brackets
* .+?:{0,2}.*?
* (?=\]$|\]\s)
* )
*/ */
const filledRegex = const filledRegex = new RegExp(
/((?<=^\(|\s\().+?:{0,2}.*?(?=\)$|\)\s)|(?<=^\[|\s\[).+?:{0,2}.*?(?=\]$|\]\s))/g; "(" +
[
// pattern for parantheses
// pos. lookbehind for opening parantheses; no closing square bracket before to exlude markdown links
/(?<=^\(|[^\]]\()/,
/.+?/,
// pos. lookahead for closing paranthesis; not followed by another one for nested ((test))
/(?=\)$|\)[^\)])/,
/|/,
// pattern for square brackets
// pos. lookbehind for opening square bracket
/(?<=\[)/,
/.+?/,
// pos. lookahead for closing bracket; not followed by another one ([[test]]) or an opening paranthese (markdown link!)
/(?=\]$|\][^\]\(])/,
]
.map((s) => s.source)
.join("") +
")",
"g",
);
/** /**
* Finds the dataview metadata field the user is currently inside. * Finds the dataview metadata field the user is currently inside.
@@ -48,6 +60,7 @@ function getTriggerTextFromRegex(
): [string, number, number] | null { ): [string, number, number] | null {
let matches = Array.from(line.matchAll(regex)); let matches = Array.from(line.matchAll(regex));
for (const match of matches) { for (const match of matches) {
console.warn(match);
if (match.index === undefined) { if (match.index === undefined) {
continue; continue;
} }