@@ -2,7 +2,7 @@ import path from "path";
22import fs from "fs" ;
33import { defineDocumentType , makeSource } from "contentlayer/source-files" ;
44import { visit } from "unist-util-visit" ;
5- import { u } from "unist-builder"
5+ import { u } from "unist-builder" ;
66import { UnistNode } from "./types/unist" ;
77import { componentConfig } from "./config" ;
88
@@ -29,7 +29,6 @@ export default makeSource({
2929 ( ) => ( tree ) => {
3030 visit ( tree , ( node : UnistNode ) => {
3131 if ( node . name === "ComponentShowcase" && node . attributes ) {
32- console . log ( JSON . stringify ( node ) ) ;
3332 const name = getNodeAttributeByName ( node , "name" )
3433 ?. value as keyof typeof componentConfig . registry ;
3534
@@ -40,6 +39,15 @@ export default makeSource({
4039 const component = componentConfig . registry [ name ] ;
4140 const filePath = path . join ( process . cwd ( ) , component . filePath ) ;
4241 const source = fs . readFileSync ( filePath , "utf8" ) ;
42+ const cleanedJSX = source
43+ . replace ( / e x p o r t d e f a u l t f u n c t i o n \w + \( \) \{ \n ? / g, "" ) // removes function wrapper
44+ . replace ( / r e t u r n \s * \( \s * / g, "" ) // removes return statement
45+ . replace ( / \n \s * \) ; ? \s * \} \s * $ / g, "" ) // Removes closing parenthesis, semicolon, and closing brace at the end of the function
46+ . replace ( / \n \s * \n / g, "\n" ) // removes extra new lines
47+ . trim ( )
48+ . split ( "\n" )
49+ . map ( ( line ) => line . replace ( / ^ { 4 } / gm, "" ) )
50+ . join ( "\n" ) ;
4351
4452 node . children ?. push (
4553 u ( "element" , {
@@ -56,15 +64,13 @@ export default makeSource({
5664 children : [
5765 {
5866 type : "text" ,
59- value : source ,
67+ value : cleanedJSX ,
6068 } ,
6169 ] ,
6270 } ) ,
6371 ] ,
6472 } )
6573 ) ;
66-
67- console . log ( JSON . stringify ( node ) ) ;
6874 }
6975 } ) ;
7076 return null ;
0 commit comments