diff --git a/shared/naturalcrit/markdown.js b/shared/naturalcrit/markdown.js index 49c2d8fa0..9388e912a 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(`(?{ }); }); -describe('Variable names as function parameters', ()=>{ - it('Single parameter as variable', function() { +describe('Math function parameter handling', ()=>{ + it('allows variables in single-parameter functions', function() { const source = '[var]:4.1\n\n$[floor(var)]'; const rendered = Markdown.render(source).trimReturns(); expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe(`
4
`); }); - it('Two parameters, one variable', function() { + it('allows one variable and a number in two-parameter functions', function() { const source = '[var]:4\n\n$[min(1,var)]'; const rendered = Markdown.render(source).trimReturns(); expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe(`1
`); }); - it('Two parameters, both variables', function() { + it('allows two variables in two-parameter functions', function() { const source = '[var1]:4\n\n[var2]:8\n\n$[min(var1,var2)]'; const rendered = Markdown.render(source).trimReturns(); expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe(`4
`); }); +}); + +describe('Variable names that are subsets of other names', ()=>{ + it('do not conflict with function names', function() { + const source = `[a]: -1\n\n$[abs(a)]`; + const rendered = Markdown.render(source).trimReturns(); + expect(rendered).toBe('1
'); + }); + + it('do not conflict with other variable names', 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