2016-11-16 18:12:36 +00:00
|
|
|
import { resolvePathToFile } from '../utils/filePath';
|
2018-02-01 21:45:19 +00:00
|
|
|
|
|
|
|
import {
|
|
|
|
MEDIA_FILE_REGEX,
|
|
|
|
STRING_REGEX,
|
2018-05-30 22:23:32 +00:00
|
|
|
PLAINTEXT_FILE_REGEX,
|
2018-02-01 21:45:19 +00:00
|
|
|
EXTERNAL_LINK_REGEX,
|
|
|
|
NOT_EXTERNAL_LINK_REGEX
|
|
|
|
} from './fileUtils';
|
2016-11-16 18:12:36 +00:00
|
|
|
|
|
|
|
function resolveLinksInString(content, files, projectId) {
|
|
|
|
let newContent = content;
|
|
|
|
let fileStrings = content.match(STRING_REGEX);
|
|
|
|
const fileStringRegex = /^('|")(?!(http:\/\/|https:\/\/)).*('|")$/i;
|
|
|
|
fileStrings = fileStrings || [];
|
2017-02-22 19:29:35 +00:00
|
|
|
fileStrings.forEach((fileString) => {
|
2016-11-17 16:15:35 +00:00
|
|
|
// if string does not begin with http or https
|
2016-11-16 18:12:36 +00:00
|
|
|
if (fileString.match(fileStringRegex)) {
|
|
|
|
const filePath = fileString.substr(1, fileString.length - 2);
|
|
|
|
const resolvedFile = resolvePathToFile(filePath, files);
|
|
|
|
if (resolvedFile) {
|
|
|
|
if (resolvedFile.url) {
|
2016-11-17 16:15:35 +00:00
|
|
|
newContent = newContent.replace(filePath, resolvedFile.url);
|
2018-05-30 22:23:32 +00:00
|
|
|
} else if (resolvedFile.name.match(PLAINTEXT_FILE_REGEX)) {
|
2016-11-16 18:12:36 +00:00
|
|
|
let resolvedFilePath = filePath;
|
|
|
|
if (resolvedFilePath.startsWith('.')) {
|
|
|
|
resolvedFilePath = resolvedFilePath.substr(1);
|
|
|
|
}
|
|
|
|
while (resolvedFilePath.startsWith('/')) {
|
|
|
|
resolvedFilePath = resolvedFilePath.substr(1);
|
|
|
|
}
|
2018-07-03 20:02:46 +00:00
|
|
|
const replacement = `/sketches/${projectId}/assets/${resolvedFilePath}`;
|
|
|
|
newContent = newContent.replace(filePath, replacement);
|
2016-11-16 18:12:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return newContent;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function injectMediaUrls(filesToInject, allFiles, projectId) {
|
2017-02-22 19:29:35 +00:00
|
|
|
filesToInject.forEach((file) => {
|
2016-11-16 18:12:36 +00:00
|
|
|
file.content = resolveLinksInString(file.content, allFiles, projectId);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function resolvePathsForElementsWithAttribute(attr, sketchDoc, files) {
|
|
|
|
const elements = sketchDoc.querySelectorAll(`[${attr}]`);
|
|
|
|
const elementsArray = Array.prototype.slice.call(elements);
|
2017-02-22 19:29:35 +00:00
|
|
|
elementsArray.forEach((element) => {
|
2018-02-01 21:45:19 +00:00
|
|
|
if (element.getAttribute(attr).match(MEDIA_FILE_REGEX)) {
|
2016-11-16 18:12:36 +00:00
|
|
|
const resolvedFile = resolvePathToFile(element.getAttribute(attr), files);
|
2018-02-01 21:45:19 +00:00
|
|
|
if (resolvedFile && resolvedFile.url) {
|
2016-11-16 18:12:36 +00:00
|
|
|
element.setAttribute(attr, resolvedFile.url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function resolveScripts(sketchDoc, files, projectId) {
|
2016-11-17 16:15:35 +00:00
|
|
|
const scriptsInHTML = sketchDoc.getElementsByTagName('script');
|
|
|
|
const scriptsInHTMLArray = Array.prototype.slice.call(scriptsInHTML);
|
2017-02-22 19:29:35 +00:00
|
|
|
scriptsInHTMLArray.forEach((script) => {
|
2016-11-17 16:15:35 +00:00
|
|
|
if (script.getAttribute('src') && script.getAttribute('src').match(NOT_EXTERNAL_LINK_REGEX) !== null) {
|
|
|
|
const resolvedFile = resolvePathToFile(script.getAttribute('src'), files);
|
|
|
|
if (resolvedFile) {
|
|
|
|
if (resolvedFile.url) {
|
|
|
|
script.setAttribute('src', resolvedFile.url);
|
|
|
|
} else {
|
|
|
|
script.removeAttribute('src');
|
|
|
|
script.innerHTML = resolvedFile.content;
|
2016-11-16 18:12:36 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-17 16:15:35 +00:00
|
|
|
} else if (!(script.getAttribute('src') && script.getAttribute('src').match(EXTERNAL_LINK_REGEX) !== null)) {
|
|
|
|
script.innerHTML = resolveLinksInString(script.innerHTML, files, projectId);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-11-16 18:12:36 +00:00
|
|
|
|
|
|
|
export function resolveStyles(sketchDoc, files, projectId) {
|
2016-11-17 16:15:35 +00:00
|
|
|
const inlineCSSInHTML = sketchDoc.getElementsByTagName('style');
|
|
|
|
const inlineCSSInHTMLArray = Array.prototype.slice.call(inlineCSSInHTML);
|
2017-02-22 19:29:35 +00:00
|
|
|
inlineCSSInHTMLArray.forEach((style) => {
|
2016-11-17 16:15:35 +00:00
|
|
|
style.innerHTML = resolveLinksInString(style.innerHTML, files, projectId);
|
|
|
|
});
|
2016-11-16 18:12:36 +00:00
|
|
|
|
2016-11-17 16:15:35 +00:00
|
|
|
const cssLinksInHTML = sketchDoc.querySelectorAll('link[rel="stylesheet"]');
|
|
|
|
const cssLinksInHTMLArray = Array.prototype.slice.call(cssLinksInHTML);
|
2017-02-22 19:29:35 +00:00
|
|
|
cssLinksInHTMLArray.forEach((css) => {
|
2016-11-17 16:15:35 +00:00
|
|
|
if (css.getAttribute('href') && css.getAttribute('href').match(NOT_EXTERNAL_LINK_REGEX) !== null) {
|
|
|
|
const resolvedFile = resolvePathToFile(css.getAttribute('href'), files);
|
|
|
|
if (resolvedFile) {
|
|
|
|
if (resolvedFile.url) {
|
|
|
|
css.setAttribute('href', resolvedFile.url);
|
|
|
|
} else {
|
|
|
|
const style = sketchDoc.createElement('style');
|
|
|
|
style.innerHTML = `\n${resolvedFile.content}`;
|
|
|
|
sketchDoc.getElementsByTagName('head')[0].appendChild(style);
|
|
|
|
css.parentNode.removeChild(css);
|
2016-11-16 18:12:36 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-17 16:15:35 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|