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