summaryrefslogtreecommitdiffstats
path: root/netlify-preview.js
diff options
context:
space:
mode:
authorSerghei Iakovlev <egrep@protonmail.ch>2024-04-13 09:04:18 +0200
committerSerghei Iakovlev <egrep@protonmail.ch>2024-04-13 09:44:25 +0200
commit0c6aad8d28d9398dc36c70149e221f3af62203cc (patch)
tree82feb54ad7f30d39be2d28b8a3a111a4d7698f76 /netlify-preview.js
parent69e11ac3fa82228ee544cc5423f344d84d1569f8 (diff)
downloadgohugo-theme-ed-0c6aad8d28d9398dc36c70149e221f3af62203cc.tar.gz
Replace Go script with Node.js implementation
Diffstat (limited to 'netlify-preview.js')
-rw-r--r--netlify-preview.js58
1 files changed, 58 insertions, 0 deletions
diff --git a/netlify-preview.js b/netlify-preview.js
new file mode 100644
index 0000000..5b37433
--- /dev/null
+++ b/netlify-preview.js
@@ -0,0 +1,58 @@
+'use strict';
+
+const fs = require('fs');
+const path = require('path');
+
+const netlifyConfig = 'netlify.toml';
+
+// Read the netlify.toml file
+fs.readFile(path.resolve(__dirname, netlifyConfig), 'utf8', (err, data) => {
+ if (err) {
+ console.error('Error reading file: ', err);
+ return;
+ }
+
+ // Split the file content into lines
+ const lines = data.split('\n');
+
+ // Process each line
+ const processedLines = lines.map((line) => {
+ // -> default-src 'self';
+ // <- default-src 'self' blob:;
+ line = line.replace(/(default-src) ('self')(;)/, '$1 $2 blob:$3');
+
+ // -> style-src 'self' cdn.hypothes.is;
+ // <- style-src 'self' 'unsafe-inline' cdn.hypothes.is;
+ line = line.replace(/(style-src) ('self') (cdn\.hypothes\.is)(;)/, '$1 $2 $3 \'unsafe-inline\'$4');
+
+ // -> media-src 'self';
+ // <- media-src 'self' blob: https://app.netlify.com;
+ line = line.replace(/(media-src) ('self')(;)/, '$1 $2 blob: https://app.netlify.com$3');
+
+ // -> frame-src hypothes.is;
+ // <- frame-src hypothes.is app.netlify.com;
+ line = line.replace(/(frame-src) (hypothes\.is)(;)/, '$1 $2 app.netlify.com$3');
+
+ // -> script-src 'self' www.googletagmanager.com hypothes.is cdn.hypothes.is;
+ // <- script-src 'self' www.googletagmanager.com hypothes.is cdn.hypothes.is netlify-cdp-loader.netlify.app;
+ line = line.replace(
+ /(script-src) ('self' www\.googletagmanager\.com hypothes\.is cdn\.hypothes\.is)(;)/,
+ '$1 $2 netlify-cdp-loader.netlify.app$3'
+ );
+
+ return line;
+ });
+
+ // Join the processed lines back into a single string
+ const output = processedLines.join('\n');
+
+ // Write the modified content back to the file
+ fs.writeFile(path.resolve(__dirname, netlifyConfig), output, 'utf8', (err) => {
+ if (err) {
+ console.error('Error writing file:', err);
+ return;
+ }
+
+ console.log('Done');
+ });
+});