Disable MathJax rendering of text between dollar signs

I am a user who occasionally has text with free floating dollar signs and has no need for MathJax or Latex. I had Gemini vibe code a plugin inspired by another forum post asking for the same thing. Their code didn’t work for me, so here’s what ended up working for me. I have no idea if it will disable other types of MathJax, since I only need the dollar sign disabler for my purposes. I’ve seen various requests for this over the forum, Reddit and Discord so hopefully it will be of use to people. Apologies to any real coders. Constructive feedback is encouraged!

TLDR:

Text with $ floating around in them in Obsidian $ will look exactly like this in Reader View if you install your own custom plugin using this vibe coded typescript

// main.ts

import { App, Plugin, PluginSettingTab, Setting, MarkdownView, MarkdownPostProcessorContext, Editor } from 'obsidian';

// An interface to define the shape of your settings object.
interface MyPluginSettings {
  // No settings are needed for this plugin.
}

// An object containing the default values for your settings.
const DEFAULT_SETTINGS: MyPluginSettings = {
  // Empty as we have no settings.
};


// This is the main class for your plugin.
export default class MyPlugin extends Plugin {
  settings: MyPluginSettings;

  // This method runs when your plugin is enabled.
  async onload() {
    await this.loadSettings();
    console.log('Delatex Plugin: Loaded');

    this.registerMarkdownPostProcessor((element: HTMLElement, context: MarkdownPostProcessorContext) => {
      const mathElements = element.findAll('span.math-inline, div.math-block');
      if (mathElements.length === 0) {
        return;
      }

      // Get information about the source markdown that created this HTML element.
      const sectionInfo = context.getSectionInfo(element);
      if (!sectionInfo) return;

      const view = this.app.workspace.getActiveViewOfType(MarkdownView);
      if (!view) return;

      // CORRECTED LOGIC:
      // The sectionInfo object gives us line numbers. We must use the editor
      // to get the text of those lines.
      const editor = view.editor;
      const { lineStart, lineEnd } = sectionInfo;
      const lines: string[] = [];
      for (let i = lineStart; i <= lineEnd; i++) {
        lines.push(editor.getLine(i));
      }
      const sectionText = lines.join('\n');

      // The rest of the logic proceeds as before...
      const mathSrcRegex = /(\$\$.*?\$\$|\$.*?\$)/gs;
      const sourceMatches = Array.from(sectionText.matchAll(mathSrcRegex));
      
      if (sourceMatches.length !== mathElements.length) {
        console.warn('Delatex: Mismatch between rendered math elements ('+mathElements.length+') and source text matches ('+sourceMatches.length+'). Skipping replacement to be safe.');
        return;
      }

      for (let i = 0; i < mathElements.length; i++) {
        const el = mathElements[i];
        const sourceText = sourceMatches[i][0];
        
        const textNode = document.createTextNode(sourceText);
        el.replaceWith(textNode);
      }
    });

    this.app.workspace.updateOptions();
  }

  // This method runs when your plugin is disabled.
  onunload() {
    console.log('Unloading Delatex plugin');
    // On unload, we force Obsidian to re-render markdown, which will bring
    // the rendered MathJax back since our post-processor is no longer active.
    this.app.workspace.updateOptions();
  }

  // Standard method to load settings from disk.
  async loadSettings() {
    this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
  }

  // Standard method to save settings to disk.
  async saveSettings() {
    await this.saveData(this.settings);
  }
}