2016-09-05 20:08:08 +00:00
|
|
|
import Project from '../models/project';
|
|
|
|
import escapeStringRegexp from 'escape-string-regexp';
|
|
|
|
const startTag = '@fs-';
|
2016-11-08 21:50:21 +00:00
|
|
|
import { resolvePathToFile } from '../utils/filePath';
|
2016-11-16 18:12:36 +00:00
|
|
|
import {
|
|
|
|
injectMediaUrls,
|
|
|
|
resolvePathsForElementsWithAttribute,
|
|
|
|
resolveScripts,
|
|
|
|
resolveStyles } from '../utils/previewGeneration';
|
|
|
|
import jsdom, { serializeDocument } from 'jsdom';
|
2016-09-05 20:08:08 +00:00
|
|
|
|
|
|
|
export function serveProject(req, res) {
|
|
|
|
Project.findById(req.params.project_id)
|
|
|
|
.exec((err, project) => {
|
2016-11-08 21:50:21 +00:00
|
|
|
//TODO this does not parse html
|
2016-09-05 20:08:08 +00:00
|
|
|
const files = project.files;
|
|
|
|
let htmlFile = files.find(file => file.name.match(/\.html$/i)).content;
|
2016-11-16 18:12:36 +00:00
|
|
|
const filesToInject = files.filter(file => file.name.match(/\.(js|css)$/i));
|
|
|
|
injectMediaUrls(filesToInject, files, req.params.project_id);
|
2016-09-05 20:08:08 +00:00
|
|
|
|
2016-11-16 18:12:36 +00:00
|
|
|
jsdom.env(htmlFile, (err, window) => {
|
|
|
|
const sketchDoc = window.document;
|
|
|
|
resolvePathsForElementsWithAttribute('src', sketchDoc, files);
|
|
|
|
resolvePathsForElementsWithAttribute('href', sketchDoc, files);
|
|
|
|
resolveScripts(sketchDoc, files);
|
|
|
|
resolveStyles(sketchDoc, files);
|
2016-09-05 20:08:08 +00:00
|
|
|
|
2016-11-16 18:12:36 +00:00
|
|
|
res.send(serializeDocument(sketchDoc));
|
2016-09-05 20:08:08 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|