(startData, leftDelim, rightDelim, display)
| 28 | }; |
| 29 | |
| 30 | const splitAtDelimiters = function splitAtDelimiters(startData, leftDelim, rightDelim, display) { |
| 31 | const finalData = []; |
| 32 | |
| 33 | for (let i = 0; i < startData.length; i++) { |
| 34 | if (startData[i].type === "text") { |
| 35 | const text = startData[i].data; |
| 36 | let lookingForLeft = true; |
| 37 | let currIndex = 0; |
| 38 | let nextIndex; |
| 39 | nextIndex = text.indexOf(leftDelim); |
| 40 | |
| 41 | if (nextIndex !== -1) { |
| 42 | currIndex = nextIndex; |
| 43 | finalData.push({ |
| 44 | type: "text", |
| 45 | data: text.slice(0, currIndex) |
| 46 | }); |
| 47 | lookingForLeft = false; |
| 48 | } |
| 49 | |
| 50 | while (true) { |
| 51 | if (lookingForLeft) { |
| 52 | nextIndex = text.indexOf(leftDelim, currIndex); |
| 53 | |
| 54 | if (nextIndex === -1) { |
| 55 | break; |
| 56 | } |
| 57 | |
| 58 | finalData.push({ |
| 59 | type: "text", |
| 60 | data: text.slice(currIndex, nextIndex) |
| 61 | }); |
| 62 | currIndex = nextIndex; |
| 63 | } else { |
| 64 | nextIndex = findEndOfMath(rightDelim, text, currIndex + leftDelim.length); |
| 65 | |
| 66 | if (nextIndex === -1) { |
| 67 | break; |
| 68 | } |
| 69 | |
| 70 | finalData.push({ |
| 71 | type: "math", |
| 72 | data: text.slice(currIndex + leftDelim.length, nextIndex), |
| 73 | rawData: text.slice(currIndex, nextIndex + rightDelim.length), |
| 74 | display: display |
| 75 | }); |
| 76 | currIndex = nextIndex + rightDelim.length; |
| 77 | } |
| 78 | |
| 79 | lookingForLeft = !lookingForLeft; |
| 80 | } |
| 81 | |
| 82 | finalData.push({ |
| 83 | type: "text", |
| 84 | data: text.slice(currIndex) |
| 85 | }); |
| 86 | } else { |
| 87 | finalData.push(startData[i]); |
no test coverage detected