-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgatsby-wrapper.ts
63 lines (56 loc) · 1.91 KB
/
gatsby-wrapper.ts
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
import fs from "fs"
export const store = {}
export const cache = {}
export function createNodeId() {}
export function createContentDigest() {}
export const reporter = {
warn: (str) => console.warn(str),
panic: (err, str) => console.error(err, str),
activityTimer: (str) => {
console.log(new Date(), str);
return {
start: () => console.log('start: ', new Date()),
setStatus: (str) => console.log('setStatus: ', str),
end: () => console.log('end: ', new Date())
}
},
}
export function createNode({ internal, dir, ...node }) {
// for cover images:
const coverImageRegex = /cover:\n image: '(.*)'/m;
if (coverImageRegex.test(node.markdown)) {
const url = node.markdown.match(coverImageRegex)[1];
node.markdown = node.markdown.replace(url, node.cover.image);
}
writeFileSyncRecursive(`${process.env.MARKDOWN_PATH}${node.path}.md`, node.markdown, "utf-8")
}
// https://gist.github.com/drodsou/de2ba6291aea67ffc5bc4b52d8c32abd
function writeFileSyncRecursive(filename, content, charset) {
// -- normalize path separator to '/' instead of path.sep,
// -- as / works in node for Windows as well, and mixed \\ and / can appear in the path
let filepath = filename.replace(/\\/g,'/');
// -- preparation to allow absolute paths as well
let root = '';
if (filepath[0] === '/') {
root = '/';
filepath = filepath.slice(1);
}
else if (filepath[1] === ':') {
root = filepath.slice(0,3); // c:\
filepath = filepath.slice(3);
}
// -- create folders all the way down
const folders = filepath.split('/').slice(0, -1); // remove last item, file
folders.reduce(
(acc, folder) => {
const folderPath = acc + folder + '/';
if (!fs.existsSync(folderPath)) {
fs.mkdirSync(folderPath);
}
return folderPath
},
root // first 'acc', important
);
// -- write file
fs.writeFileSync(root + filepath, content, charset);
}