From ed45018fdccdc4ec1f783d18d0331a517e161150 Mon Sep 17 00:00:00 2001 From: JK Date: Thu, 28 May 2026 12:00:23 +0200 Subject: [PATCH] fix: update regex for equation links to correctly handle sub-indices --- manifest.json | 2 +- .../equations/live-preview-equations.ts | 24 ++++++++++++------- src/features/equations/numbering.ts | 12 ++++++++-- src/latex-provider.ts | 15 ++++++++---- src/main.ts | 8 ++++++- 5 files changed, 44 insertions(+), 17 deletions(-) diff --git a/manifest.json b/manifest.json index 4a5cf4d..9aeb1ba 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "latex-referencer", "name": "LaTeX Equation Referencer", - "version": "2.2.5", + "version": "2.2.6", "minAppVersion": "1.3.5", "description": "A powerful indexing & referencing system for theorems & equations in your vault. Bring LaTeX-like workflow into Obsidian with theorem environments, automatic equation numbering, and more.", "author": "Jovi Koikkara", diff --git a/src/features/equations/live-preview-equations.ts b/src/features/equations/live-preview-equations.ts index cf4ac03..d10b7db 100644 --- a/src/features/equations/live-preview-equations.ts +++ b/src/features/equations/live-preview-equations.ts @@ -104,13 +104,18 @@ function parseEquationInfo(state: EditorState, plugin: LatexReferencer): Equatio // 1. Scan for all references, including sub-equation links const referenceMap = new Map }>(); - const linkRegex = /\[\[#\^eq-[\w-]+(?:-\d+)?\]\]/g; + const linkRegex = /\[\[#\^eq-[\w-]+\]\]/g; let match; while ((match = linkRegex.exec(text)) !== null) { const linkText = match[0].slice(4, -2); // eq-id or eq-id-2 - const parts = linkText.split('-'); - const baseId = parts.slice(0, 2).join('-'); // eq-id - const subIndexStr = parts.length > 2 ? parts[parts.length - 1] : undefined; + const subIndexMatch = linkText.match(/-(\d+)$/); + let baseId = linkText; + let subIndexStr: string | undefined = undefined; + + if (subIndexMatch) { + subIndexStr = subIndexMatch[1]; + baseId = linkText.substring(0, subIndexMatch.index); + } if (!referenceMap.has(baseId)) { referenceMap.set(baseId, { totalCount: 0, subIndices: new Set() }); @@ -250,18 +255,19 @@ function createTagManagerPlugin(plugin: LatexReferencer, equationField: StateFie let hasContent = false; const taggedRows = rows.map((row, index) => { - if (row.trim() === '') return row; + const cleanedRow = row.replace(/^[ \t]+/, ''); + if (cleanedRow.trim() === '') return cleanedRow; hasContent = true; const subIndex = index + 1; const newTag = ` \\tag{${baseName}.${subIndex}}`; - const endEnvMatch = row.match(/(\\end\{[a-zA-Z*]+\})/); + const endEnvMatch = cleanedRow.match(/(\\end\{[a-zA-Z*]+\})/); if (endEnvMatch && endEnvMatch.index !== undefined) { - const before = row.substring(0, endEnvMatch.index).trimEnd(); + const before = cleanedRow.substring(0, endEnvMatch.index).trimEnd(); const environment = endEnvMatch[0]; - const after = row.substring(endEnvMatch.index + environment.length); + const after = cleanedRow.substring(endEnvMatch.index + environment.length); return before + newTag + ' ' + environment + after; } else { - return row.trimEnd() + newTag; + return cleanedRow.trimEnd() + newTag; } }); diff --git a/src/features/equations/numbering.ts b/src/features/equations/numbering.ts index 4f3629b..a381461 100644 --- a/src/features/equations/numbering.ts +++ b/src/features/equations/numbering.ts @@ -21,10 +21,18 @@ export function processActiveNoteEquations(plugin: LatexReferencer, file: TFile, // 1. Scan the entire document once to build a map of reference counts. const referenceMap = new Map(); - const linkRegex = /\[\[(?:#\^|\^)(eq-[\w-]+)(?:-(\d+))?\]\]/g; + const linkRegex = /\[\[(?:#\^|\^)(eq-[\w-]+)\]\]/g; let match; while ((match = linkRegex.exec(content)) !== null) { - const [, baseId, subIndexStr] = match; + const fullId = match[1]; + const subIndexMatch = fullId.match(/-(\d+)$/); + let baseId = fullId; + let subIndexStr: string | undefined = undefined; + + if (subIndexMatch) { + subIndexStr = subIndexMatch[1]; + baseId = fullId.substring(0, subIndexMatch.index); + } if (!referenceMap.has(baseId)) { referenceMap.set(baseId, { totalCount: 0, subIndices: new Set() }); diff --git a/src/latex-provider.ts b/src/latex-provider.ts index 36fae94..071ef8f 100644 --- a/src/latex-provider.ts +++ b/src/latex-provider.ts @@ -22,11 +22,18 @@ export class LatexLinkProvider extends Provider { } const subpath = parsedLinktext.subpath.substring(2); // remove #^ - const subpathMatch = subpath.match(/^(eq-[\w-]+)(?:-(\d+))?$/); - if (!subpathMatch) return null; + const subIndexMatch = subpath.match(/-(\d+)$/); + let blockId = subpath; + let subIndex: number | undefined = undefined; - const [, blockId, subIndexStr] = subpathMatch; - const subIndex = subIndexStr ? parseInt(subIndexStr) : undefined; + if (subIndexMatch) { + subIndex = parseInt(subIndexMatch[1]); + blockId = subpath.substring(0, subIndexMatch.index); + } + + if (!blockId.startsWith('eq-')) { + return null; + } const activeView = this.app.workspace.getActiveViewOfType(MarkdownView); const activeFile = activeView?.file; diff --git a/src/main.ts b/src/main.ts index f7e1c58..2d1adc8 100644 --- a/src/main.ts +++ b/src/main.ts @@ -228,7 +228,13 @@ export default class LatexReferencer extends Plugin { // Check if it's our custom equation link (e.g., [[#^eq-...]] or [[file#^eq-...]]) if (subpath && subpath.startsWith('^eq-')) { - const blockId = subpath.substring(1); // Remove '^', leaving 'eq-...' + const subpathText = subpath.substring(1); // Remove '^', leaving 'eq-...' + const subIndexMatch = subpathText.match(/-(\d+)$/); + let blockId = subpathText; + + if (subIndexMatch) { + blockId = subpathText.substring(0, subIndexMatch.index); + } const targetFile = plugin.app.metadataCache.getFirstLinkpathDest(path, sourcePath); if (targetFile instanceof TFile) {