Skip to content

Commit af06823

Browse files
committed
updated
1 parent cde67c0 commit af06823

3 files changed

Lines changed: 181 additions & 8 deletions

File tree

docs/app.js

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ const state = {
1010

1111
const treeRoot = document.getElementById("treeRoot");
1212
const codeBlock = document.getElementById("codeBlock");
13+
const codeContainer = document.getElementById("codeContainer");
14+
const markdownContent = document.getElementById("markdownContent");
15+
const imageContent = document.getElementById("imageContent");
1316
const contentTitle = document.getElementById("contentTitle");
1417
const contentFooter = document.getElementById("contentFooter");
1518
const copyLink = document.getElementById("copyLink");
@@ -79,6 +82,30 @@ function languageForPath(path) {
7982
return map[ext] || "";
8083
}
8184

85+
function isImageFile(path) {
86+
const ext = fileExtension(path);
87+
return ["png", "jpg", "jpeg", "gif", "svg", "webp"].includes(ext);
88+
}
89+
90+
function renderCode(text, languageClass) {
91+
const highlighted = window.hljs
92+
? (languageClass
93+
? window.hljs.highlight(text, { language: languageClass }).value
94+
: window.hljs.highlightAuto(text).value)
95+
: text.replace(/[&<>]/g, (char) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;" }[char]));
96+
97+
const lines = highlighted.split("\n");
98+
const wrapped = lines
99+
.map((line, index) => {
100+
const lineNumber = index + 1;
101+
const content = line.length ? line : "&nbsp;";
102+
return `<span class="code-line"><span class="line-number">${lineNumber}</span><span class="line-content">${content}</span></span>`;
103+
})
104+
.join("\n");
105+
106+
codeBlock.innerHTML = `<span class="code-lines">${wrapped}</span>`;
107+
}
108+
82109
function setActiveItem(element) {
83110
if (state.activeItem) {
84111
state.activeItem.classList.remove("active");
@@ -117,12 +144,44 @@ async function loadFile(path, meta = {}) {
117144
if (!response.ok) {
118145
throw new Error(`Failed to fetch ${path}`);
119146
}
147+
if (isImageFile(path)) {
148+
const blob = await response.blob();
149+
const imageUrl = URL.createObjectURL(blob);
150+
imageContent.innerHTML = `<img src="${imageUrl}" alt="${path}" />`;
151+
imageContent.classList.remove("hidden");
152+
markdownContent.classList.add("hidden");
153+
codeContainer.classList.add("hidden");
154+
return;
155+
}
156+
120157
const text = await response.text();
121-
codeBlock.textContent = text;
122-
if (window.hljs) {
123-
window.hljs.highlightElement(codeBlock);
158+
if (languageClass === "markdown" && window.marked) {
159+
const html = window.marked.parse(text);
160+
markdownContent.innerHTML = html;
161+
markdownContent.classList.remove("hidden");
162+
imageContent.classList.add("hidden");
163+
codeContainer.classList.add("hidden");
164+
} else if (languageClass === "json") {
165+
let formatted = text;
166+
try {
167+
formatted = JSON.stringify(JSON.parse(text), null, 2);
168+
} catch (error) {
169+
formatted = text;
170+
}
171+
markdownContent.classList.add("hidden");
172+
imageContent.classList.add("hidden");
173+
codeContainer.classList.remove("hidden");
174+
renderCode(formatted, languageClass);
175+
} else {
176+
markdownContent.classList.add("hidden");
177+
imageContent.classList.add("hidden");
178+
codeContainer.classList.remove("hidden");
179+
renderCode(text, languageClass);
124180
}
125181
} catch (error) {
182+
markdownContent.classList.add("hidden");
183+
imageContent.classList.add("hidden");
184+
codeContainer.classList.remove("hidden");
126185
codeBlock.textContent = `Unable to load ${path}. ${error.message}`;
127186
}
128187
}

docs/index.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,16 @@
5656
</div>
5757
</div>
5858
<div class="content-body">
59-
<pre><code id="codeBlock" class="language-markdown"></code></pre>
59+
<div id="markdownContent" class="markdown hidden"></div>
60+
<div id="imageContent" class="image-viewer hidden"></div>
61+
<pre id="codeContainer"><code id="codeBlock" class="language-markdown"></code></pre>
6062
</div>
6163
<div class="content-footer" id="contentFooter"></div>
6264
</main>
6365
</div>
6466

6567
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
68+
<script src="https://cdnjs.cloudflare.com/ajax/libs/marked/12.0.1/marked.min.js"></script>
6669
<script src="app.js"></script>
6770
</body>
6871
</html>

docs/styles.css

Lines changed: 115 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,13 +260,124 @@ input {
260260

261261
pre {
262262
margin: 0;
263+
padding: 24px;
264+
border-radius: 16px;
265+
background: #fdf9f2;
266+
border: 1px solid rgba(31, 27, 22, 0.12);
267+
font-family: "Spline Sans Mono", monospace;
268+
font-size: 14px;
269+
line-height: 1.85;
270+
letter-spacing: 0.1px;
271+
tab-size: 2;
272+
overflow-x: auto;
273+
}
274+
275+
code {
276+
white-space: pre;
277+
}
278+
279+
.code-lines {
280+
display: block;
281+
}
282+
283+
.code-line {
284+
display: grid;
285+
grid-template-columns: 44px 1fr;
286+
gap: 14px;
287+
align-items: start;
288+
}
289+
290+
.line-number {
291+
text-align: right;
292+
color: rgba(31, 27, 22, 0.45);
293+
user-select: none;
294+
}
295+
296+
.image-viewer {
297+
background: #fdf9f2;
298+
border: 1px solid rgba(31, 27, 22, 0.12);
299+
border-radius: 16px;
263300
padding: 20px;
301+
display: flex;
302+
align-items: center;
303+
justify-content: center;
304+
}
305+
306+
.image-viewer img {
307+
max-width: 100%;
308+
height: auto;
309+
border-radius: 12px;
310+
box-shadow: 0 12px 30px rgba(31, 27, 22, 0.18);
311+
}
312+
313+
.markdown {
314+
background: #fdf9f2;
315+
border: 1px solid rgba(31, 27, 22, 0.12);
264316
border-radius: 16px;
265-
background: #fbf8f3;
266-
border: 1px solid rgba(31, 27, 22, 0.08);
317+
padding: 28px;
318+
line-height: 1.85;
319+
font-size: 15px;
320+
color: var(--text);
321+
}
322+
323+
.markdown h1,
324+
.markdown h2,
325+
.markdown h3 {
326+
margin-top: 24px;
327+
margin-bottom: 12px;
328+
}
329+
330+
.markdown h1 {
331+
font-size: 26px;
332+
}
333+
334+
.markdown h2 {
335+
font-size: 22px;
336+
}
337+
338+
.markdown h3 {
339+
font-size: 18px;
340+
}
341+
342+
.markdown p {
343+
margin: 12px 0;
344+
color: #2d241d;
345+
}
346+
347+
.markdown ul,
348+
.markdown ol {
349+
padding-left: 22px;
350+
margin: 12px 0;
351+
}
352+
353+
.markdown li {
354+
margin: 6px 0;
355+
}
356+
357+
.markdown code {
267358
font-family: "Spline Sans Mono", monospace;
268-
font-size: 13px;
269-
line-height: 1.7;
359+
background: rgba(31, 27, 22, 0.08);
360+
padding: 2px 6px;
361+
border-radius: 6px;
362+
font-size: 0.9em;
363+
}
364+
365+
.markdown pre {
366+
margin: 16px 0;
367+
padding: 18px;
368+
background: #f7f2ea;
369+
border-radius: 12px;
370+
overflow-x: auto;
371+
}
372+
373+
.markdown a {
374+
color: var(--accent-dark);
375+
text-decoration: none;
376+
border-bottom: 1px solid rgba(143, 61, 30, 0.3);
377+
}
378+
379+
.markdown a:hover {
380+
border-bottom-color: rgba(143, 61, 30, 0.8);
270381
}
271382

272383
.content-footer {

0 commit comments

Comments
 (0)