( tree: MdxNode, line: number, col: number, originalText: string, )
| 163 | } |
| 164 | |
| 165 | function selectBestContainer( |
| 166 | tree: MdxNode, |
| 167 | line: number, |
| 168 | col: number, |
| 169 | originalText: string, |
| 170 | ): ContainerCandidate | null { |
| 171 | const candidates = collectContainerCandidates(tree); |
| 172 | if (candidates.length === 0) return null; |
| 173 | |
| 174 | const normalizedOriginal = normalizeTextForMatch(originalText); |
| 175 | const exactTextMatches = candidates.filter( |
| 176 | (candidate) => normalizeTextForMatch(candidate.renderedText) === normalizedOriginal, |
| 177 | ); |
| 178 | const partialTextMatches = candidates.filter((candidate) => { |
| 179 | const normalizedCandidate = normalizeTextForMatch(candidate.renderedText); |
| 180 | return normalizedCandidate.includes(normalizedOriginal) || normalizedOriginal.includes(normalizedCandidate); |
| 181 | }); |
| 182 | |
| 183 | const pool = |
| 184 | exactTextMatches.length > 0 |
| 185 | ? exactTextMatches |
| 186 | : partialTextMatches.length > 0 |
| 187 | ? partialTextMatches |
| 188 | : candidates; |
| 189 | |
| 190 | let best: ContainerCandidate | null = null; |
| 191 | let bestScore = Number.NEGATIVE_INFINITY; |
| 192 | for (const candidate of pool) { |
| 193 | const score = scoreCandidate(candidate, line, col, normalizedOriginal); |
| 194 | if (score > bestScore) { |
| 195 | best = candidate; |
| 196 | bestScore = score; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | return bestScore > Number.NEGATIVE_INFINITY ? best : null; |
| 201 | } |
| 202 | |
| 203 | function collectContainerCandidates(root: MdxNode): ContainerCandidate[] { |
| 204 | const candidates: ContainerCandidate[] = []; |
no test coverage detected