forked from FlatFilers/Guides
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
115 lines (107 loc) · 3.56 KB
/
script.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
function simpleMarkdownToHtml(markdown) {
let htmlContent = markdown
// Remove commit number patterns
.replace(/\b[0-9a-f]{7}:\s/gim, "")
// Headers, blockquotes, and other formatting
.replace(
/^## (.*$)/gim,
"<h2 id='version_$1'><br/><a href='#version_$1'>$1</a></h2>"
)
.replace(/^### (.*$)/gim, "<h3 style='margin-top: 15px'>$1</h3>")
.replace(/^#### (.*$)/gim, "<code>$1</code>")
.replace(/^# (.*$)/gim, "")
.replace(/^\> (.*$)/gim, "<blockquote>$1</blockquote>")
.replace(/\*\*(.*)\*\*/gim, "<strong>$1</strong>")
.replace(/\*(.*)\*/gim, "<em>$1</em>")
.replace(/!\[(.*?)\]\((.*?)\)/gim, "<img alt='$1' src='$2'>")
.replace(/\[(.*?)\]\((.*?)\)/gim, "<a href='$2'>$1</a>")
.split("\n");
// Process lists
let inList = false;
let listLevel = 0;
for (let i = 0; i < htmlContent.length; i++) {
const line = htmlContent[i];
const trimmedLine = line.trim();
const currentLevel = line.indexOf(trimmedLine) / 2; // Assuming 2 spaces for indentation
if (/^- /.test(trimmedLine)) {
if (!inList) {
inList = true;
listLevel = currentLevel;
if (trimmedLine.includes("Updated dependencies")) {
htmlContent[i] = "<strong>Updated dependencies</strong>";
} else {
htmlContent[i] = "<ul><li>" + trimmedLine.substring(2) + "</li>";
}
} else {
if (trimmedLine.includes("Updated dependencies")) {
htmlContent[i] = "<strong>Updated dependencies</strong>";
htmlContent[i - 1] += "</li></ul>".repeat(listLevel + 1);
inList = false;
listLevel = 0;
} else {
htmlContent[i] = "<li>" + trimmedLine.substring(2) + "</li>";
}
}
} else {
if (inList) {
htmlContent[i - 1] += "</li></ul>".repeat(listLevel + 1);
inList = false;
listLevel = 0;
}
}
}
if (inList) {
htmlContent[htmlContent.length - 1] += "</li></ul>".repeat(listLevel + 1);
}
return htmlContent.join("\n");
}
let lastPath = window.location.pathname;
// Function to handle the route logic
function handleRouteChange(path) {
console.log(`Route changed to: ${path}`);
let segments = path.split("/");
let lastSegment = segments[segments.length - 1];
if (path.includes("changelog/sdks/")) {
fetch(
"https://raw.githubusercontent.com/FlatFilers/" +
(path.includes("plugins")
? "flatfile-plugins"
: "flatfile-core-libraries") +
"/main/" +
(path.includes("utils")
? "utils"
: path.includes("plugins")
? "plugins"
: "packages") +
"/" +
lastSegment +
"/CHANGELOG.md"
)
.then((response) => {
if (!response.ok) {
throw new Error("Network response was not ok");
}
return response.text();
})
.then((markdown) => {
const htmlContent = simpleMarkdownToHtml(markdown);
// Wrapping the converted HTML content in a div
document.getElementById(
"dynamic-content"
).innerHTML = `<div class='mt-8 relative prose prose-gray dark:prose-invert'>${htmlContent}</div>`;
})
.catch((error) => {
console.error("Fetching Markdown file failed:", error);
});
}
}
// Check the route on initial load
handleRouteChange(lastPath);
// Then set up an interval to check for route changes
setInterval(() => {
const currentPath = window.location.pathname;
if (currentPath !== lastPath) {
lastPath = currentPath;
handleRouteChange(currentPath);
}
}, 1000); // Check every second