-
Notifications
You must be signed in to change notification settings - Fork 0
/
packSite.js
48 lines (46 loc) · 1.63 KB
/
packSite.js
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
const fs = require('fs');
function packSite() {
'use strict';
let list = fs.readdirSync(".");
list = list.filter(a => a.endsWith('.mpat-page')).map(a => a.substring(0, a.length-10));
const all = list.indexOf('all-pages');
if (all >= 0) list.splice(all, 1);
const res = [];
list.forEach((name, i) => {
const obj = JSON.parse(fs.readFileSync(name+'.mpat-page'));
console.log(">>> processing "+name);
obj.clones = [];
obj.page_links = [];
obj.page.ID = i+1;
obj.page.post_parent = "";
obj.page.layoutId = obj.page_layout.ID = i+1+list.length;
Object.keys(obj.page.meta.mpat_content.content).forEach((a) => {
let component = obj.page.meta.mpat_content.content[a];
Object.keys(component).forEach((s) => {
let state = component[s];
if (state.type === 'link') {
if (state.data.url) {
let url = state.data.url;
if (url.startsWith('http://')) {
if (url.endsWith('/#preview')) {
url = url.substring(0, url.length-9);
} else if (url.endsWith('/')) {
url = url.substring(0, url.length-1);
}
url = url.substring(url.lastIndexOf('/')+1);
}
const i = list.indexOf(url);
if (i >= 0) {
console.log("lien "+url+" "+(i+1));
state.data.url = "page://"+(i+1);
obj.page_links.push({path: [a, s, 'data', 'url'], text: "page://"+(i+1), id:(i+1)})
}
}
}
})
});
res.push(obj)
});
return res;
}
fs.writeFileSync("all-pages.mpat-page", JSON.stringify(packSite(), 2));