UNPKG

@mintlify/common

Version:

Commonly shared code within Mintlify

46 lines (45 loc) 1.59 kB
export function findOriginalTextInContent(content, anchor) { var _a, _b; const exactIndex = content.indexOf(anchor.originalText); if (exactIndex !== -1) { return { found: true, startIndex: exactIndex, endIndex: exactIndex + anchor.originalText.length, }; } const contextPattern = anchor.contextBefore + anchor.originalText + anchor.contextAfter; const contextIndex = content.indexOf(contextPattern); if (contextIndex !== -1) { const startIndex = contextIndex + anchor.contextBefore.length; return { found: true, startIndex, endIndex: startIndex + anchor.originalText.length, }; } const lines = content.split('\n'); if (anchor.line <= lines.length) { const targetLine = lines[anchor.line - 1]; if (targetLine) { const lineIndex = targetLine.indexOf(anchor.originalText); if (lineIndex !== -1) { let startIndex = 0; for (let i = 0; i < anchor.line - 1; i++) { startIndex += ((_b = (_a = lines[i]) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) + 1; } startIndex += lineIndex; return { found: true, startIndex, endIndex: startIndex + anchor.originalText.length, }; } } } return { found: false, startIndex: -1, endIndex: -1, }; }