MCPcopy Create free account
hub / github.com/donghaxkim/react-rewrite / selectBestContainer

Function selectBestContainer

packages/cli/src/mdx-text.ts:165–201  ·  view source on GitHub ↗
(
  tree: MdxNode,
  line: number,
  col: number,
  originalText: string,
)

Source from the content-addressed store, hash-verified

163}
164
165function 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
203function collectContainerCandidates(root: MdxNode): ContainerCandidate[] {
204 const candidates: ContainerCandidate[] = [];

Callers 1

applyMdxTextEditFunction · 0.85

Calls 3

normalizeTextForMatchFunction · 0.85
scoreCandidateFunction · 0.85

Tested by

no test coverage detected