summaryrefslogtreecommitdiffstats
path: root/netlify-preview.js
blob: 5b37433af259aeac0ee765a83b925c3fc28ed0d1 (plain)
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
'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');
  });
});