forked from flucoma/learn-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess.js
164 lines (141 loc) · 3.91 KB
/
preprocess.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import markdownLinkExtractor from 'markdown-link-extractor';
import fg from 'fast-glob';
import path from 'path';
import _ from 'lodash';
import fs from 'fs';
import GithubSlugger from 'github-slugger';
import frontmatter from 'front-matter';
import { markdown } from 'markdown';
import { urlFromRoute, spillToArray, getComponents } from './util.js';
const slugger = new GithubSlugger();
let structure = {};
let tags = {};
let edits = {};
let related = {};
let featured = {
reference: {},
learn: {},
explore: {}
};
let db = [];
const add = (key, reference) => {
if (!(related[key] instanceof Array)) {
related[key] = [reference];
} else {
related[key].push(reference);
}
};
const ignores = [
'src/routes/(content)/reference/+page.svx',
'src/routes/(content)/explore/+page.svx',
'src/routes/(content)/learn/+page.svx',
'src/routes/(content)/+page.svx'
];
let routes = fg.sync(['**/*+page.svx']);
routes = routes.filter(route => !ignores.includes(route));
routes.forEach(route => {
const section = route.split('/')[3];
let url = urlFromRoute(route);
// Read the page in as a string
const data = fs.readFileSync(route, 'utf8');
// Get frontmatter
let fm = frontmatter(data).attributes;
fm.url = url;
fm.section = section;
// Parse the markdown tree for the headings
let tree = markdown.parse(data);
if (!Object.keys(structure).includes(url)) {
structure[url] = [];
}
// iterate over the tree and find headers for ToC
tree.forEach(el => {
if (el[0] === 'header' && el[1].level === 2) {
const rawText = el.slice(2);
const text = rawText
.flat()
.filter(x => !x.includes('em'))
.filter(x => x !== 'inlinecode')
.join('');
slugger.reset();
let hashPart = slugger.slug(text);
structure[url].push({
url: `${url}#${hashPart}`,
text: text
});
}
});
// Tags
if (Object.keys(fm).includes('tags')) {
fm.tags.forEach(tag => {
if (!Object.keys(tags).includes(tag)) {
tags[tag] = [];
}
tags[tag].push(url);
});
}
// Database
let featureInfo = {
featuredimage: '',
images: [],
video: [],
youtube: [],
vimeo: []
};
let componentDict = getComponents(frontmatter(data).body, ['Image', 'Video', 'YouTube', 'Vimeo']);
// These wouldn't necessarily have to be spilled to arrays
featureInfo.images = spillToArray(componentDict.Image, 'src');
featureInfo.video = spillToArray(componentDict.Video, 'url');
featureInfo.youtube = spillToArray(componentDict.YouTube, 'url');
featureInfo.vimeo = spillToArray(componentDict.Vimeo, 'src');
if (Object.keys(fm).includes('featuredimage')) {
featureInfo['featuredimage'] = fm.featuredimage;
} else {
if (componentDict.Image.length != 0) {
featureInfo['featuredimage'] =
componentDict.Image[Math.floor(Math.random() * componentDict.Image.length)].src;
}
}
fm.feature = featureInfo;
db.push(fm);
// // Related Links
let { links } = markdownLinkExtractor(data, false);
links =
links
.filter(x => x.startsWith('/'))
.filter(x => path.extname(x) === '');
links = [...new Set(links)];
let backreference = {
title: fm.title,
flair: fm.flair,
blurb: fm.blurb,
url: url
};
links.forEach(link => {
const length = link.split('/').filter(x => x != '').length; // filter out index.svx type situations
link = link.split('#')[0];
if (length > 1 && link !== url) {
add(link, backreference);
const branch = fs.readFileSync(`src/routes/(content)/${link}/+page.svx`, 'utf8');
const branchfm = frontmatter(branch).attributes;
const fwdreference = {
title: branchfm.title,
flair: branchfm.flair,
blurb: branchfm.blurb,
url: link
};
add(url, fwdreference);
}
});
});
// De-duplicate Related Links
for (const key in related) {
related[key] = _.uniqWith(related[key], _.isEqual);
}
let preprocData = {
tags: tags,
structure: structure,
db: db,
related: related
};
// Write out results
fs.writeFile('src/lib/data/metadata.json', JSON.stringify(preprocData, null, 0), 'utf8', () => {});