-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.js
More file actions
182 lines (167 loc) · 5.06 KB
/
Copy pathdev.js
File metadata and controls
182 lines (167 loc) · 5.06 KB
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 chokidar from "chokidar";
import { fileURLToPath } from "url";
import { createServer } from "vite";
import { Marked } from "marked";
import { markedHighlight } from "marked-highlight";
import hljs from "highlight.js";
import fs from "fs-extra";
const __dirname = fileURLToPath(new URL(".", import.meta.url));
function getNewPath(path) {
let isMarkdownOrHTMLFile = false;
let isHome = path.includes("content/home");
// make sure we don't replace any other path segment named `content` that we don't intend to
let firstContentKeywordReplaced = false;
const pathSegments = path.split("/").map((segment) => {
if (segment === "content" && !firstContentKeywordReplaced) {
firstContentKeywordReplaced = true;
return "pages";
} else {
return segment;
}
});
const lastPathSegment = pathSegments[pathSegments.length - 1];
const newLastPathSegment = lastPathSegment
.split(".")
.map((segment, i) => {
if (i === lastPathSegment.split(".").length - 1) {
if (segment === "md" || segment === "html") {
isMarkdownOrHTMLFile = true;
return "html";
} else {
return segment;
}
} else {
return segment;
}
})
.join(".");
pathSegments[pathSegments.length - 1] = newLastPathSegment;
return [pathSegments.join("/"), isMarkdownOrHTMLFile, isHome];
}
const marked = new Marked(
markedHighlight({
langPrefix: "hljs language-",
highlight(code, lang) {
const language = hljs.getLanguage(lang) ? lang : "plaintext";
return hljs.highlight(code, { language }).value;
},
}),
);
function wrapWithBase(parsedContent) {
return fs
.readFileSync(__dirname + "base.html", "utf8")
.replace("__content_marker__", parsedContent);
}
function parseMarkdown(content) {
return marked.parse(
content.replace(/^[\u200B\u200C\u200D\u200E\u200F\uFEFF]/, ""),
);
}
function handleFileOutput(path) {
const [newPath, isMarkdownOrHTMLFile, isHome] = getNewPath(path);
if (isMarkdownOrHTMLFile) {
if (isHome) {
// output other files if file is for home page
fs.outputFileSync(
__dirname + "index.html",
wrapWithBase(parseMarkdown(fs.readFileSync(path, "utf8"))),
);
fs.outputFileSync(
__dirname + "404.html",
wrapWithBase(
`<div class="not-found"><a href="/">404<br />Oops, Page Not Found<br />Please Click To Go Back Home</a></div>`,
),
);
fs.outputFileSync(
__dirname + "last-updated.txt",
new Date()
.toDateString()
.split(" ")
.filter((_, index) => index !== 0)
.map((value, index) => (index === 1 ? `${value},` : value))
.join(" "),
);
} else {
fs.outputFileSync(
newPath,
wrapWithBase(parseMarkdown(fs.readFileSync(path, "utf8"))),
);
}
} else {
fs.outputFileSync(newPath, fs.readFileSync(path, "utf8"));
}
}
function createContentWatcher() {
const contentWatcher = chokidar.watch(`${__dirname}content`, {
ignored: /(^|[\/\\])\../, // ignore dotfiles
persistent: true,
});
contentWatcher
.on("add", (path) => {
console.log(`File ${path} has been added`);
handleFileOutput(path);
})
.on("change", (path) => {
console.log(`File ${path} has been changed`);
handleFileOutput(path);
})
.on("unlink", (path) => {
console.log(`File ${path} has been removed`);
const [newPath, isMarkdownOrHTMLFile, isHome] = getNewPath(path);
if (isMarkdownOrHTMLFile && !isHome) {
fs.removeSync(newPath);
}
})
.on("addDir", (path) => {
console.log(`Directory ${path} has been added`);
const [newPath, _, isHome] = getNewPath(path);
if (!isHome) {
fs.ensureDirSync(newPath);
}
})
.on("unlinkDir", (path) => {
console.log(`Directory ${path} has been removed`);
const [newPath, _, isHome] = getNewPath(path);
if (!isHome) {
fs.removeSync(newPath);
}
})
.on("error", (error) => {
console.log(`Watcher error: ${error}`);
})
.on("ready", () => {
console.log("Initial scan complete. Ready for changes");
});
return contentWatcher;
}
async function init() {
let contentWatcher = createContentWatcher();
const baseHTMLWatcher = chokidar.watch(
[`${__dirname}base.html`, `${__dirname}assets`],
{
ignored: /(^|[\/\\])\../, // ignore dotfiles
persistent: true,
},
);
baseHTMLWatcher
.on("add", async (path) => {
console.log(`File ${path} has been added`);
contentWatcher.close();
contentWatcher = createContentWatcher();
})
.on("change", async (path) => {
console.log(`File ${path} has been changed`);
contentWatcher.close();
contentWatcher = createContentWatcher();
});
const server = await createServer({
configFile: "vite.config.js",
root: __dirname,
});
await server.listen();
server.printUrls();
server.bindCLIShortcuts({ print: true });
}
init()
.then(() => console.log("Dev started!"))
.catch(() => console.log("Dev failed!"));