fixed rendering in Callout Manager

This commit is contained in:
JK 2026-01-17 22:45:36 +01:00
parent e8e9b3e93e
commit b3aec3f6c3

View file

@ -3,7 +3,7 @@ import { EquationBlock } from 'types';
import { trimMathText, parseMarkdownComment, parseYamlLike } from 'utils/parse';
export class ActiveNoteEquationProvider {
constructor(public app: App) {}
constructor(public app: App) { }
getEquations(file: TFile, content: string): EquationBlock[] {
const cache: CachedMetadata | null = this.app.metadataCache.getFileCache(file);
@ -11,42 +11,31 @@ export class ActiveNoteEquationProvider {
return [];
}
const mathSections = cache.sections.filter((section) => section.type === 'math');
const equations: EquationBlock[] = [];
let ordinal = 0;
for (const section of mathSections) {
const text = content.slice(section.position.start.offset, section.position.end.offset);
const mathText = trimMathText(text);
const processMathBlock = (mathText: string, position: { start: { line: number; offset: number; }; end: { line: number; offset: number; }; }) => {
// Fix: trimMathText expects $$...$$, but mathText might be already stripped (from callout regex) or not.
// If it doesn't start with $$, trimMathText returns empty string.
// Check if it has $$ wrappers; if not, just trim whitespace.
let trimmedMathText = mathText.trim();
if (trimmedMathText.startsWith('$$')) {
trimmedMathText = trimMathText(trimmedMathText);
}
let blockId: string | undefined;
// Priority 1: Check for the new internal ID format.
const internalIdMatch = mathText.match(/% id: (eq-[\w-]+)/);
const internalIdMatch = trimmedMathText.match(/% id: (eq-[\w-]+)/);
if (internalIdMatch) {
blockId = internalIdMatch[1];
} else {
// Priority 2: Fallback to the legacy external ID format.
blockId = section.id;
const nextLineIndex = section.position.end.line + 1;
const lines = content.split('\n');
if (nextLineIndex < lines.length) {
const nextLine = lines[nextLineIndex].trim();
const legacyIdMatch = nextLine.match(/^\^([a-zA-Z0-9\-_]+)$/);
if (legacyIdMatch) {
blockId = legacyIdMatch[1];
}
}
}
const tagMatch = mathText.match(/\\tag\{(.*?[^\s])\}/);
const tagMatch = trimmedMathText.match(/\\tag\{(.*?[^\s])\}/);
let manualTag: string | null = null;
if (tagMatch) {
// tagMatch[1] is the full content, e.g., "1.1" or "1"
manualTag = tagMatch[1].split('.')[0];
manualTag = tagMatch[1].split('.')[0];
}
const comments = parseMarkdownComment(mathText);
const comments = parseMarkdownComment(trimmedMathText);
let label: string | undefined;
let display: string | undefined;
for (const comment of comments) {
@ -56,22 +45,83 @@ export class ActiveNoteEquationProvider {
if (parsed['display']) display = parsed['display'];
}
}
const equation: EquationBlock = {
return {
$file: file.path,
$type: 'equation',
$type: 'equation' as const,
$blockId: blockId,
$pos: section.position,
$position: { start: section.position.start.line, end: section.position.end.line },
$mathText: mathText,
$pos: position as any,
$position: { start: position.start.line, end: position.end.line },
$mathText: trimmedMathText,
$manualTag: manualTag,
$label: label,
$display: display,
$printName: null,
$refName: null,
};
equations.push(equation);
};
for (let i = 0; i < cache.sections.length; i++) {
const section = cache.sections[i];
if (section.type === 'math') {
const text = content.slice(section.position.start.offset, section.position.end.offset);
const eq = processMathBlock(text, section.position);
// Legacy ID Check
if (!eq.$blockId) {
eq.$blockId = section.id;
const nextLineIndex = section.position.end.line + 1;
const lines = content.split('\n');
if (nextLineIndex < lines.length) {
const nextLine = lines[nextLineIndex].trim();
const legacyIdMatch = nextLine.match(/^\^([a-zA-Z0-9\-_]+)$/);
if (legacyIdMatch) {
eq.$blockId = legacyIdMatch[1];
}
}
}
equations.push(eq);
}
else if (section.type === 'callout' || section.type === 'blockquote') {
const text = content.slice(section.position.start.offset, section.position.end.offset);
const lines = text.split(/\r?\n/);
// Improved regex to strip blockquote markers
const cleanLines = lines.map(l => l.replace(/^\s*>\s?/, ''));
const cleanText = cleanLines.join('\n');
// Handle split/lazy blockquotes (odd number of $$) by appending a closing one
let processedText = cleanText;
if ((processedText.match(/\$\$/g) || []).length % 2 !== 0) {
processedText += '\n$$';
}
const mathRegex = /\$\$([\s\S]*?)\$\$/g;
let match;
while ((match = mathRegex.exec(processedText)) !== null) {
const mathContent = match[1];
const matchIndex = match.index;
const prefix = processedText.substring(0, matchIndex);
const startLineOffset = (prefix.match(/\n/g) || []).length;
const contentLineCount = (match[0].match(/\n/g) || []).length;
const absStartLine = section.position.start.line + startLineOffset;
const absEndLine = section.position.start.line + startLineOffset + contentLineCount;
const eq = processMathBlock(mathContent, {
start: { line: absStartLine, offset: 0 },
end: { line: absEndLine, offset: 0 }
});
if (eq) {
// @ts-ignore
equations.push(eq);
}
}
}
}
return equations;