From d00f8e18214405d52cae49ce09fa22ccaaae4047 Mon Sep 17 00:00:00 2001 From: qwinsi <70425035+qwinsi@users.noreply.github.com> Date: Fri, 23 Aug 2024 23:13:14 +0800 Subject: [PATCH] implement option.customTexMacros using tokenizer --- src/parser.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/parser.ts b/src/parser.ts index 27e6163..8c430b5 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -690,9 +690,15 @@ export class LatexParser { export function parseTex(tex: string, customTexMacros: {[key: string]: string}): TexNode { const parser = new LatexParser(); - for (const [macro, replacement] of Object.entries(customTexMacros)) { - tex = tex.replaceAll(macro, replacement); + const original_tokens = tokenize(tex); + let processed_tokens: Token[] = []; + for (const token of original_tokens) { + if (token.type === 'command' && customTexMacros[token.value]) { + const expanded_tokens = tokenize(customTexMacros[token.value]); + processed_tokens = processed_tokens.concat(expanded_tokens); + } else { + processed_tokens.push(token); + } } - const tokens = tokenize(tex); - return parser.parse(tokens) as TexNode; + return parser.parse(processed_tokens); }