1+ var nav = require ( './build/gatsbyConfig.js' ) ;
2+ const path = require ( 'path' ) ;
3+ const fs = require ( 'node:fs' ) ;
4+ // regex to find sections:
5+ // subPages:((\s* .*)*)
6+ try {
7+ if ( ! nav ) {
8+ throw new TypeError ( "Unable to get nav" ) ;
9+ }
10+
11+ if ( ! nav . gatsbyConfig ) {
12+ throw new TypeError ( "Gatsby config not defined" ) ;
13+ }
14+
15+ if ( ! nav . gatsbyConfig . pathPrefix ) {
16+ throw new TypeError ( "pathPrefix not found" ) ;
17+ }
18+
19+ console . log ( nav . gatsbyConfig ) ;
20+ const pathPrefix = nav . gatsbyConfig . pathPrefix ;
21+ let siteMetadata = nav . gatsbyConfig . siteMetadata ? nav . gatsbyConfig . siteMetadata : { } ;
22+
23+
24+ let topNavMarkdown = `` ;
25+ // TODO: prob need url fixer from gatsby theme
26+ // home link defines the first link defaults to Products
27+ // can be hidden
28+ // siteMetadata.versions
29+ // siteMetadata.home
30+
31+ topNavMarkdown += `- pathPrefix:\n` ;
32+ topNavMarkdown += ` - ${ pathPrefix } :\n` ;
33+
34+ if ( siteMetadata . home ) {
35+ topNavMarkdown += '\n- home:\n' ;
36+ topNavMarkdown += ` - [${ topNav . home . title } ](${ topNav . home . path } )\n` ;
37+
38+ if ( siteMetadata . home . hidden ) {
39+ topNavMarkdown += ` - hidden\n` ;
40+ }
41+ }
42+
43+ if ( siteMetadata . versions ) {
44+ topNavMarkdown += '\n- versions:\n' ;
45+
46+ siteMetadata . versions . forEach ( ( versionItem ) => {
47+ let isSelectedText = versionItem . selected ? `selected` : '' ;
48+ let versionPathText = versionItem . path ? versionItem . path : '/' ;
49+ topNavMarkdown += ` - [${ versionItem . title } ](${ versionPathText } ) ${ isSelectedText } \n` ;
50+ } ) ;
51+ }
52+
53+ if ( siteMetadata . pages ) {
54+ topNavMarkdown += `\n- pages:\n` ;
55+ }
56+
57+ siteMetadata . pages ?. forEach ( ( navItem ) => {
58+ //let pathText = navItem.path ? navItem.path : '';
59+ if ( navItem . path ) {
60+ topNavMarkdown += ` - [${ navItem . title } ](${ navItem . path } )\n` ;
61+ } else {
62+ topNavMarkdown += ` - ${ navItem . title } \n` ;
63+ navItem . menu . forEach ( ( menuItem ) => {
64+ topNavMarkdown += ` - [${ menuItem . title } ](${ menuItem . path } )\n` ;
65+ } ) ;
66+ }
67+ } ) ;
68+
69+ if ( siteMetadata . subPages ) {
70+ topNavMarkdown += `\n- subPages:\n` ;
71+ let sideNavMarkdown = `` ;
72+ let depth = 1 ;
73+
74+ sideNavMarkdown += buildSideNavRecursively ( siteMetadata . subPages , depth ) ;
75+ topNavMarkdown += sideNavMarkdown ;
76+ }
77+
78+ fs . writeFileSync ( path . resolve ( __dirname + '/src/pages/config.md' ) , topNavMarkdown ) ;
79+ } catch ( err ) {
80+ console . error ( err ) ;
81+ }
82+ // subpages menu should only appear on the subpages path
83+ // need to check paths to
84+ function buildSideNavRecursively ( sideNav , depth ) {
85+ let sideNavMarkdown = '' ;
86+ console . log ( sideNav )
87+
88+ for ( var k in sideNav ) {
89+ let header = sideNav [ k ] . header ? 'header' : '' ;
90+ sideNavMarkdown += `${ insertSpace ( depth ) } - [${ sideNav [ k ] . title } ](${ sideNav [ k ] . path } ) ${ header } \n` ;
91+
92+ if ( sideNav [ k ] . pages ) {
93+ sideNavMarkdown += buildSideNavRecursively ( sideNav [ k ] . pages , depth + 1 ) ;
94+ }
95+ }
96+ return sideNavMarkdown ;
97+ }
98+
99+ function insertSpace ( indentLevel ) {
100+ let spaces = `` ;
101+ for ( var i = 0 ; i < indentLevel ; i ++ ) {
102+ spaces += ` `
103+ }
104+ return spaces ;
105+ }
106+
107+ // src/pages/topNav.md
108+ // src/pages/sideNav.md
109+ // src/pages/get-started/sideNav.md
110+
111+ // go through each subPages and find each path that relates to a subfolder
112+
113+
114+ // title with path only
115+ // header setting
0 commit comments