fix: update regex for equation links to correctly handle sub-indices

This commit is contained in:
JK 2026-05-28 12:00:23 +02:00
parent c5786a8913
commit ed45018fdc
5 changed files with 44 additions and 17 deletions

View file

@ -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",

View file

@ -104,13 +104,18 @@ function parseEquationInfo(state: EditorState, plugin: LatexReferencer): Equatio
// 1. Scan for all references, including sub-equation links
const referenceMap = new Map<string, { totalCount: number, subIndices: Set<number> }>();
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;
}
});

View file

@ -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<string, ReferenceInfo>();
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() });

View file

@ -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;

View file

@ -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) {