From 7d755fe2a349f646db9dafa4339a8155836f3fe8 Mon Sep 17 00:00:00 2001 From: David Bolack Date: Fri, 23 Feb 2024 11:35:57 -0600 Subject: [PATCH 1/2] Fix syntax highlighting on sub and superscript I did not test this very robustly, it seems. --- client/homebrew/editor/editor.jsx | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/client/homebrew/editor/editor.jsx b/client/homebrew/editor/editor.jsx index d79d2ce4e..3fa936ab7 100644 --- a/client/homebrew/editor/editor.jsx +++ b/client/homebrew/editor/editor.jsx @@ -160,21 +160,25 @@ const Editor = createClass({ } } - // Superscript - if(line.includes('\^')) { - const regex = /\^(?!\s)(?=([^\n\^]*[^\s\^]))\1\^/g; - let match; - while ((match = regex.exec(line)) != null) { - codeMirror.markText({ line: lineNumber, ch: line.indexOf(match[1]) - 1 }, { line: lineNumber, ch: line.indexOf(match[1]) + match[1].length + 1 }, { className: 'superscript' }); - } - } - // Subscript if(line.includes('^^')) { + //const regex = /\^\^(?!\s)(?=([^\n\^]*[^\s\^]))\1\^\^/g; const regex = /\^\^(?!\s)(?=([^\n\^]*[^\s\^]))\1\^\^/g; let match; while ((match = regex.exec(line)) != null) { - codeMirror.markText({ line: lineNumber, ch: line.indexOf(match[1]) - 2 }, { line: lineNumber, ch: line.indexOf(match[1]) + match[1].length + 2 }, { className: 'subscript' }); + if(line.indexOf(match[0]) - 1 != '^') { + codeMirror.markText({ line: lineNumber, ch: line.indexOf(match[0]) }, { line: lineNumber, ch: line.indexOf(match[0]) + match[0].length }, { className: 'subscript' }); + } + } + } + + // Superscript + if(line.includes('^')) { + //const regex = /\^(?!\s)(?=([^\n\^]*[^\s\^]))\1\^/g; + const regex = /\^(?!\s)(?=([^\n\^]*[^\s\^]))\1\^/g; + let match; + while ((match = regex.exec(line)) != null) { + codeMirror.markText({ line: lineNumber, ch: line.indexOf(match[0]) }, { line: lineNumber, ch: line.indexOf(match[0]) + match[0].length }, { className: 'superscript' }); } } From 774b555a61dc15b3938fde3a35b57f4b5606b496 Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Thu, 29 Feb 2024 17:17:38 -0500 Subject: [PATCH 2/2] Rework to fix 5eCleric's tests --- client/homebrew/editor/editor.jsx | 32 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/client/homebrew/editor/editor.jsx b/client/homebrew/editor/editor.jsx index 3fa936ab7..4f3ef44f5 100644 --- a/client/homebrew/editor/editor.jsx +++ b/client/homebrew/editor/editor.jsx @@ -160,25 +160,21 @@ const Editor = createClass({ } } - // Subscript - if(line.includes('^^')) { - //const regex = /\^\^(?!\s)(?=([^\n\^]*[^\s\^]))\1\^\^/g; - const regex = /\^\^(?!\s)(?=([^\n\^]*[^\s\^]))\1\^\^/g; - let match; - while ((match = regex.exec(line)) != null) { - if(line.indexOf(match[0]) - 1 != '^') { - codeMirror.markText({ line: lineNumber, ch: line.indexOf(match[0]) }, { line: lineNumber, ch: line.indexOf(match[0]) + match[0].length }, { className: 'subscript' }); - } - } - } - - // Superscript + // Subscript & Superscript if(line.includes('^')) { - //const regex = /\^(?!\s)(?=([^\n\^]*[^\s\^]))\1\^/g; - const regex = /\^(?!\s)(?=([^\n\^]*[^\s\^]))\1\^/g; - let match; - while ((match = regex.exec(line)) != null) { - codeMirror.markText({ line: lineNumber, ch: line.indexOf(match[0]) }, { line: lineNumber, ch: line.indexOf(match[0]) + match[0].length }, { className: 'superscript' }); + let startIndex = line.indexOf('^'); + const superRegex = /\^(?!\s)(?=([^\n\^]*[^\s\^]))\1\^/gy; + const subRegex = /\^\^(?!\s)(?=([^\n\^]*[^\s\^]))\1\^\^/gy; + + while (startIndex >= 0) { + superRegex.lastIndex = subRegex.lastIndex = startIndex; + let isSuper = false; + let match = subRegex.exec(line) || superRegex.exec(line); + if (match) { + isSuper = !subRegex.lastIndex; + codeMirror.markText({ line: lineNumber, ch: match.index }, { line: lineNumber, ch: match.index + match[0].length }, { className: isSuper ? 'superscript' : 'subscript' }); + } + startIndex = line.indexOf('^', Math.max(startIndex + 1, subRegex.lastIndex, superRegex.lastIndex)); } }