-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__repair_embed.js
More file actions
19 lines (19 loc) · 1.01 KB
/
__repair_embed.js
File metadata and controls
19 lines (19 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const fs = require('fs');
const path = 'd:/AlgoCard';
const dataPath = `${path}/data/algorithms.json`;
const htmlPath = `${path}/index.html`;
const dataRaw = fs.readFileSync(dataPath, 'utf8');
const data = JSON.parse(dataRaw);
const pretty = JSON.stringify(data, null, 2);
let html = fs.readFileSync(htmlPath, 'utf8');
const startTag = '<script id="fallback-data" type="application/json">';
const endTag = '</script>';
const start = html.indexOf(startTag);
if (start === -1) throw new Error('fallback-data tag not found');
const end = html.indexOf(endTag, start + startTag.length);
if (end === -1) throw new Error('fallback-data end tag not found');
html = html.slice(0, start + startTag.length) + '\n' + pretty + '\n' + html.slice(end);
fs.writeFileSync(htmlPath, html, 'utf8');
const verify = html.slice(html.indexOf(startTag) + startTag.length, html.indexOf(endTag, html.indexOf(startTag) + startTag.length)).trim();
const parsed = JSON.parse(verify);
console.log('embedded repaired', Array.isArray(parsed) ? parsed.length : 0);