e87390adb9
* update eslint and dependencies, fix linting errors that can be fixed with --fix * fix lots of linting errors * update eslintrc, fix some linting errors * fix all server side linting errors, untested * fix errors that fixing linting errors had caused * fix client side eslint errors * fix client side linting errors * fix refs lint errors * fix more linting errors * update eslint and dependencies, fix linting errors that can be fixed with --fix * fix lots of linting errors * update eslintrc, fix some linting errors * fix all server side linting errors, untested * fix errors that fixing linting errors had caused * fix client side eslint errors * fix client side linting errors * fix refs lint errors * fix more linting errors * fix some accessibility linting errors * fix a lot of linting errors * fix a billion more linting errors * hopefully fix all linting errors, still need to test * fix bugs that fixing linting had caused
36 lines
1.3 KiB
JavaScript
36 lines
1.3 KiB
JavaScript
import jsdom, { serializeDocument } from 'jsdom';
|
|
import Project from '../models/project';
|
|
import {
|
|
injectMediaUrls,
|
|
resolvePathsForElementsWithAttribute,
|
|
resolveScripts,
|
|
resolveStyles } from '../utils/previewGeneration';
|
|
|
|
export function serveProject(req, res) {
|
|
Project.findById(req.params.project_id)
|
|
.exec((err, project) => {
|
|
// TODO this does not parse html
|
|
const files = project.files;
|
|
const htmlFile = files.find(file => file.name.match(/\.html$/i)).content;
|
|
const filesToInject = files.filter(file => file.name.match(/\.(js|css)$/i));
|
|
injectMediaUrls(filesToInject, files, req.params.project_id);
|
|
|
|
jsdom.env(htmlFile, (innerErr, window) => {
|
|
const sketchDoc = window.document;
|
|
|
|
const base = sketchDoc.createElement('base');
|
|
const fullUrl = `${req.protocol}://${req.get('host')}${req.originalUrl}`;
|
|
base.href = `${fullUrl}/`;
|
|
sketchDoc.head.appendChild(base);
|
|
|
|
resolvePathsForElementsWithAttribute('src', sketchDoc, files);
|
|
resolvePathsForElementsWithAttribute('href', sketchDoc, files);
|
|
resolveScripts(sketchDoc, files);
|
|
resolveStyles(sketchDoc, files);
|
|
|
|
res.send(serializeDocument(sketchDoc));
|
|
});
|
|
});
|
|
}
|
|
|
|
export default serveProject;
|