@@ -22,6 +22,45 @@ function fetchUrl(url) {
2222 } ) ;
2323}
2424
25+ // Helper function to extract snippet from content using comment markers
26+ function extractSnippet ( content , snippetId = null ) {
27+ const lines = content . split ( '\n' ) ;
28+
29+ // Define comment patterns for different languages
30+ const commentPatterns = [
31+ // Hash-style comments (Python, Ruby, Shell, YAML, etc.)
32+ { start : `#docs-start${ snippetId ? `-${ snippetId } ` : '' } ` , end : `#docs-end${ snippetId ? `-${ snippetId } ` : '' } ` } ,
33+ // Double-slash comments (JavaScript, Java, C++, etc.)
34+ { start : `//docs-start${ snippetId ? `-${ snippetId } ` : '' } ` , end : `//docs-end${ snippetId ? `-${ snippetId } ` : '' } ` } ,
35+ // Block comments (CSS, SQL, etc.)
36+ { start : `/*docs-start${ snippetId ? `-${ snippetId } ` : '' } */` , end : `/*docs-end${ snippetId ? `-${ snippetId } ` : '' } */` } ,
37+ // XML/HTML comments
38+ { start : `<!--docs-start${ snippetId ? `-${ snippetId } ` : '' } -->` , end : `<!--docs-end${ snippetId ? `-${ snippetId } ` : '' } -->` }
39+ ] ;
40+
41+ for ( const pattern of commentPatterns ) {
42+ let startIndex = - 1 ;
43+ let endIndex = - 1 ;
44+
45+ for ( let i = 0 ; i < lines . length ; i ++ ) {
46+ const line = lines [ i ] . trim ( ) ;
47+ if ( line . includes ( pattern . start ) ) {
48+ startIndex = i + 1 ; // Start from the line after the start marker
49+ } else if ( line . includes ( pattern . end ) && startIndex !== - 1 ) {
50+ endIndex = i ; // End at the line before the end marker
51+ break ;
52+ }
53+ }
54+
55+ if ( startIndex !== - 1 && endIndex !== - 1 && startIndex < endIndex ) {
56+ return lines . slice ( startIndex , endIndex ) . join ( '\n' ) ;
57+ }
58+ }
59+
60+ // If no snippet markers found, return original content
61+ return content ;
62+ }
63+
2564function codeImportPlugin ( context , options ) {
2665 return {
2766 name : 'code-import-plugin' ,
@@ -49,19 +88,25 @@ function codeImportPlugin(context, options) {
4988 for ( const match of matches ) {
5089 const [ fullMatch , lang , param , additionalMeta , existingContent ] = match ;
5190
91+ // Parse snippet parameter from additional metadata
92+ const snippetMatch = additionalMeta . match ( / s n i p p e t = ( \w + ) / ) ;
93+ const snippetId = snippetMatch ? snippetMatch [ 1 ] : null ;
94+
5295 try {
5396 let importedContent ;
5497
5598 if ( param . startsWith ( 'file=' ) ) {
5699 // Handle file import
57100 const importPath = param . replace ( 'file=' , '' ) ;
58101 const absoluteImportPath = path . resolve ( context . siteDir , importPath ) ;
59- importedContent = fs . readFileSync ( absoluteImportPath , 'utf8' ) ;
102+ const rawContent = fs . readFileSync ( absoluteImportPath , 'utf8' ) ;
103+ importedContent = extractSnippet ( rawContent , snippetId ) ;
60104 } else if ( param . startsWith ( 'url=' ) ) {
61105 // Handle URL import
62106 const url = param . replace ( 'url=' , '' ) ;
63107 try {
64- importedContent = await fetchUrl ( url ) ;
108+ const rawContent = await fetchUrl ( url ) ;
109+ importedContent = extractSnippet ( rawContent , snippetId ) ;
65110 } catch ( urlError ) {
66111 console . warn ( `Could not fetch URL ${ url } in ${ filePath } : ${ urlError . message } ` ) ;
67112 continue ; // Skip this replacement if URL fetch fails
@@ -71,7 +116,7 @@ function codeImportPlugin(context, options) {
71116 // Preserve the complete metadata
72117 const fullMeta = `${ param } ${ additionalMeta } ` ;
73118 const metaStr = fullMeta ? ` ${ fullMeta } ` : '' ;
74- const replacement = `\`\`\`${ lang || '' } ${ metaStr } \n${ importedContent } \`\`\`` ;
119+ const replacement = `\`\`\`${ lang || '' } ${ metaStr } \n${ importedContent } \n\ `\`\`` ;
75120
76121 content = content . replace ( fullMatch , replacement ) ;
77122 modified = true ;
0 commit comments