|
| 1 | +/** |
| 2 | + * Copyright (C) 2021 Tencent. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +import Link from '@/core/hooks/Link'; |
| 17 | + |
| 18 | +const linkRule = new Link({ config: {}, globalConfig: {} }).RULE.reg; |
| 19 | +const delimiterPairs = [ |
| 20 | + { open: '~D~D', close: '~D~D' }, |
| 21 | + { open: '~D', close: '~D' }, |
| 22 | + { open: '\\[', close: '\\]' }, |
| 23 | + { open: '\\(', close: '\\)' }, |
| 24 | +]; |
| 25 | + |
| 26 | +function isEscaped(str, index) { |
| 27 | + let backslashCount = 0; |
| 28 | + for (let i = index - 1; i >= 0 && str[i] === '\\'; i -= 1) { |
| 29 | + backslashCount += 1; |
| 30 | + } |
| 31 | + return backslashCount % 2 === 1; |
| 32 | +} |
| 33 | + |
| 34 | +function findUnescaped(str, delimiter, fromIndex) { |
| 35 | + let index = str.indexOf(delimiter, fromIndex); |
| 36 | + while (index !== -1 && isEscaped(str, index)) { |
| 37 | + index = str.indexOf(delimiter, index + delimiter.length); |
| 38 | + } |
| 39 | + return index; |
| 40 | +} |
| 41 | + |
| 42 | +function getLinkDestinationRanges(str) { |
| 43 | + const ranges = []; |
| 44 | + str.replace(linkRule, (match, leadingChar, text, link, title, target, targetValue, offset) => { |
| 45 | + const destinationStart = match.indexOf(link, match.indexOf(']') + 1); |
| 46 | + if (destinationStart !== -1) { |
| 47 | + ranges.push({ start: offset + destinationStart, end: offset + match.length }); |
| 48 | + } |
| 49 | + return match; |
| 50 | + }); |
| 51 | + return ranges; |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * @param {string} str |
| 56 | + * @param {Record<string, { replacement: string, selfClosing?: boolean }>} replacements |
| 57 | + */ |
| 58 | +export function normalizeMathDelimiters(str, replacements) { |
| 59 | + let delimiters = delimiterPairs.map((delimiter) => ({ |
| 60 | + ...delimiter, |
| 61 | + ...replacements[delimiter.open], |
| 62 | + })); |
| 63 | + let result = ''; |
| 64 | + let cursor = 0; |
| 65 | + const linkDestinationRanges = getLinkDestinationRanges(str); |
| 66 | + |
| 67 | + while (cursor < str.length) { |
| 68 | + let nextDelimiter; |
| 69 | + let openIndex = -1; |
| 70 | + for (const delimiter of delimiters) { |
| 71 | + const index = findUnescaped(str, delimiter.open, cursor); |
| 72 | + if (index !== -1 && (openIndex === -1 || index < openIndex)) { |
| 73 | + nextDelimiter = delimiter; |
| 74 | + openIndex = index; |
| 75 | + } |
| 76 | + } |
| 77 | + if (!nextDelimiter) { |
| 78 | + return result + str.slice(cursor); |
| 79 | + } |
| 80 | + |
| 81 | + result += str.slice(cursor, openIndex); |
| 82 | + const contentStart = openIndex + nextDelimiter.open.length; |
| 83 | + const closeIndex = findUnescaped(str, nextDelimiter.close, contentStart); |
| 84 | + const isInLinkDestination = linkDestinationRanges.some( |
| 85 | + ({ start, end }) => openIndex >= start && openIndex < end, |
| 86 | + ); |
| 87 | + |
| 88 | + if (closeIndex === -1) { |
| 89 | + const content = str.slice(contentStart); |
| 90 | + const hasContent = content.replace(/CHERRYFLOWSESSIONCURSOR\n*$/, '').trim(); |
| 91 | + if (nextDelimiter.replacement && nextDelimiter.selfClosing && hasContent && !isInLinkDestination) { |
| 92 | + return result + nextDelimiter.replacement + content; |
| 93 | + } |
| 94 | + result += nextDelimiter.open; |
| 95 | + cursor = contentStart; |
| 96 | + delimiters = delimiters.filter((delimiter) => delimiter !== nextDelimiter); |
| 97 | + continue; |
| 98 | + } |
| 99 | + |
| 100 | + const content = str.slice(contentStart, closeIndex); |
| 101 | + if (nextDelimiter.replacement && content.trim() && !isInLinkDestination) { |
| 102 | + result += `${nextDelimiter.replacement}${content}${nextDelimiter.replacement}`; |
| 103 | + } else { |
| 104 | + result += str.slice(openIndex, closeIndex + nextDelimiter.close.length); |
| 105 | + } |
| 106 | + cursor = closeIndex + nextDelimiter.close.length; |
| 107 | + } |
| 108 | + return result; |
| 109 | +} |
0 commit comments