Rewritten highlight parser regex to resolve multiple issues

This commit is contained in:
Stephen Tse 2025-04-19 15:10:12 -07:00
parent bc414a792e
commit 4b4a5556bb

View File

@ -126,7 +126,19 @@ export const tableRegex = new RegExp(/^\|([^\n])+\|\n(\|)( ?:?-{3,}:? ?\|)+\n(\|
// matches any wikilink, only used for escaping wikilinks inside tables
export const tableWikilinkRegex = new RegExp(/(!?\[\[[^\]]*?\]\]|\[\^[^\]]*?\])/g)
const highlightRegex = new RegExp(/==([^=]+?)==/g)
// Edge cases for highlight syntax `==<text>==`:
// 1. Exclude arrow syntax from highlight syntax matching, namely `==>` and `<==`.
// -> (?<!<)==(?!>)
// Test examples:
// * ==A ==> B and B <== C==
// 2. Skip over `==` signs in LaTeX math blocks or code blocks.
// -> For LaTeX: (?:.*?\${1,2}[^$]*?==[^$]*?\${1,2}.*?)*?
// -> For code blocks: (?:.*?`[^`]*?==[^`]*?`.*?)*?
// Test Examples:
// * ==$$A == B AND B == C$$ and $$B == C AND C == D$$==
// * ==$A == B AND B == C$ and $B == C AND C == D$==
// * ==`A == B && B == C` and `B == C && C == D`==
const highlightRegex = new RegExp(/(?<!<)==(?!>)((?:.*?\${1,2}[^$]*?==[^$]*?\${1,2}.*?)*?|(?:.*?`[^`]*?==[^`]*?`.*?)*?|.+?)(?<!<)==(?!>)/g)
const commentRegex = new RegExp(/%%[\s\S]*?%%/g)
// from https://github.com/escwxyz/remark-obsidian-callout/blob/main/src/index.ts
const calloutRegex = new RegExp(/^\[\!([\w-]+)\|?(.+?)?\]([+-]?)/)