diff --git a/main.ts b/main.ts index c3cc2d1..3470b15 100644 --- a/main.ts +++ b/main.ts @@ -13,6 +13,7 @@ import { checkTranslatedMacros, } from "src/latex_translator"; import { createExportButton } from "src/export_button"; +import { extractInlineMacros } from "src/inline_macro"; import * as pseudocode from "pseudocode"; @@ -29,11 +30,15 @@ export default class PseudocodePlugin extends Plugin { const blockWidth = this.settings.blockSize; blockDiv.style.width = `${blockWidth}em`; + // Extract inline macros + const [inlineMacros, nonMacroLines] = extractInlineMacros(source); + const allPreamble = this.preamble + inlineMacros; + // find all $ enclosements in source, and add the preamble. // TODO: Might be able to optimize. const mathRegex = /\$(.*?)\$/g; - const withPreamble = source.replace(mathRegex, (_, group1) => { - return `$${this.preamble}${group1}$`; + const withPreamble = nonMacroLines.replace(mathRegex, (_, group1) => { + return `$${allPreamble}${group1}$`; }); const preEl = blockDiv.createEl("pre", { diff --git a/src/inline_macro.ts b/src/inline_macro.ts new file mode 100644 index 0000000..eb9eca7 --- /dev/null +++ b/src/inline_macro.ts @@ -0,0 +1,26 @@ +import { + translateUnsupportedMacrosPerf, + checkTranslatedMacros, +} from "./latex_translator"; + +export function extractInlineMacros(source: string): [string, string] { + const lines = source.split("\n"); + + let i: number; + for (i = 0; i < lines.length; i++) { + if (lines[i].includes("\\begin{algorithm}")) break; + } + + const macroLines = lines.slice(0, i).join("\n"); + const nonMacroLines = lines.slice(i).join("\n"); + + let inlineMacros = ""; + try { + const translated = translateUnsupportedMacrosPerf(macroLines); + inlineMacros = checkTranslatedMacros(translated); + } catch (error) { + console.error(error); + } + + return [inlineMacros, nonMacroLines]; +}