123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- /* eslint no-constant-condition:0 */
- const findEndOfMath = function(delimiter, text, startIndex) {
- // Adapted from
- // https://github.com/Khan/perseus/blob/master/src/perseus-markdown.jsx
- let index = startIndex;
- let braceLevel = 0;
- const delimLength = delimiter.length;
- while (index < text.length) {
- const character = text[index];
- if (braceLevel <= 0 &&
- text.slice(index, index + delimLength) === delimiter) {
- return index;
- } else if (character === "\\") {
- index++;
- } else if (character === "{") {
- braceLevel++;
- } else if (character === "}") {
- braceLevel--;
- }
- index++;
- }
- return -1;
- };
- const splitAtDelimiters = function(startData, leftDelim, rightDelim, display) {
- const finalData = [];
- for (let i = 0; i < startData.length; i++) {
- if (startData[i].type === "text") {
- const text = startData[i].data;
- let lookingForLeft = true;
- let currIndex = 0;
- let nextIndex;
- nextIndex = text.indexOf(leftDelim);
- if (nextIndex !== -1) {
- currIndex = nextIndex;
- finalData.push({
- type: "text",
- data: text.slice(0, currIndex),
- });
- lookingForLeft = false;
- }
- while (true) {
- if (lookingForLeft) {
- nextIndex = text.indexOf(leftDelim, currIndex);
- if (nextIndex === -1) {
- break;
- }
- finalData.push({
- type: "text",
- data: text.slice(currIndex, nextIndex),
- });
- currIndex = nextIndex;
- } else {
- nextIndex = findEndOfMath(
- rightDelim,
- text,
- currIndex + leftDelim.length);
- if (nextIndex === -1) {
- break;
- }
- finalData.push({
- type: "math",
- data: text.slice(
- currIndex + leftDelim.length,
- nextIndex),
- rawData: text.slice(
- currIndex,
- nextIndex + rightDelim.length),
- display: display,
- });
- currIndex = nextIndex + rightDelim.length;
- }
- lookingForLeft = !lookingForLeft;
- }
- finalData.push({
- type: "text",
- data: text.slice(currIndex),
- });
- } else {
- finalData.push(startData[i]);
- }
- }
- return finalData;
- };
- export default splitAtDelimiters;
|