88const fs = require ( 'fs' ) ;
99const path = require ( 'path' ) ;
1010const archiver = require ( 'archiver' ) ;
11+ const { composePlugins, ignoreLinePlugin, ignoreFilePlugin, ignoreBlockPlugin } = require ( './plugins/index' ) ;
1112
1213/**
1314 * Build configuration
1415 */
1516const defaultConfig = {
17+ // Global plugins applied to all examples
18+ plugins : [ ignoreFilePlugin , ignoreBlockPlugin , ignoreLinePlugin ] ,
1619 examples : [
1720 {
1821 path : 'basics/next-app-router' ,
@@ -23,6 +26,8 @@ const defaultConfig = {
2326 includes : [ ] ,
2427 regex : [ ] ,
2528 } ,
29+ // Example-specific plugins (optional)
30+ plugins : [ ] ,
2631 } ,
2732 {
2833 path : 'basics/next-pages-router' ,
@@ -33,6 +38,8 @@ const defaultConfig = {
3338 includes : [ ] ,
3439 regex : [ ] ,
3540 } ,
41+ // Example-specific plugins (optional)
42+ plugins : [ ] ,
3643 } ,
3744 ] ,
3845 globalSkipPatterns : {
@@ -100,14 +107,38 @@ function buildMarkdownHeader(frameworkName, repoUrl, relativePath) {
100107
101108/**
102109 * Convert a file to a markdown code block
110+ * Now supports plugins for content transformation
111+ *
112+ * @param {string } relativePath - The relative path of the file
113+ * @param {string } content - The file content
114+ * @param {string } extension - The file extension
115+ * @param {Array } plugins - Optional array of plugins to transform content
116+ * @returns {string|null } - The markdown representation of the file, or null if content is empty after transformation
103117 */
104- function fileToMarkdown ( relativePath , content , extension ) {
118+ function fileToMarkdown ( relativePath , content , extension , plugins = [ ] ) {
119+ // Create context object for plugins
120+ const context = {
121+ relativePath,
122+ extension,
123+ } ;
124+
125+ // Apply plugins to content if provided
126+ const transformedContent = plugins . length > 0
127+ ? composePlugins ( plugins ) ( content , context )
128+ : content ;
129+
130+ // If content is empty after transformation, return null to skip the file
131+ if ( ! transformedContent || transformedContent . trim ( ) === '' ) {
132+ return null ;
133+ }
134+
135+ // Build markdown output
105136 let markdown = `## ${ relativePath } \n\n` ;
106137 if ( extension === 'md' ) {
107- markdown += content ;
138+ markdown += transformedContent ;
108139 } else {
109140 markdown += `\`\`\`${ extension } \n` ;
110- markdown += content ;
141+ markdown += transformedContent ;
111142 markdown += '\n```\n\n' ;
112143 }
113144 markdown += '\n---\n\n' ;
@@ -181,7 +212,7 @@ function getAllFiles(dirPath, arrayOfFiles = [], baseDir = dirPath, skipPatterns
181212/**
182213 * Convert an example project directory to markdown
183214 */
184- function convertProjectToMarkdown ( absolutePath , frameworkInfo , relativePath , skipPatterns ) {
215+ function convertProjectToMarkdown ( absolutePath , frameworkInfo , relativePath , skipPatterns , plugins = [ ] ) {
185216 const repoUrl = 'https://github.com/PostHog/examples' ;
186217 let markdown = buildMarkdownHeader ( frameworkInfo . displayName , repoUrl , relativePath ) ;
187218
@@ -203,7 +234,12 @@ function convertProjectToMarkdown(absolutePath, frameworkInfo, relativePath, ski
203234 try {
204235 const content = fs . readFileSync ( file . fullPath , 'utf8' ) ;
205236 const extension = path . extname ( file . fullPath ) . slice ( 1 ) || '' ;
206- markdown += fileToMarkdown ( file . relativePath , content , extension ) ;
237+ const fileMarkdown = fileToMarkdown ( file . relativePath , content , extension , plugins ) ;
238+
239+ // Skip file if plugins returned empty content
240+ if ( fileMarkdown !== null ) {
241+ markdown += fileMarkdown ;
242+ }
207243 } catch ( e ) {
208244 // Skip files that can't be read as text
209245 console . warn ( `Skipping ${ file . relativePath } : ${ e . message } ` ) ;
@@ -243,12 +279,19 @@ async function build() {
243279 example . skipPatterns
244280 ) ;
245281
282+ // Merge global and example-specific plugins
283+ const plugins = [
284+ ...( defaultConfig . plugins || [ ] ) ,
285+ ...( example . plugins || [ ] )
286+ ] ;
287+
246288 console . log ( `Processing ${ example . displayName } ...` ) ;
247289 const markdown = convertProjectToMarkdown (
248290 absolutePath ,
249291 example ,
250292 example . path ,
251- skipPatterns
293+ skipPatterns ,
294+ plugins
252295 ) ;
253296
254297 const outputFilename = `${ example . id } .md` ;
0 commit comments