-
Notifications
You must be signed in to change notification settings - Fork 1
/
gen.js
182 lines (156 loc) · 4.73 KB
/
gen.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { marked } from 'marked';
import fs from 'fs';
import { join } from 'path';
import sizeOf from 'image-size';
import indexhtml from './tmplt/indexhtml.js';
import galleryhtml from './tmplt/galleryhtml.js';
let currentDir = './public';
let currentDirList = [];
const dirList = {
name: 'dirList',
level: 'block',
start(src) { return src.match(/\$ dirlist/)?.index; },
tokenizer(src, tokens) {
const rule = /^\$ dirlist/;
const match = rule.exec(src);
if (match) {
const token = {
type: 'dirList',
raw: match[0],
text: match[0].trim(),
tokens: []
};
return token;
}
},
renderer(token) {
const links = currentDirList.map(
(dir) => `<a href="./${dir}">${dir}</a>`
);
return `<nav class="dirlist">${links.join('')}</nav>`;
}
};
const breadCrumbs = {
name: 'breadCrumbs',
level: 'block',
start(src) { return src.match(/\$ breadcrumbs/)?.index; },
tokenizer(src, tokens) {
const rule = /^\$ breadcrumbs/;
const match = rule.exec(src);
if (match) {
const token = {
type: 'breadCrumbs',
raw: match[0],
text: match[0].trim(),
tokens: []
};
return token;
}
},
renderer(token) {
const crumbs = currentDir.split('/');
const crumbLength = crumbs.length;
const links = crumbs.map((dir, i) => {
if (i === 0) {
return `<a href="/">Home</a>`
}
if (i === crumbLength - 1) {
return `<span>${dir}</span>`;
}
return `<a href="${Array(crumbLength - 1 - i).fill('../').join()}">${dir}</a>`;
});
return `<nav class="breadcrumbs">${links.join(' > ')}</nav>`;
}
};
const gallery = {
name: 'gallery',
level: 'block',
start(src) { return src.match(/\$ gallery/)?.index; },
tokenizer(src, tokens) {
const rule = /^\$ gallery/;
const match = rule.exec(src);
if (match) {
const token = {
type: 'gallery',
raw: match[0],
text: match[0].trim(),
tokens: []
};
return token;
}
},
renderer(token) {
console.log(currentDir);
const galleryDir = `${currentDir}/_gallery`;
const smallJpg = fs.readdirSync(galleryDir).filter((name) => name.match(/small\.jpg$/));
if (!smallJpg.length) {
console.error(galleryDir);
throw new Error('gallery requires small jpgs');
}
const withInfo = smallJpg.map((filename) => {
const imgData = {
filename: filename,
...sizeOf(`${currentDir}/_gallery/${filename}`)
}
return imgData;
});
return galleryhtml(withInfo);
}
};
marked.use({ extensions: [dirList, breadCrumbs, gallery] });
const mdEnding = /\.md$/;
function getDirContents(path) {
return new Promise((resolve, reject) => {
fs.readdir(path, { withFileTypes: true }, (err, files) => {
if (err) throw err;
const jpgs = files.filter((file) => file.name.match(/\.jpg$/)).map((file) => file.name);
resolve({
dir: files.filter((file) => file.isDirectory()).filter((file) => file.name.match(/^[^_]/)).map((file) => file.name),
md: files.filter((file) => file.name.match(mdEnding)).map((file) => file.name),
jpg: jpgs,
smallJpg: jpgs.filter((name) => name.match(/small\.jpg$/))
})
});
});
}
function processMarkdown(path) {
return new Promise((resolve, reject) => {
if (!path.match(mdEnding)) {
throw new Error('processMarkdown: path should end with .md');
}
fs.readFile(path, 'utf8', (err, md) => {
if (err) throw err;
const html = marked.parse(md);
const fullHtml = indexhtml({ body: html });
const filePath = path.replace(mdEnding, '.html');
fs.writeFile(filePath, fullHtml, (err) => {
if (err) throw err;
console.log(`wrote file ${filePath}`);
resolve();
});
});
});
}
async function processDirectory(path) {
return new Promise(async (resolve) => {
console.log(`process directory ${path}`);
currentDir = path;
const dirContents = await getDirContents(path);
currentDirList = dirContents.dir;
console.log(`process markdown files: ${dirContents.md.join(', ')}`);
for (let i = 0; i < dirContents.md.length; i++) {
await processMarkdown(join(currentDir, dirContents.md[i]))
}
resolve(dirContents.dir);
});
}
async function processDirectoryTree(path) {
const dirs = await processDirectory(path);
for (let i = 0; i < dirs.length; i++) {
await processDirectoryTree(join(path, dirs[i]));
}
}
(async () => {
await processDirectoryTree('./public');
console.log('finished');
})();