mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-08 20:23:39 +00:00
Fix Math assignments
This commit is contained in:
@@ -150,6 +150,8 @@ const BrewRenderer = (props)=>{
|
|||||||
if(rawPages.length != renderedPages.length) // Re-render all pages when page count changes
|
if(rawPages.length != renderedPages.length) // Re-render all pages when page count changes
|
||||||
renderedPages.length = 0;
|
renderedPages.length = 0;
|
||||||
|
|
||||||
|
//TODO ALWAYS RENDER CURRENT EDITOR PAGE FIRST SO CHANGES CAN POPOGATE OUT. MOVE OUT OF "SHOULDRENDER"
|
||||||
|
|
||||||
_.forEach(rawPages, (page, index)=>{
|
_.forEach(rawPages, (page, index)=>{
|
||||||
if((shouldRender(index) || !renderedPages[index]) && typeof window !== 'undefined'){
|
if((shouldRender(index) || !renderedPages[index]) && typeof window !== 'undefined'){
|
||||||
renderedPages[index] = renderPage(page, index); // Render any page not yet rendered, but only re-render those in PPR range
|
renderedPages[index] = renderPage(page, index); // Render any page not yet rendered, but only re-render those in PPR range
|
||||||
|
|||||||
@@ -331,20 +331,22 @@ const replaceVar = function(input, hoist=false) {
|
|||||||
let missingValues = [];
|
let missingValues = [];
|
||||||
|
|
||||||
//v=====--------------------< HANDLE MATH >-------------------=====v//
|
//v=====--------------------< HANDLE MATH >-------------------=====v//
|
||||||
const variableRegex = /[a-zA-Z_][a-zA-Z0-9_]*(?=\s*(?:[+\-*\/()]|$))/g; // Capture only variables, ignore mathy stuff
|
// Split the string into separate expressions
|
||||||
let mathVars = label.match(variableRegex)?.map((s)=>s.trim());
|
|
||||||
|
//const variableRegex = /[a-zA-Z_][a-zA-Z0-9_]*(?=\s*(?:[+\-*\/)]|$))/g;
|
||||||
|
let mathRegex = /[a-z]+\(|[+\-*/()]/g;
|
||||||
|
let matches = label.split(mathRegex)
|
||||||
|
let mathVars = matches.filter(match => isNaN(match))?.map((s)=>s.trim()); // Capture any variable names
|
||||||
|
|
||||||
let replacedLabel = label;
|
let replacedLabel = label;
|
||||||
|
|
||||||
if(mathVars?.[0] !== label.trim()) {// If there was mathy stuff not captured, let's do math!
|
if(mathVars?.[0] !== label.trim()) {// If there was mathy stuff not captured, let's do math!
|
||||||
mathVars?.forEach((variable) => {
|
mathVars?.forEach((variable) => {
|
||||||
const foundVar = lookupVar(variable, globalPageNumber, hoist);
|
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
|
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(variable, foundVar.content);
|
||||||
}
|
else
|
||||||
else {
|
missingValues.push(variable);
|
||||||
missingValues.push(foundVar);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let result;
|
let result;
|
||||||
@@ -386,7 +388,7 @@ const replaceVar = function(input, hoist=false) {
|
|||||||
|
|
||||||
let value;
|
let value;
|
||||||
|
|
||||||
if(!prefix[0] && href) // Link
|
if(!prefix[0] && href) // Link
|
||||||
value = `[${label}](${href}${title ? ` ${title}` : ''})`;
|
value = `[${label}](${href}${title ? ` ${title}` : ''})`;
|
||||||
|
|
||||||
if(prefix[0] == '!' && href) // Image
|
if(prefix[0] == '!' && href) // Image
|
||||||
@@ -406,9 +408,8 @@ const lookupVar = function(label, index, hoist=false) {
|
|||||||
index = Object.keys(globalLinks).length;
|
index = Object.keys(globalLinks).length;
|
||||||
|
|
||||||
while (index >= 0) {
|
while (index >= 0) {
|
||||||
if(globalLinks[index]?.[label] !== undefined) {
|
if(globalLinks[index]?.[label] !== undefined)
|
||||||
return globalLinks[index][label];
|
return globalLinks[index][label];
|
||||||
}
|
|
||||||
index--;
|
index--;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user