diff --git a/scripts/web.js b/scripts/web.js index 0115e206c..384212d9d 100644 --- a/scripts/web.js +++ b/scripts/web.js @@ -48,10 +48,12 @@ sass.render( ); // Set variables for paths const snippetsPath = './snippets', + archivedSnippetsPath = './snippets_archive', staticPartsPath = './static-parts', docsPath = './docs'; // Set variables for script let snippets = {}, + archivedSnippets = {}, beginnerSnippetNames = ['everyNth', 'filterNonUnique', 'last', 'maxN', 'minN', 'nthElement', 'sample', 'similarity', 'tail', 'currentURL', 'hasClass', 'getMeridiemSuffixOfInteger', 'factorial', 'fibonacci', 'gcd', 'isDivisible', 'isEven', 'isPrime', 'lcm', 'randomIntegerInRange', 'sum', 'reverseString', 'truncateString'], startPart = '', endPart = '', @@ -59,6 +61,10 @@ let snippets = {}, beginnerStartPart = '', beginnerEndPart = '', beginnerOutput = '', + archivedStartPart = '', + archivedEndPart = '', + archivedOutput = '', + indexStaticFile = '', pagesOutput = []; tagDbData = {}; @@ -66,6 +72,8 @@ let snippets = {}, console.time('Webber'); // Synchronously read all snippets and sort them as necessary (case-insensitive) snippets = util.readSnippets(snippetsPath); +archivedSnippets = util.readSnippets(archivedSnippetsPath); + // Load static parts for all pages try { @@ -75,6 +83,9 @@ try { beginnerStartPart = fs.readFileSync(path.join(staticPartsPath, 'beginner-page-start.html'), 'utf8'); beginnerEndPart = fs.readFileSync(path.join(staticPartsPath, 'beginner-page-end.html'), 'utf8'); + archivedStartPart = fs.readFileSync(path.join(staticPartsPath, 'archived-page-start.html'), 'utf8'); + archivedEndPart = fs.readFileSync(path.join(staticPartsPath, 'archived-page-end.html'), 'utf8'); + indexStaticFile = fs.readFileSync(path.join(staticPartsPath, 'index.html'), 'utf8'); } catch (err) { // Handle errors (hopefully not!) @@ -271,5 +282,52 @@ try { process.exit(1); } +// Create the output for the beginner.html file +try { + // Add the static part + beginnerOutput += `${beginnerStartPart + '\n'}`; + + // Filter begginer snippets + const filteredBeginnerSnippets = Object.keys(snippets) + .filter(key => beginnerSnippetNames.map(name => name+'.md').includes(key)) + .reduce((obj, key) => { + obj[key] = snippets[key]; + return obj; + }, {}); + + for (let snippet of Object.entries(filteredBeginnerSnippets)) + beginnerOutput += + '
([^\0]*?)<\/code><\/pre>/gm, (match, p1) => `${Prism.highlight(unescapeHTML(p1), Prism.languages.javascript)}`)
+ .replace(/<\/pre>\s+📋 Copy to clipboard' +
+ '