From 5bb5cdec050af4c3c4e5e7dddfdc099e6e90c77d Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Fri, 9 Aug 2024 13:10:30 +1200 Subject: [PATCH 1/3] Change replaceAll to use RegEx from string --- shared/naturalcrit/markdown.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared/naturalcrit/markdown.js b/shared/naturalcrit/markdown.js index 518b48705..5ffb79f5b 100644 --- a/shared/naturalcrit/markdown.js +++ b/shared/naturalcrit/markdown.js @@ -462,7 +462,7 @@ const replaceVar = function(input, hoist=false, allowUnresolved=false) { mathVars?.forEach((variable)=>{ const foundVar = lookupVar(variable, globalPageNumber, hoist); if(foundVar && foundVar.resolved && foundVar.content && !isNaN(foundVar.content)) // Only subsitute math values if fully resolved, not empty strings, and numbers - replacedLabel = replacedLabel.replaceAll(variable, foundVar.content); + replacedLabel = replacedLabel.replaceAll(new RegExp(`/(? Date: Fri, 9 Aug 2024 15:33:28 +1200 Subject: [PATCH 2/3] Fix typo in regex --- shared/naturalcrit/markdown.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared/naturalcrit/markdown.js b/shared/naturalcrit/markdown.js index 5ffb79f5b..f7f1763e5 100644 --- a/shared/naturalcrit/markdown.js +++ b/shared/naturalcrit/markdown.js @@ -462,7 +462,7 @@ const replaceVar = function(input, hoist=false, allowUnresolved=false) { mathVars?.forEach((variable)=>{ const foundVar = lookupVar(variable, globalPageNumber, hoist); if(foundVar && foundVar.resolved && foundVar.content && !isNaN(foundVar.content)) // Only subsitute math values if fully resolved, not empty strings, and numbers - replacedLabel = replacedLabel.replaceAll(new RegExp(`/(? Date: Fri, 9 Aug 2024 17:42:15 +1200 Subject: [PATCH 3/3] Add variable name checks --- tests/markdown/variables.test.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/markdown/variables.test.js b/tests/markdown/variables.test.js index e6018e19f..3a03d64f6 100644 --- a/tests/markdown/variables.test.js +++ b/tests/markdown/variables.test.js @@ -370,4 +370,18 @@ describe('Cross-page variables', ()=>{ const rendered = renderAllPages([source0, source1]).join('\n\\page\n').trimReturns(); expect(rendered, `Input:\n${[source0, source1].join('\n\\page\n')}`, { showPrefix: false }).toBe('

two

one

\\page

two

'); }); +}); + +describe('Variable name as a subset of a function or other variable name', ()=>{ + it('Variable name as a subset of a function', function() { + const source = `[a]: -1\n\n$[abs(a)]`; + const rendered = Markdown.render(source).trimReturns(); + expect(rendered).toBe('

1

'); + }); + + it('Variable name as a subset of another variable name', function() { + const source = `[ab]: 2\n\n[aba]: 8\n\n[ba]: 4\n\n$[ab + aba + ba]`; + const rendered = Markdown.render(source).trimReturns(); + expect(rendered).toBe('

14

'); + }); }); \ No newline at end of file