1+ #!/usr/bin/env node
2+
3+ const fs = require ( 'fs' ) ;
4+ const path = require ( 'path' ) ;
5+
6+ const util = require ( 'util' ) ;
7+ const exec = util . promisify ( require ( 'child_process' ) . exec ) ;
8+
9+ const topDir = path . normalize ( path . join ( __dirname , '..' , '..' ) ) ;
10+ const srcDir = path . join ( topDir , 'src' ) ;
11+ const contentDir = path . join ( srcDir , 'content' ) ;
12+ const assetsDir = path . join ( srcDir , 'assets' ) ;
13+ const redirectsFile = path . join ( topDir , 'config' , 'redirects.json' ) ;
14+
15+ const firmwareMdPath = path . join ( contentDir , 'reference' , 'device-os' , 'firmware.md' ) ;
16+
17+
18+ async function run ( ) {
19+ const redirectsJson = JSON . parse ( fs . readFileSync ( redirectsFile , 'utf8' ) ) ;
20+
21+ const origStr = fs . readFileSync ( firmwareMdPath , 'utf8' ) ;
22+ let lineArray = [ ] ;
23+
24+ let anchors = { } ;
25+ let sections = [ '' , '' ] ;
26+
27+ let lineNum = 0 ;
28+ for ( let line of origStr . split ( '\n' ) ) {
29+ lineNum ++ ;
30+ if ( line . startsWith ( '## Device OS versions' ) ) {
31+ // This is no longer included in the index as it's huge and doesn't render properly either
32+ // It must be the last thing in the file, as everything after it is ignored!
33+ break ;
34+ }
35+
36+ if ( line . startsWith ( '##' ) ) {
37+ // Any L2 or higher is an an anchor
38+ const m1 = line . match ( / ^ ( # + ) / ) ;
39+ const level = m1 [ 1 ] . length ;
40+ const spaceIndex = line . indexOf ( ' ' ) ;
41+
42+ const origPrefix = line . substring ( 0 , spaceIndex + 1 ) ;
43+ const origTitle = line . substring ( spaceIndex + 1 ) . trim ( ) . replace ( / & / g, '&' ) ;
44+
45+ let origClassName ;
46+ const m2 = line . match ( / - ( .+ ) $ / ) ;
47+ if ( m2 ) {
48+ origClassName = m2 [ 1 ] ;
49+ }
50+
51+ let shortTitle = origTitle ;
52+ {
53+ const offset1 = shortTitle . indexOf ( '[' ) ;
54+ if ( offset1 > 0 ) {
55+ shortTitle = shortTitle . substring ( 0 , offset1 ) . trim ( ) ;
56+ }
57+ const offset2 = shortTitle . indexOf ( '(' ) ;
58+ if ( offset2 > 0 ) {
59+ shortTitle = shortTitle . substring ( 0 , offset2 ) . trim ( ) ;
60+ }
61+ }
62+
63+ if ( level < sections . length ) {
64+ sections = sections . slice ( 0 , level ) ;
65+ }
66+ sections [ level ] = shortTitle ;
67+
68+ let newClassName ;
69+ if ( sections [ level - 1 ] . length > 0 ) {
70+ newClassName = sections [ level - 1 ] ;
71+ }
72+
73+ let origAnchor = origTitle . toLowerCase ( ) . replace ( / < [ ^ > ] + > / g, '' ) . replace ( / [ ^ \w ] + / g, '-' ) ;
74+ if ( origAnchor === 'constructor' ) {
75+ origAnchor += '-' ;
76+ }
77+
78+ const origAnchorNoTrailingDash = origAnchor . replace ( / [ - ] + $ / g, '' ) ;
79+
80+ /*
81+ if (typeof origClassName != 'undefined' && typeof newClassName != 'undefined' && origClassName != newClassName) {
82+ console.log('class name changed', {origClassName, newClassName});
83+ }
84+ */
85+
86+ if ( typeof origClassName == 'undefined' ) {
87+ if ( sections [ level - 1 ] . length > 0 ) {
88+ const newTitle = origTitle + ' - ' + sections [ level - 1 ] ;
89+ line = origPrefix + newTitle ;
90+ console . log ( 'updated title' , { newTitle, origTitle, line} ) ;
91+ }
92+ }
93+ else {
94+ if ( typeof anchors [ origAnchor ] != 'undefined' ) {
95+ anchors [ origAnchor ] . push ( lineNum ) ;
96+ console . log ( 'non-unique anchor' , { origAnchor, level, section : sections [ level - 1 ] , lines :anchors [ origAnchor ] , } ) ;
97+ }
98+ else {
99+ anchors [ origAnchor ] = [ lineNum ] ;
100+ }
101+ }
102+
103+ }
104+
105+ lineArray . push ( line ) ;
106+ }
107+
108+ // fs.writeFileSync(firmwareMdPath, lineArray.join('\n'));
109+ fs . writeFileSync ( redirectsFile , JSON . stringify ( redirectsJson , null , 2 ) ) ;
110+ }
111+
112+ run ( ) ;
0 commit comments