Skip to content

Commit 6c2afe0

Browse files
committed
made it possible to load markdown at runtime (janky af)
1 parent a2d77ad commit 6c2afe0

4 files changed

Lines changed: 111 additions & 4 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import MarkdownConverter from "/Scripts/runtimeMarkdown.js";
2+
3+
document.addEventListener('DOMContentLoaded', () => {
4+
const converter = new MarkdownConverter();
5+
6+
const test = converter.parse("# Sample\n\n**Bold Text**")
7+
const html = converter.convertToHTML(test)
8+
document.querySelector('.text').innerHTML = html;
9+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>UltraModding wiki</title>
7+
<link rel="stylesheet" href="../styles.css">
8+
</head>
9+
<body>
10+
<div id="sidebar"></div>
11+
12+
<div id="content" class="content">
13+
<div class="textbox">
14+
<div class="text">
15+
16+
</div>
17+
</div>
18+
</div>
19+
20+
<script type="module" src="/Scripts/runtimeMarkdown.js"></script>
21+
<script type="module" src="Collector.js"></script>
22+
<script src="/Scripts/sidebar.js"></script>
23+
<script src="/Scripts/dropdown.js"></script>
24+
25+
</body>
26+
</html>

Scripts/markdown-converter.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,17 @@ class MarkdownConverter {
6363
}
6464
return html.replace(/\n/g, '<br>');
6565
}
66-
67-
6866
}
67+
//export default MarkdownConverter;
6968
document.addEventListener('DOMContentLoaded', () => {
7069
const scriptTag = document.querySelector('script[src*="markdown-converter.js"]');
7170
const file = scriptTag?.getAttribute('data-md');
72-
71+
72+
if (file == null)
73+
{
74+
return;
75+
}
76+
7377
fetch(file)
7478
.then(response => response.text())
7579
.then(markdown => {
@@ -78,4 +82,5 @@ document.addEventListener('DOMContentLoaded', () => {
7882
updateContent();
7983
})
8084
.catch(err => console.error('Error loading markdown:', err));
81-
});
85+
});
86+

Scripts/runtimeMarkdown.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
class MarkdownConverter {
2+
parse(markdown) {
3+
let result = '';
4+
let currentContent = '';
5+
const lines = markdown.split('\n');
6+
let i = 0;
7+
8+
while (i < lines.length) {
9+
const line = lines[i];
10+
11+
if (line.startsWith('---os:')) {
12+
if (currentContent) {
13+
result += this.convertToHTML(currentContent);
14+
currentContent = '';
15+
}
16+
17+
const osType = line.replace('---os:', '').trim();
18+
let osContent = '';
19+
i++;
20+
21+
while (i < lines.length && !lines[i].includes('---end---')) {
22+
osContent += lines[i] + '\n';
23+
i++;
24+
}
25+
26+
result += `
27+
<div class="OSSpesific">
28+
<div class="os-content" data-os="windows">${osType === 'windows' ? this.convertToHTML(osContent) : ''}</div>
29+
<div class="os-content" data-os="mac">${osType === 'mac' ? this.convertToHTML(osContent) : ''}</div>
30+
<div class="os-content" data-os="linux">${osType === 'linux' ? this.convertToHTML(osContent) : ''}</div>
31+
</div>
32+
`;
33+
} else {
34+
currentContent += line + '\n';
35+
}
36+
37+
i++;
38+
}
39+
40+
if (currentContent) {
41+
result += this.convertToHTML(currentContent);
42+
}
43+
44+
return result;
45+
}
46+
convertToHTML(markdown) {
47+
let html = markdown
48+
.replace(/^# (.*$)/gm, '<h1>$1</h1>')
49+
.replace(/^## (.*$)/gm, '<h2>$1</h2>')
50+
.replace(/^### (.*$)/gm, '<h3>$1</h3>')
51+
.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>')
52+
.replace(/\*(.*?)\*/g, '<em>$1</em>')
53+
.replace(/!\[([^\]]*)\]\(([^)]+)\s*"(.*?)"\)/g, (match, alt, src, size) => {
54+
const sizeStyle = size ? ` style="${size}"` : '';
55+
return `<img src="${src}" alt="${alt}"${sizeStyle} />`;
56+
})
57+
.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2" target="_blank">$1</a>')
58+
.replace(/```([^`]+)```/gs, '<pre><code>$1</code></pre>');
59+
60+
if (html.includes('<li>')) {
61+
html = html.replace(/(<li>.*?<\/li>)/gs, '<ul>$1</ul>');
62+
html = html.replace(/<\/ul>\s*<ul>/g, ''); // Merge consecutive <ul>
63+
}
64+
return html.replace(/\n/g, '<br>');
65+
}
66+
}
67+
export default MarkdownConverter;

0 commit comments

Comments
 (0)