UNPKG

1.08 kBJavaScriptView Raw
1const lzString = require('lz-string');
2
3const createUrl = ({ baseUrl, code, themes, widths }) => {
4 let path = '';
5
6 if (code || themes || widths) {
7 const data = JSON.stringify({
8 ...(code ? { code } : {}),
9 ...(themes ? { themes } : {}),
10 ...(widths ? { widths } : {}),
11 });
12
13 const compressedData = lzString.compressToEncodedURIComponent(data);
14 path = `#?code=${compressedData}`;
15 }
16
17 if (baseUrl) {
18 const trimmedBaseUrl = baseUrl.replace(/\/$/, '');
19
20 return `${trimmedBaseUrl}/${path}`;
21 }
22
23 return path;
24};
25
26const createPreviewUrl = ({ baseUrl, code, theme }) => {
27 let path = '';
28
29 if (code || theme) {
30 const data = JSON.stringify({
31 ...(code ? { code } : {}),
32 ...(theme ? { theme } : {}),
33 });
34
35 const compressedData = lzString.compressToEncodedURIComponent(data);
36 path = `/preview#?code=${compressedData}`;
37 }
38
39 if (baseUrl) {
40 const trimmedBaseUrl = baseUrl.replace(/\/$/, '');
41
42 return `${trimmedBaseUrl}${path}`;
43 }
44
45 return path;
46};
47
48module.exports = {
49 createUrl,
50 createPreviewUrl,
51};