Skip to content

Commit 77ef430

Browse files
Copilotabernier
andcommitted
Fix JSX tag removal to handle nested components properly
Co-authored-by: abernier <76580+abernier@users.noreply.github.com>
1 parent a4bd894 commit 77ef430

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

src/app/llms-full.txt/route.ts

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,34 @@ export const dynamic = 'force-static'
88
* Remove JSX/MDX components and clean up markdown for plain text output
99
*/
1010
function cleanMarkdown(content: string): string {
11-
return (
12-
content
13-
// Remove JSX components like <Intro>, <Keypoints>, etc.
14-
.replace(/<[A-Z][^>]*>[\s\S]*?<\/[A-Z][^>]*>/g, '')
15-
.replace(/<[A-Z][^>]*\/>/g, '')
16-
// Remove inline link syntax <https://...>
17-
.replace(/<(http[^>]+)>/g, '$1')
18-
// Clean up multiple empty lines
19-
.replace(/\n{3,}/g, '\n\n')
20-
.trim()
21-
)
11+
let result = content
12+
13+
// Remove self-closing JSX tags like <Component />
14+
result = result.replace(/<[A-Z][a-zA-Z0-9]*[^>]*\/>/g, '')
15+
16+
// Remove JSX component blocks (opening and closing tags with content)
17+
// Keep applying until no more matches (handles nested components)
18+
let prevResult
19+
let maxIterations = 20 // Safety limit
20+
let iterations = 0
21+
22+
do {
23+
prevResult = result
24+
// Match opening tag, content, and corresponding closing tag
25+
result = result.replace(/<[A-Z][a-zA-Z0-9]*(?:\s[^>]*)?>[\s\S]*?<\/[A-Z][a-zA-Z0-9]*>/g, '')
26+
iterations++
27+
} while (result !== prevResult && iterations < maxIterations)
28+
29+
// Clean up any remaining orphaned tags (shouldn't happen but just in case)
30+
result = result.replace(/<\/?[A-Z][a-zA-Z0-9]*(?:\s[^>]*)?>/g, '')
31+
32+
// Remove inline link syntax <https://...>
33+
result = result.replace(/<(http[^>]+)>/g, '$1')
34+
35+
// Clean up multiple empty lines
36+
result = result.replace(/\n{3,}/g, '\n\n')
37+
38+
return result.trim()
2239
}
2340

2441
export async function GET() {

0 commit comments

Comments
 (0)