import User from '../models/user'; import Project from '../models/project'; export function get404Sketch(callback) { User.findOne({ username: 'p5' }, (userErr, user) => { // Find p5 user if (userErr) { throw userErr; } else { 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 const html = htmlFile.split(''); html[0] = ` ${html[0]} 404 Page Not Found - p5.js Web Editor `; html[1] = `

404 Page Not Found

The page you are trying to reach does not exist.
Please check the URL or return to the home page.
${html[1]} `; htmlFile = html.join(''); // 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); }); } }); } export default get404Sketch;