-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath-validate.js
More file actions
44 lines (39 loc) · 1.01 KB
/
path-validate.js
File metadata and controls
44 lines (39 loc) · 1.01 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
"use strict";
function getQueryPath() {
// Gets URL parameters
let queryString = window.location.search;
let urlParams = new URLSearchParams(queryString);
// return ?path, empty otherwise
return urlParams.get("path") ?? "";
}
// Make sure it points to a markdown file
function isPathMd(path) {
return path.endsWith(".md") || path.endsWith(".markdown");
}
async function isPathValid() {
let path = getQueryPath();
if (path === "") {
let response = await fetch("/index.md");
if (response.ok) {
window.location.replace(window.location.pathname + "?path=/index.md");
return false;
} else {
window.location.replace(window.location.pathname + "?path=/README.md");
return false;
}
}
if (!isPathMd(path)) {
// open without saving current url to history
window.location.replace(path);
return false;
}
return true;
}
// global variable
let run_main;
isPathValid().then(val => {
run_main = val;
if (typeof entry !== "undefined" && run_main) {
entry();
}
});