Skip to content

Commit

Permalink
[rebber-plugins] Better math sanitation (fixes #471) (#472)
Browse files Browse the repository at this point in the history
  • Loading branch information
LikaKavkasidze committed Oct 23, 2022
1 parent 830e942 commit 3edd453
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
21 changes: 14 additions & 7 deletions packages/rebber-plugins/dist/preprocessors/mathEscape.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,26 @@ module.exports = function () {
var commandStart = node.value.indexOf('\\');

while (commandStart !== -1) {
var commandEnd = node.value.substr(commandStart + 1).search(/[{[\s\\]/); // End not found is end of line
// Eat leading backslashes
var leadSlashes = 1;

if (commandEnd === -1) {
commandEnd = node.value.length - 1;
}
for (; node.value.charAt(commandStart + leadSlashes) === '\\'; leadSlashes++) {
;
} // Find end of command


var potentialEnd = node.value.substr(commandStart + leadSlashes).search(/[{[\s\\]/); // Is end was not found, use end of line

var commandName = node.value.substr(commandStart, commandEnd + 1); // Check for unknown commands
var commandLength = potentialEnd === -1 ? node.value.length : leadSlashes + potentialEnd;
var commandName = node.value.substr(commandStart, commandLength); // Check for unknown commands

if (!katexConstants.includes(commandName)) {
node.value = node.value.replace(commandName, ' ');
var beforeCommand = node.value.substring(0, commandStart);
var afterCommand = node.value.substring(commandStart + commandLength, node.value.length);
node.value = "".concat(beforeCommand, " ").concat(afterCommand);
}

commandStart = node.value.indexOf('\\', commandStart + 1);
commandStart = node.value.indexOf('\\', commandStart + leadSlashes);
} // Check count of brackets


Expand Down
22 changes: 14 additions & 8 deletions packages/rebber-plugins/src/preprocessors/mathEscape.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,27 @@ module.exports = () => node => {
let commandStart = node.value.indexOf('\\')

while (commandStart !== -1) {
let commandEnd = node.value.substr(commandStart + 1).search(/[{[\s\\]/)
// Eat leading backslashes
let leadSlashes = 1
for (; node.value.charAt(commandStart + leadSlashes) === '\\'; leadSlashes++);

// End not found is end of line
if (commandEnd === -1) {
commandEnd = node.value.length - 1
}
// Find end of command
const potentialEnd = node.value.substr(commandStart + leadSlashes).search(/[{[\s\\]/)

// Is end was not found, use end of line
const commandLength = potentialEnd === -1 ? node.value.length : leadSlashes + potentialEnd

const commandName = node.value.substr(commandStart, commandEnd + 1)
const commandName = node.value.substr(commandStart, commandLength)

// Check for unknown commands
if (!katexConstants.includes(commandName)) {
node.value = node.value.replace(commandName, ' ')
const beforeCommand = node.value.substring(0, commandStart)
const afterCommand = node.value.substring(commandStart + commandLength, node.value.length)

node.value = `${beforeCommand} ${afterCommand}`
}

commandStart = node.value.indexOf('\\', commandStart + 1)
commandStart = node.value.indexOf('\\', commandStart + leadSlashes)
}

// Check count of brackets
Expand Down

0 comments on commit 3edd453

Please sign in to comment.