Skip to content

Commit 60fd445

Browse files
feat(details): add line numbers and language tag to code blocks
Almost pure CSS baby
1 parent 5d50d91 commit 60fd445

File tree

1 file changed

+49
-2
lines changed

1 file changed

+49
-2
lines changed

src/routes/[pid=pid]/[org]/[repo]/[id=number]/PageRenderer.svelte

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,16 @@
5656
rehypePlugin: [
5757
rehypeShikiFromHighlighter,
5858
highlighter,
59-
{ themes: { light: "github-light-default", dark: "github-dark-default" } }
59+
{
60+
themes: { light: "github-light-default", dark: "github-dark-default" },
61+
transformers: [
62+
{
63+
pre(node) {
64+
node.properties["data-language"] = this.options.lang;
65+
}
66+
}
67+
]
68+
} satisfies Parameters<typeof rehypeShikiFromHighlighter>[1]
6069
]
6170
};
6271
@@ -573,9 +582,47 @@
573582
:global(html.dark .shiki span) {
574583
color: var(--shiki-dark) !important;
575584
background-color: var(--shiki-dark-bg) !important;
576-
/* Optional, if you also want font styles */
577585
font-style: var(--shiki-dark-font-style) !important;
578586
font-weight: var(--shiki-dark-font-weight) !important;
579587
text-decoration: var(--shiki-dark-text-decoration) !important;
580588
}
589+
590+
:global {
591+
.shiki {
592+
/* Show line numbers */
593+
/* Credit to https://github.com/shikijs/shiki/issues/3#issuecomment-830564854 */
594+
code {
595+
counter-reset: step;
596+
counter-increment: step 0;
597+
598+
.line::before {
599+
content: counter(step);
600+
counter-increment: step;
601+
width: 1rem;
602+
margin-right: 1.5rem;
603+
display: inline-block;
604+
text-align: right;
605+
color: color-mix(in oklab, var(--color-muted-foreground) 70%, transparent);
606+
}
607+
}
608+
609+
/* Add language tag to code blocks */
610+
&:is(pre[data-language]) {
611+
position: relative;
612+
border-radius: var(--radius-xl);
613+
border: 1px var(--tw-border-style) var(--color-border);
614+
615+
&::after {
616+
content: attr(data-language);
617+
position: absolute;
618+
inset: 0.55rem 0.55rem auto auto;
619+
padding: 0.25rem 0.5rem;
620+
background-color: var(--color-background);
621+
font-size: var(--text-xs);
622+
border-radius: var(--radius-lg);
623+
border: 1px var(--tw-border-style) var(--color-border);
624+
}
625+
}
626+
}
627+
}
581628
</style>

0 commit comments

Comments
 (0)