Skip to content

Commit 414afb9

Browse files
committed
added
1 parent 5303062 commit 414afb9

2 files changed

Lines changed: 79 additions & 32 deletions

File tree

docs/app.js

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,24 +114,79 @@ function isImageFile(path) {
114114
return ["png", "jpg", "jpeg", "gif", "svg", "webp"].includes(ext);
115115
}
116116

117+
function escapeHtml(text) {
118+
return text.replace(/[&<>]/g, (char) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;" }[char]));
119+
}
120+
121+
function renderPythonWithCommentBlocks(text) {
122+
const lines = text.split("\n");
123+
const delimiters = ["'''", '"""'];
124+
let inBlock = false;
125+
let blockDelim = "";
126+
127+
const wrapped = lines
128+
.map((line, index) => {
129+
let output = "";
130+
let cursor = 0;
131+
132+
while (cursor < line.length) {
133+
if (inBlock) {
134+
const endIndex = line.indexOf(blockDelim, cursor);
135+
if (endIndex === -1) {
136+
output += `<span class="hljs-comment">${escapeHtml(line.slice(cursor))}</span>`;
137+
cursor = line.length;
138+
} else {
139+
output += `<span class="hljs-comment">${escapeHtml(line.slice(cursor, endIndex + 3))}</span>`;
140+
cursor = endIndex + 3;
141+
inBlock = false;
142+
blockDelim = "";
143+
}
144+
} else {
145+
const nextSingle = line.indexOf(delimiters[0], cursor);
146+
const nextDouble = line.indexOf(delimiters[1], cursor);
147+
const candidates = [nextSingle, nextDouble].filter((value) => value !== -1);
148+
if (!candidates.length) {
149+
output += escapeHtml(line.slice(cursor));
150+
cursor = line.length;
151+
} else {
152+
const nextIndex = Math.min(...candidates);
153+
const delim = nextIndex === nextSingle ? delimiters[0] : delimiters[1];
154+
output += escapeHtml(line.slice(cursor, nextIndex));
155+
inBlock = true;
156+
blockDelim = delim;
157+
cursor = nextIndex;
158+
}
159+
}
160+
}
161+
162+
if (!output) {
163+
output = "&nbsp;";
164+
}
165+
const lineNumber = index + 1;
166+
return `<span class="code-line"><span class="line-number">${lineNumber}</span><span class="line-content">${output}</span></span>`;
167+
})
168+
.join("\n");
169+
170+
codeBlock.innerHTML = `<span class="code-lines">${wrapped}</span>`;
171+
}
172+
117173
function renderCode(text, languageClass) {
174+
if (languageClass === "python") {
175+
renderPythonWithCommentBlocks(text);
176+
return;
177+
}
178+
118179
let highlighted;
119180
try {
120181
if (window.hljs && languageClass) {
121182
highlighted = window.hljs.highlight(text, { language: languageClass }).value;
122183
} else if (window.hljs) {
123184
highlighted = window.hljs.highlightAuto(text).value;
124185
} else {
125-
highlighted = text
126-
.replace(/&/g, "&amp;")
127-
.replace(/</g, "&lt;")
128-
.replace(/>/g, "&gt;");
186+
highlighted = escapeHtml(text);
129187
}
130188
} catch (error) {
131-
highlighted = text
132-
.replace(/&/g, "&amp;")
133-
.replace(/</g, "&lt;")
134-
.replace(/>/g, "&gt;");
189+
highlighted = escapeHtml(text);
135190
}
136191

137192
const lines = highlighted.split("\n");

docs/styles.css

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -263,36 +263,30 @@ input {
263263

264264
pre {
265265
margin: 0;
266-
padding: 24px;
267-
border-radius: 16px;
268-
background: #fdf9f2;
269-
border: 1px solid rgba(31, 27, 22, 0.12);
270-
font-family: "Spline Sans Mono", monospace;
271-
font-size: 13px;
272-
line-height: 1.6;
273-
letter-spacing: 0.1px;
274-
tab-size: 2;
275-
overflow-x: auto;
276-
color: #2d241d;
277-
width: 100%;
278-
box-sizing: border-box;
279266
}
280267

281268
code {
282-
white-space: pre-wrap;
283-
word-wrap: break-word;
284-
overflow-wrap: break-word;
269+
white-space: pre;
285270
font-family: "Spline Sans Mono", monospace;
286271
display: block;
287272
width: 100%;
288273
}
289274

290275
#codeContainer {
291276
margin: 0;
292-
padding: 24px;
277+
padding: 18px 20px;
293278
border-radius: 16px;
294279
background: #fdf9f2;
295280
border: 1px solid rgba(31, 27, 22, 0.12);
281+
font-family: "Spline Sans Mono", monospace;
282+
font-size: 13.5px;
283+
line-height: 1.7;
284+
letter-spacing: 0;
285+
tab-size: 4;
286+
overflow-x: auto;
287+
color: #2d241d;
288+
width: 100%;
289+
box-sizing: border-box;
296290
}
297291

298292
.code-lines {
@@ -302,11 +296,10 @@ code {
302296

303297
.code-line {
304298
display: grid;
305-
grid-template-columns: 44px 1fr;
306-
gap: 14px;
307-
align-items: flex-start;
299+
grid-template-columns: 36px 1fr;
300+
gap: 12px;
301+
align-items: baseline;
308302
width: 100%;
309-
min-height: 1.6em;
310303
}
311304

312305
.line-number {
@@ -315,12 +308,11 @@ code {
315308
user-select: none;
316309
flex-shrink: 0;
317310
white-space: nowrap;
311+
font-variant-numeric: tabular-nums;
318312
}
319313

320314
.line-content {
321-
word-wrap: break-word;
322-
overflow-wrap: break-word;
323-
white-space: pre-wrap;
315+
white-space: pre;
324316
}
325317

326318
.image-viewer {

0 commit comments

Comments
 (0)