-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathpreviewGeneration.js
More file actions
110 lines (103 loc) · 3.75 KB
/
Copy pathpreviewGeneration.js
File metadata and controls
110 lines (103 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { resolvePathToFile } from './filePath';
import { resolvePathsForElementsWithAttribute } from './resolveUtils';
import {
STRING_REGEX,
PLAINTEXT_FILE_REGEX,
EXTERNAL_LINK_REGEX,
NOT_EXTERNAL_LINK_REGEX
} from './fileUtils';
function resolveLinksInString(content, files, projectId) {
let newContent = content;
let fileStrings = content.match(STRING_REGEX);
const fileStringRegex = /^('|")(?!(http:\/\/|https:\/\/)).*('|")$/i;
fileStrings = fileStrings || [];
fileStrings.forEach((fileString) => {
// if string does not begin with http or https
if (fileString.match(fileStringRegex)) {
const filePath = fileString.substr(1, fileString.length - 2);
const resolvedFile = resolvePathToFile(filePath, files);
if (resolvedFile) {
if (resolvedFile.url) {
newContent = newContent.replace(filePath, resolvedFile.url);
} else if (resolvedFile.name.match(PLAINTEXT_FILE_REGEX)) {
let resolvedFilePath = filePath;
if (resolvedFilePath.startsWith('.')) {
resolvedFilePath = resolvedFilePath.substr(1);
}
while (resolvedFilePath.startsWith('/')) {
resolvedFilePath = resolvedFilePath.substr(1);
}
const replacement = `/sketches/${projectId}/assets/${resolvedFilePath}`;
newContent = newContent.replace(filePath, replacement);
}
}
}
});
return newContent;
}
export function injectMediaUrls(filesToInject, allFiles, projectId) {
filesToInject.forEach((file) => {
file.content = resolveLinksInString(file.content, allFiles, projectId);
});
}
export function resolveMediaElements(sketchDoc, files) {
resolvePathsForElementsWithAttribute('src', sketchDoc, files);
}
export function resolveScripts(sketchDoc, files, projectId) {
const scriptsInHTML = sketchDoc.getElementsByTagName('script');
const scriptsInHTMLArray = Array.prototype.slice.call(scriptsInHTML);
scriptsInHTMLArray.forEach((script) => {
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;
}
}
} else if (
!(
script.getAttribute('src') &&
script.getAttribute('src').match(EXTERNAL_LINK_REGEX) !== null
)
) {
script.innerHTML = resolveLinksInString(
script.innerHTML,
files,
projectId
);
}
});
}
export function resolveStyles(sketchDoc, files, projectId) {
const inlineCSSInHTML = sketchDoc.getElementsByTagName('style');
const inlineCSSInHTMLArray = Array.prototype.slice.call(inlineCSSInHTML);
inlineCSSInHTMLArray.forEach((style) => {
style.innerHTML = resolveLinksInString(style.innerHTML, files, projectId);
});
const cssLinksInHTML = sketchDoc.querySelectorAll('link[rel="stylesheet"]');
const cssLinksInHTMLArray = Array.prototype.slice.call(cssLinksInHTML);
cssLinksInHTMLArray.forEach((css) => {
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);
}
}
}
});
}