Skip to content

Commit

Permalink
feat: code block line numbers
Browse files Browse the repository at this point in the history
Signed-off-by: ZTL-UwU <[email protected]>
  • Loading branch information
ZTL-UwU committed Nov 19, 2024
1 parent 2110ffb commit 404e5a4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
19 changes: 17 additions & 2 deletions components/content/ProseCode.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
<UiScrollArea>
<div
class="overflow-x-auto py-3 text-sm"
:class="[!inGroup && !filename && 'inline-copy', !language && 'pl-3', !inGroup]"
:class="[
!inGroup && !filename && 'inline-copy',
!language && 'pl-3', !inGroup,
showLineNumber && 'show-line-number',
]"
>
<slot />
</div>
Expand Down Expand Up @@ -58,8 +62,14 @@ const props = defineProps({
type: Array as () => number[],
default: () => [],
},
meta: {
type: String,
default: null,
},
});
const showLineNumber = computed(() => props.meta.includes('line-numbers'));
const iconMap = new Map(Object.entries(useConfig().value.main.codeIcon));
const icon = iconMap.get(props.filename?.toLowerCase()) || iconMap.get(props.language);
</script>
Expand All @@ -75,7 +85,7 @@ const icon = iconMap.get(props.filename?.toLowerCase()) || iconMap.get(props.lan
}
.shiki .line.highlight {
background-color: hsl(var(--muted) / 0.8);
background-color: hsl(var(--muted) / 0.9);
}
.shiki .line {
Expand All @@ -86,4 +96,9 @@ const icon = iconMap.get(props.filename?.toLowerCase()) || iconMap.get(props.lan
.inline-copy .line {
padding-right: 2.75rem;
}
.show-line-number .line::before {
content: attr(line);
@apply text-sm w-5 inline-block text-right mr-4 text-muted-foreground;
}
</style>
8 changes: 7 additions & 1 deletion components/content/ProsePre.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<template>
<ProseCode :code="code" :language="language" :filename="filename" :highlights="highlights" :meta="meta">
<ProseCode
:code="code"
:language="language"
:filename="filename"
:highlights="highlights"
:meta="meta"
>
<pre :class="$props.class" :style="style"><slot /></pre>
</ProseCode>
</template>
Expand Down
12 changes: 12 additions & 0 deletions content/2.components/1.prose.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ icon: lucide:letter-text
console.log('Code block');
};
```

```ts{1} [Line Numbers] line-numbers
// line-numbers is enabled
const line2 = 'This is line 2';
const line3 = 'This is line 3';
```
::

````md [Code]
Expand All @@ -91,6 +97,12 @@ icon: lucide:letter-text
console.log('Code block');
};
```

```ts{1} [Line Numbers] line-numbers
// line-numbers is enabled
const line2 = 'This is line 2';
const line3 = 'This is line 3';
```
````
::

Expand Down

0 comments on commit 404e5a4

Please sign in to comment.