Skip to content

Commit 3845a41

Browse files
committed
feat: add algorithm shortcode to render pseudocodes nicely
1 parent c739e5d commit 3845a41

4 files changed

Lines changed: 121 additions & 0 deletions

File tree

assets/_custom.scss

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,82 @@ figure.image-with-source {
372372
opacity: 1;
373373
}
374374

375+
// Algorithm pseudocode blocks
376+
.algorithm {
377+
margin: 1em 0;
378+
border-top: 1px solid var(--gray-500);
379+
border-bottom: 1px solid var(--gray-500);
380+
381+
.algorithm-header {
382+
position: relative;
383+
padding: $padding-4 0;
384+
border-bottom: 1px solid var(--gray-200);
385+
386+
.algorithm-title {
387+
margin: 0;
388+
}
389+
}
390+
391+
.algorithm-toggle {
392+
position: absolute;
393+
top: 0;
394+
right: 0;
395+
background: none;
396+
border: none;
397+
cursor: pointer;
398+
padding: $padding-4;
399+
color: var(--body-font-color);
400+
opacity: 0.35;
401+
transition: opacity 0.2s ease, transform 0.2s ease;
402+
line-height: 0;
403+
404+
&:hover {
405+
opacity: 0.7;
406+
}
407+
408+
svg {
409+
transition: transform 0.2s ease;
410+
}
411+
412+
&[aria-expanded="true"] svg {
413+
transform: rotate(180deg);
414+
}
415+
}
416+
417+
.algorithm-body {
418+
display: none;
419+
padding: $padding-4 0 $padding-8;
420+
font-style: normal;
421+
line-height: 1.5;
422+
423+
&--open {
424+
display: block;
425+
}
426+
427+
> :first-child {
428+
margin-top: 0;
429+
}
430+
431+
> :last-child {
432+
margin-bottom: 0;
433+
}
434+
435+
p {
436+
margin: 0;
437+
text-indent: 0;
438+
}
439+
440+
.indent {
441+
padding-left: 1.5em;
442+
}
443+
444+
mjx-container {
445+
font-size: 100% !important;
446+
}
447+
}
448+
}
449+
450+
375451
.brand-row {
376452
display: flex;
377453
align-items: center;

layouts/partials/docs/inject/body.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,14 @@
6464
btn.setAttribute("aria-expanded", !open);
6565
body.classList.toggle("details-collapse-body--open", !open);
6666
});
67+
68+
// Collapsible algorithm blocks
69+
document.addEventListener("click", event => {
70+
const btn = event.target.closest(".algorithm-toggle");
71+
if (!btn) return;
72+
const body = btn.closest(".algorithm").querySelector(".algorithm-body");
73+
const open = btn.getAttribute("aria-expanded") === "true";
74+
btn.setAttribute("aria-expanded", !open);
75+
body.classList.toggle("algorithm-body--open", !open);
76+
});
6777
</script>

layouts/shortcodes/algorithm.html

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{{/* Algorithm pseudocode shortcode.
2+
Renders a numbered algorithm block with a collapsible body.
3+
4+
Usage:
5+
6+
{{% algorithm number="1" title="(Gradient Descent)" %}}
7+
Require: $\alpha$: Step size
8+
9+
**while** not converged do
10+
11+
$\theta \leftarrow \theta - \alpha \nabla f$
12+
13+
**end while**
14+
{{% /algorithm %}}
15+
16+
Parameters:
17+
number: (optional) Algorithm number, e.g. "1" or "2.3"
18+
title: (optional) Algorithm title, e.g. "(Gradient Descent)"
19+
*/}}
20+
{{- $number := .Get "number" -}}
21+
{{- $title := .Get "title" -}}
22+
<div class="algorithm">
23+
<div class="algorithm-header">
24+
<p class="algorithm-title"><strong>Algorithm{{ with $number }} {{ . }}{{ end }}{{ with $title }} {{ . | markdownify }}{{ end }}</strong></p>
25+
<button class="algorithm-toggle" aria-expanded="false" aria-label="Toggle algorithm">
26+
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="6 9 12 15 18 9"/></svg>
27+
</button>
28+
</div>
29+
<div class="algorithm-body">
30+
{{ .Inner }}
31+
</div>
32+
</div>

layouts/shortcodes/indent.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div class="indent">
2+
{{ .Inner }}
3+
</div>

0 commit comments

Comments
 (0)