import User from '../models/user'; import Project from '../models/project'; function insertErrorMessage(htmlFile) { const html = htmlFile.split(''); const metaDescription = 'A web editor for p5.js, a JavaScript library with the goal of making coding accessible to artists, designers, educators, and beginners.'; // eslint-disable-line html[0] = ` ${html[0]} 404 Page Not Found - p5.js Web Editor `; const body = html[1].split(''); html[1] = ` ${body[0]}

404 Page Not Found

The page you are trying to reach does not exist.
Please check the URL or return to the home page.
${body[1]} `; return html.join(''); } export function get404Sketch(callback) { User.findOne({ username: 'p5' }, (userErr, user) => { // Find p5 user if (userErr) { throw userErr; } else if (user) { Project.find({ user: user._id }, (projErr, projects) => { // Find example projects // Choose a random sketch const randomIndex = Math.floor(Math.random() * projects.length); const sketch = projects[randomIndex]; let instanceMode = false; // Get sketch files let htmlFile = sketch.files.filter(file => file.name.match(/.*\.html$/i))[0].content; const jsFiles = sketch.files.filter(file => file.name.match(/.*\.js$/i)); const cssFiles = sketch.files.filter(file => file.name.match(/.*\.css$/i)); const linkedFiles = sketch.files.filter(file => file.url); instanceMode = jsFiles.find(file => file.name === 'sketch.js').content.includes('Instance Mode'); jsFiles.forEach((file) => { // Add js files as script tags const html = htmlFile.split(''); html[0] = `${html[0]}`; htmlFile = html.join(''); }); cssFiles.forEach((file) => { // Add css files as style tags const html = htmlFile.split(''); html[0] = `${html[0]}`; htmlFile = html.join(''); }); linkedFiles.forEach((file) => { // Add linked files as link tags const html = htmlFile.split(''); html[1] = `${html[1]}`; htmlFile = html.join(''); }); // Add 404 html and position canvas htmlFile = insertErrorMessage(htmlFile); // Fix links to assets htmlFile = htmlFile.replace( /'assets/g, "'https://rawgit.com/processing/p5.js-website/master/dist/assets/examples/assets/" ); htmlFile = htmlFile.replace( /"assets/g, '"https://rawgit.com/processing/p5.js-website/master/dist/assets/examples/assets/' ); // Change canvas size htmlFile = htmlFile.replace(/createCanvas\(\d+, ?\d+/g, instanceMode ? 'createCanvas(p.windowWidth, p.windowHeight' : 'createCanvas(windowWidth, windowHeight'); callback(htmlFile); }); } else { callback(insertErrorMessage(` `)); } }); } export default get404Sketch;