@@ -20,12 +20,78 @@ const path = require('path');
2020const readline = require ( 'readline' ) ;
2121
2222async function main ( ) {
23+ await applyExtras ( ) ;
24+ await fixHomePage ( ) ;
25+ await fixTitles ( ) ;
26+ }
27+
28+ /**
29+ * Adds extra content to the generated markdown files. Content in each file in the `extras/`
30+ * directory is added/merged to the top of the corresponding markdown file in the `markdown/`
31+ * directory.
32+ */
33+ async function applyExtras ( ) {
2334 const extras = await getExtraFiles ( ) ;
2435 for ( const source of extras ) {
2536 await applyExtraContentFrom ( source ) ;
2637 }
2738}
2839
40+ /**
41+ * Replace dotted module names in the home page with the correct slash-separated names. For
42+ * example, `firebase-admin.foo` becomes `firebase-admin/foo`. Also replaces the term "Package"
43+ * with "Module" for accuracy.
44+ */
45+ async function fixHomePage ( ) {
46+ const homePage = path . join ( __dirname , 'markdown' , 'index.md' ) ;
47+ const content = await fs . readFile ( homePage ) ;
48+ const updatedText = content . toString ( )
49+ . replace ( / \[ f i r e b a s e - a d m i n \. / g, '[firebase-admin/' )
50+ . replace ( / _ p a c k a g e / g, '_module' )
51+ . replace ( / P a c k a g e / g, 'Module' ) ;
52+ console . log ( `Updating module listings in ${ homePage } ` ) ;
53+ await fs . writeFile ( homePage , updatedText ) ;
54+ }
55+
56+ /**
57+ * Replaces dotted module names and the term "package" in page titles. For example, the title text
58+ * `firebase-admin.foo package` becomes `firebase-admin/foo module`.
59+ */
60+ async function fixTitles ( ) {
61+ const markdownDir = path . join ( __dirname , 'markdown' ) ;
62+ const files = await fs . readdir ( markdownDir ) ;
63+ for ( const file of files ) {
64+ await fixTitleOf ( path . join ( markdownDir , file ) ) ;
65+ }
66+ }
67+
68+ async function fixTitleOf ( file ) {
69+ const reader = readline . createInterface ( {
70+ input : fs . createReadStream ( file ) ,
71+ } ) ;
72+
73+ const buffer = [ ] ;
74+ let updated = false ;
75+ for await ( let line of reader ) {
76+ if ( line . startsWith ( '{% block title %}' ) ) {
77+ if ( line . match ( / f i r e b a s e - a d m i n \. / ) ) {
78+ line = line . replace ( / f i r e b a s e - a d m i n \. / , 'firebase-admin/' ) . replace ( 'package' , 'module' ) ;
79+ updated = true ;
80+ } else {
81+ break ;
82+ }
83+ }
84+
85+ buffer . push ( line ) ;
86+ }
87+
88+ if ( updated ) {
89+ console . log ( `Updating title in ${ file } ` ) ;
90+ const content = Buffer . from ( buffer . join ( '\r\n' ) ) ;
91+ await fs . writeFile ( file , content ) ;
92+ }
93+ }
94+
2995async function getExtraFiles ( ) {
3096 const extrasPath = path . join ( __dirname , 'extras' ) ;
3197 const files = await fs . readdir ( extrasPath ) ;
@@ -45,6 +111,18 @@ async function applyExtraContentFrom(source) {
45111 await writeExtraContentTo ( target , extra ) ;
46112}
47113
114+ async function readExtraContentFrom ( source ) {
115+ const reader = readline . createInterface ( {
116+ input : fs . createReadStream ( source ) ,
117+ } ) ;
118+ const content = [ '' ] ;
119+ for await ( const line of reader ) {
120+ content . push ( line ) ;
121+ }
122+
123+ return content ;
124+ }
125+
48126async function writeExtraContentTo ( target , extra ) {
49127 const output = [ ] ;
50128 const reader = readline . createInterface ( {
@@ -62,18 +140,6 @@ async function writeExtraContentTo(target, extra) {
62140 await fs . writeFile ( target , outputBuffer ) ;
63141}
64142
65- async function readExtraContentFrom ( source ) {
66- const reader = readline . createInterface ( {
67- input : fs . createReadStream ( source ) ,
68- } ) ;
69- const content = [ '' ] ;
70- for await ( const line of reader ) {
71- content . push ( line ) ;
72- }
73-
74- return content ;
75- }
76-
77143( async ( ) => {
78144 try {
79145 await main ( ) ;
0 commit comments