From 6bb264663577e618c4ce6b1405cec62722c8d8d0 Mon Sep 17 00:00:00 2001 From: Taufik Nurrohman Date: Mon, 12 Feb 2024 00:14:00 +0700 Subject: [PATCH] fix(auto-render): strict dollar delimiter The dollar delimiter, especially the non-display version, is disabled by default because it often causes errors in determining the boundary when there are at least two numbers followed by the `$` symbol present in one paragraph. This change makes it possible to make the delimiter stricter in determining the math boundary, by checking what characters come before the opening delimiter and what characters come after the closing delimiter. Tests are available as follows: ~~~ html ~~~ --- contrib/auto-render/splitAtDelimiters.js | 27 ++++++++++++++++++------ 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/contrib/auto-render/splitAtDelimiters.js b/contrib/auto-render/splitAtDelimiters.js index 21b59030a5..c2679eb820 100644 --- a/contrib/auto-render/splitAtDelimiters.js +++ b/contrib/auto-render/splitAtDelimiters.js @@ -63,13 +63,26 @@ const splitAtDelimiters = function(text, delimiters) { const math = amsRegex.test(rawData) ? rawData : text.slice(delimiters[i].left.length, index); - data.push({ - type: "math", - data: math, - rawData, - display: delimiters[i].display, - }); + let previousData = data.slice(-1).pop(), + // Treat current data as plain text if previous data ends with any words or any numbers with optional space after them + currentData = previousData && /(\w|\b[+-]?\d+([,.]\d+)*\s*)$/.test(previousData.data) && rawData[0] === '$' ? { + type: "text", + data: rawData, + } : { + type: "math", + data: math, + rawData, + display: delimiters[i].display, + }; text = text.slice(index + delimiters[i].right.length); + // Treat current data as plain text if next data starts with any words or any numbers with optional space after them + if (currentData.type === "math" && /^(\w|\s*[+-]?\d+([,.]\d+)*\b)/.test(text) && currentData.rawData[0] === '$') { + currentData.type = "text"; + currentData.data = currentData.rawData; + delete currentData.display; + delete currentData.rawData; + } + data.push(currentData); } if (text !== "") { @@ -82,4 +95,4 @@ const splitAtDelimiters = function(text, delimiters) { return data; }; -export default splitAtDelimiters; +export default splitAtDelimiters; \ No newline at end of file