From d1e5f85603002c5c192bc415d6fdaa2f8d2cf1f4 Mon Sep 17 00:00:00 2001 From: JK Date: Thu, 30 Oct 2025 20:51:44 +0100 Subject: [PATCH] Staging the setup --- src/equations/provider.ts | 82 +++++++++++++++++++++++++++++++++++++++ src/settings/settings.ts | 58 +++++++++++++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 src/equations/provider.ts diff --git a/src/equations/provider.ts b/src/equations/provider.ts new file mode 100644 index 0000000..a16aad3 --- /dev/null +++ b/src/equations/provider.ts @@ -0,0 +1,82 @@ +import { Editor, TFile, App, Pos } from 'obsidian'; +import { syntaxTree } from '@codemirror/language'; +import { EquationBlock } from 'index/typings/markdown'; +import { trimMathText, parseMarkdownComment, parseYamlLike } from 'utils/parse'; + +export class ActiveNoteEquationProvider { + constructor(public app: App) {} + + getEquations(file: TFile, editor: Editor): EquationBlock[] { + // @ts-ignore + if (!editor.cm) return []; + + const state = editor.cm.state; + const tree = syntaxTree(state); + const equations: EquationBlock[] = []; + let ordinal = 0; + + tree.iterate({ + enter: (node) => { + // In Obsidian's Lezer grammar, display math blocks are typed as 'HyperMD-math-block_math-display' + if (node.name.includes("math-block")) { + const from = node.from; + const to = node.to; + const startPos = editor.offsetToPos(from); + const endPos = editor.offsetToPos(to); + + const text = state.doc.sliceString(from, to); + const mathText = trimMathText(text); + + // Check for block ID on the next line + let blockId: string | undefined; + if (endPos.line + 1 < editor.lineCount()) { + const nextLine = editor.getLine(endPos.line + 1).trim(); + const idMatch = nextLine.match(/^\^([a-zA-Z0-9\-_]+)$/); + if (idMatch) { + blockId = idMatch[1]; + } + } + + const tagMatch = mathText.match(/\\tag\{(.*?[^\s])\}/); + const manualTag = tagMatch ? tagMatch[1] : null; + + const comments = parseMarkdownComment(mathText); + let label: string | undefined; + let display: string | undefined; + for (const comment of comments) { + const parsed = parseYamlLike(comment); + if (parsed) { + if (parsed['label']) label = parsed['label']; + if (parsed['display']) display = parsed['display']; + } + } + + const pos: Pos = { + start: { line: startPos.line, col: startPos.ch, offset: from }, + end: { line: endPos.line, col: endPos.ch, offset: to } + }; + + const equation = new EquationBlock({ + $file: file.path, + $id: EquationBlock.readableId(file.path, ordinal), + $ordinal: ordinal++, + $position: { start: startPos.line, end: endPos.line }, + $pos: pos, + $links: [], // Links are not needed for this provider, leave empty. + $blockId: blockId, + $type: 'equation', + $mathText: mathText, + $manualTag: manualTag, + $label: label, + $display: display, + }); + + equations.push(equation); + return false; // Do not iterate into children of the math block + } + } + }); + + return equations; + } +} diff --git a/src/settings/settings.ts b/src/settings/settings.ts index 663d50c..bea86bb 100644 --- a/src/settings/settings.ts +++ b/src/settings/settings.ts @@ -275,3 +275,61 @@ export const DEFAULT_EXTRA_SETTINGS: Required = { searchModalRange: 'recent', searchModalDvQuery: '', }; + +export interface EquationToolsSettings { + // Numbering + numberOnlyReferencedEquations: boolean; + inferEqNumberPrefix: boolean; + inferEqNumberPrefixFromProperty: string; + inferEqNumberPrefixRegExp: string; + eqNumberPrefix: string; + eqNumberSuffix: string; + eqNumberInit: number; + eqNumberStyle: NumberStyle; + lineByLine: boolean; + + // Referencing + eqRefPrefix: string; + eqRefSuffix: string; + insertSpace: boolean; + + // Autocomplete & Search + enableSuggest: boolean; + triggerSuggest: string; + renderMathInSuggestion: boolean; + suggestNumber: number; + searchMethod: SearchMethod; + modifierToJump: Modifier; + modifierToNoteLink: Modifier; + showModifierInstruction: boolean; + suggestLeafOption: LeafOption; +} + +export const DEFAULT_EQUATION_TOOLS_SETTINGS: Required = { + // Numbering + numberOnlyReferencedEquations: true, + inferEqNumberPrefix: true, + inferEqNumberPrefixFromProperty: "", + inferEqNumberPrefixRegExp: "^[0-9]+(\\.[0-9]+)*", + eqNumberPrefix: "", + eqNumberSuffix: "", + eqNumberInit: 1, + eqNumberStyle: "arabic", + lineByLine: true, + + // Referencing + eqRefPrefix: "", + eqRefSuffix: "", + insertSpace: true, + + // Autocomplete & Search + enableSuggest: true, + triggerSuggest: "\\eqref", + renderMathInSuggestion: true, + suggestNumber: 20, + searchMethod: "Fuzzy", + modifierToJump: "Mod", + modifierToNoteLink: "Shift", + showModifierInstruction: true, + suggestLeafOption: "Current tab", +};