/* * * impress.js website - build.js * * Developed 2023 by Janis Hutz * */ /* We will convert certain MD files to html and include them in the documentation. This mostly includes the plugin documentation. If there is no MD file in the directory, there will be no documentation. The script will also automatically build the nav menu and copy all the necessary files to the correct places. */ const fs = require( 'fs' ); const path = require( 'path' ); const mdhtml = require( 'markdown-it' ); const md2html = new mdhtml(); const docRoot = path.join( __dirname + '/../' ); const prompt = require( 'prompt-sync' )( { sigint: true } ); const pluginsPath = path.join( __dirname + '/../../../src/plugins' ); let plugins = fs.readdirSync( pluginsPath ); delete plugins[0]; for ( let item in plugins ) { fs.readFile( path.join( pluginsPath + '/' + plugins[item] + '/README.md' ), ( error, data ) => { if ( error ) { console.log( 'NO README found for ' + path.join( pluginsPath + '/' + plugins[item] ) + ' PLEASE MAKE SURE YOU HAVE CREATED A README!' ); } else { let html = md2html.render( '' + data ); storeHTML( findLinks( html, path.join( pluginsPath + '/' + plugins[item] ) ), plugins[item], 'plugins' ); }; } ); } if ( prompt( 'Do you want to regenerate the API reference? (y/n) ' ).toLowerCase() == 'y' ) { console.log( 'Regenerating API reference' ); parseDocumentationMD(); } if ( prompt( 'Do you want to regenerate the Getting Started Guide? (y/n) ' ).toLowerCase() == 'y' ) { console.log( 'Regenerating Getting Started Guide' ); storeHTML( md2html.render( '' + fs.readFileSync( path.join( __dirname + '/../../../GettingStarted.md' ) ) ), '/gettingStarted.html', '' ); } let docPages = fs.readdirSync( __dirname + '/../../../website/docs/reference' ); for ( let obj in docPages ) { if ( docPages[obj] == 'index.html' ) { delete docPages[obj]; }; } generateNav (); /* This function finds links. The reason for this is possible incompatibilities with links on the website */ function findLinks ( html, path ) { let returnHTML = html; for ( let letter in html ) { if ( html[letter] === '<' ) { if ( html.slice( parseInt( letter ), parseInt( letter ) + 9 ) === ''; } else if ( link.slice( 0, 2 ) === '..' ) { // here we map the relative path to an absolute path that can be used with the GitHub repo. while ( link.slice( pos, pos + 3 ) === '../' ) { pos += 3; let pathPos = 1; while ( filepath.slice( parseInt( filepath.length ) - pathPos, parseInt( filepath.length ) - pathPos + 1 ) !== '/' ) { pathPos += 1; }; filepath = filepath.slice( 0, parseInt( filepath.length ) - pathPos + 1 ); }; // Here we find the impress.js root in the filepath to remove it and finish the link generation let fsPos = 0; while ( filepath.slice( parseInt( filepath.length ) - fsPos - 10, parseInt( filepath.length ) - fsPos ) !== 'impress.js' ) { fsPos += 1; }; let fpSlice = filepath.slice( parseInt( filepath.length ) - fsPos, parseInt( filepath.length ) ); let linkSlice = link.slice( pos, link.length ); // now let's assemble a link and add it back into the html return ''; } else if ( link.slice( 0, 1 ) !== '.' && link.slice( 0, 1 ) !== '/' ) { let fsPos = 0; while ( filepath.slice( parseInt( filepath.length ) - fsPos - 10, parseInt( filepath.length ) - fsPos ) !== 'impress.js' ) { fsPos += 1; }; let fpSlice = filepath.slice( parseInt( filepath.length ) - fsPos, parseInt( filepath.length ) ); return ''; } else if ( link.slice( 0, 7 ) === 'http://' || link.slice( 0, 8 ) === 'https://' ) { return ''; } else { throw Error( 'Invalid link found! Link is: "' + link + '" in file: ' + filepath + '/README.md' ); }; }; /* This function generates & stores the HTML in the correct directory */ function storeHTML ( html, path, type ) { let fileOut = ` ${path} :: ${type} | DOCS - impress.js
` + html + `
`; fs.writeFileSync( docRoot + '/' + type + '/' + path + '.html', fileOut ); }; /* This function, as the name implies, generates the navbar on the side in the docs. */ function generateNav () { let fileStruct = `
`; fs.writeFileSync( docRoot + '/nav.html', fileStruct ); }; function parseDocumentationMD () { let doc = '' + fs.readFileSync( path.join( __dirname + '/../../../DOCUMENTATION.md' ) ); let lastHashtagPos = 0; let posArray = []; for ( let letter in doc ) { if ( doc[letter] == '#' ) { if ( doc.slice( parseInt( letter ), parseInt( letter ) + 3 ) === '###' || doc.slice( parseInt( letter ), parseInt( letter ) + 4 ) === '####' ) { } else if ( doc.slice( parseInt( letter ), parseInt( letter ) + 2 ) === '##' && lastHashtagPos + 1 < parseInt( letter ) ) { posArray.push(letter); }; lastHashtagPos = parseInt( letter ); }; }; for ( let item in posArray ) { let titleArea = doc.slice( parseInt( posArray[item] ), parseInt( posArray[item] + 20 ) ); let title = ''; for ( let pos in titleArea ) { if ( titleArea[pos] === '\n' ) { title = titleArea.slice( 3, pos ); break; }; }; let page = md2html.render( doc.slice( parseInt( posArray[parseInt( item )] ), parseInt( posArray[parseInt( item ) + 1] ) || parseInt( doc.length ) ) ); let updatedPage = page; for ( let letter in page ) { if ( page[letter] === '<' ) { if ( page.slice( parseInt( letter ), parseInt( letter ) + 9 ) === '' ) { let i = 9; while ( page.slice( parseInt( letter ) + i, parseInt( letter ) + i + 1 ) !== '<' ) { i += 1; }; let heading = '' + page.slice( parseInt( letter ) + 9, parseInt( letter ) + i ); } }; storeHTML( updatedPage, title, 'reference' ); } } }