@@ -8,17 +8,34 @@ export const dynamic = 'force-static'
88 * Remove JSX/MDX components and clean up markdown for plain text output
99 */
1010function 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 ( / < ( h t t p [ ^ > ] + ) > / 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 - z A - Z 0 - 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 - z A - Z 0 - 9 ] * (?: \s [ ^ > ] * ) ? > [ \s \S ] * ?< \/ [ A - Z ] [ a - z A - Z 0 - 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 - z A - Z 0 - 9 ] * (?: \s [ ^ > ] * ) ? > / g, '' )
31+
32+ // Remove inline link syntax <https://...>
33+ result = result . replace ( / < ( h t t p [ ^ > ] + ) > / 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
2441export async function GET ( ) {
0 commit comments