@@ -3,6 +3,7 @@ const path = require('path');
3
3
const YAML = require ( 'yaml' ) ;
4
4
5
5
const samplesDir = process . argv [ 2 ] ;
6
+ let samplesMdContent = ''
6
7
7
8
// Inspired by https://github.com/compose-spec/compose-go/blob/main/template/template.go
8
9
const interpolationRegex =
@@ -23,6 +24,12 @@ directories.forEach((sample) => {
23
24
let readme ;
24
25
try {
25
26
readme = fs . readFileSync ( path . join ( samplesDir , sample , 'README.md' ) , 'utf8' ) ;
27
+ // replace the text after the first `#` with a link to the sample
28
+ // readme = readme.replace(/^# (.*)/, `# [$1](https://github.com/DefangLabs/samples/tree/main/samples/${sample})`);
29
+ readme = readme . replace ( / ^ # ( .* ) / , ( match , p1 ) => {
30
+ return `# [${ p1 } ](https://github.com/DefangLabs/samples/tree/main/samples/${ sample } )` ;
31
+ } ) ;
32
+ samplesMdContent += readme + "\n\n" ;
26
33
} catch ( error ) {
27
34
readme = `# ${ sample } ` ;
28
35
}
@@ -34,10 +41,16 @@ directories.forEach((sample) => {
34
41
// Languages:
35
42
//
36
43
// We want to extract the title, short description, tags, and languages from the readme. Tags and languages are comma separated lists.
37
- const title = readme . match ( / T i t l e : ( .* ) / ) [ 1 ] ;
38
- const shortDescription = readme . match ( / S h o r t D e s c r i p t i o n : ( .* ) / ) [ 1 ] ;
39
- const tags = readme . match ( / T a g s : ( .* ) / ) [ 1 ] . split ( ',' ) . map ( tag => tag . trim ( ) ) ;
40
- const languages = readme . match ( / L a n g u a g e s : ( .* ) / ) [ 1 ] . split ( ',' ) . map ( language => language . trim ( ) ) ;
44
+ let title , shortDescription , tags , languages ;
45
+ try {
46
+ title = readme . match ( / T i t l e : ( .* ) / ) [ 1 ] ;
47
+ shortDescription = readme . match ( / S h o r t D e s c r i p t i o n : ( .* ) / ) [ 1 ] ;
48
+ tags = readme . match ( / T a g s : ( .* ) / ) [ 1 ] . split ( ',' ) . map ( tag => tag . trim ( ) ) ;
49
+ languages = readme . match ( / L a n g u a g e s : ( .* ) / ) [ 1 ] . split ( ',' ) . map ( language => language . trim ( ) ) ;
50
+ } catch ( error ) {
51
+ console . log ( `@@ Failed to parse readme for sample ${ sample } ` , error ) ;
52
+ return ;
53
+ }
41
54
42
55
let configs = new Set ( ) ;
43
56
try {
@@ -96,14 +109,30 @@ directories.forEach((sample) => {
96
109
97
110
const stringified = JSON . stringify ( jsonArray , null , 2 ) ;
98
111
99
- // fs.writeFileSync(path.join(__dirname, '..', 'samples.json'), stringified);
112
+ // exclude any lines which start with '---'
113
+ samplesMdContent = samplesMdContent . replace ( / - - - .* \n / g, '' ) ;
114
+ // exclude any lines which include markdown images `![`
115
+ samplesMdContent = samplesMdContent . replace ( / ^ .* ! \[ .* \] \( .* \) .* \n ? / gm, '' ) ;
100
116
101
- // we're going to open up the ../docs/samples.md file and replce [] with the stringified JSON
117
+ // increase the header level of all headers in the markdown content
118
+ samplesMdContent = samplesMdContent . replace ( / ^ ( # { 1 , 6 } ) \s / gm, ( match ) => {
119
+ const headerLevel = match . length - 1 ; // number of '#' characters
120
+ const newHeaderLevel = Math . min ( headerLevel + 1 , 6 ) ;
121
+ return '#' . repeat ( newHeaderLevel ) + ' ' ;
122
+ } ) ;
102
123
103
- // const samplesMd = path.join(__dirname, '..', 'docs', 'samples.md');
104
- // let samplesMdContents = fs.readFileSync(samplesMd, 'utf8');
105
- // samplesMdContents += `<Samples samples={${stringified}} />`;
106
- // fs.writeFileSync(samplesMd, samplesMdContents);
124
+ const frontMatter = `---
125
+ sidebar_class_name: hidden
126
+ title: Samples
127
+ description: A collection of sample applications and configurations for Defang Labs.
128
+ ---
129
+ ` ;
130
+ // prefix samplesMdContent with the front matter
131
+ samplesMdContent = frontMatter + "\n\n" + samplesMdContent ;
132
+
133
+ const samplesMd = path . join ( __dirname , '..' , 'docs' , 'samples.md' ) ;
134
+ console . log ( `@@ Writing samples markdown to ${ samplesMd } ` ) ;
135
+ fs . writeFileSync ( samplesMd , samplesMdContent ) ;
107
136
108
137
// save the json to the samples.json file in static
109
138
fs . writeFileSync ( path . join ( __dirname , '..' , 'static' , 'samples-v2.json' ) , stringified ) ;
0 commit comments