Skip to content

Commit 8f3fd2e

Browse files
committed
feat: ECodeBlock line highlighting
1 parent 38eb579 commit 8f3fd2e

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

playground/src/App.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const tokens = await codeToThemedTokens('<div class="foo">bar</div>', {
2323

2424
<template>
2525
<Suspense>
26-
<ECodeBlock style="padding: 20px;" :code="code" lang="typescript" theme="dracula" show-line-numbers />
26+
<ECodeBlock style="padding: 20px;" :code="code" lang="typescript" theme="dracula" show-line-numbers :highlighted-lines="[4, 5]" />
2727
</Suspense>
2828
<ECodeInline>@vue-email/nuxt</ECodeInline>
2929
<iframe :srcdoc="email" />

src/components/ECodeBlock.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,16 @@ export default defineComponent({
3030
type: String,
3131
default: '#636E7B',
3232
},
33+
lineHighlightingColor: {
34+
type: String,
35+
default: 'rgba(101, 117, 133, .16)',
36+
},
37+
highlightedLines: {
38+
type: Array as PropType<number[]>,
39+
default: () => [],
40+
},
3341
},
34-
async setup({ code, lang, theme, showLineNumbers, lineNumberColor }) {
42+
async setup({ code, lang, theme, showLineNumbers, lineNumberColor, highlightedLines, lineHighlightingColor }) {
3543
const shiki = await getHighlighter({
3644
langs: [lang],
3745
themes: [theme],
@@ -42,6 +50,11 @@ export default defineComponent({
4250
lang,
4351
theme,
4452
transformers: [
53+
{
54+
code(node) {
55+
node.properties.style = 'display: table; width: 100%;'
56+
},
57+
},
4558
{
4659
line(node, line) {
4760
node.properties.style = 'display: table-row; line-height: 1.5; height: 1.5em;'
@@ -52,7 +65,7 @@ export default defineComponent({
5265
tagName: 'span',
5366
properties: {
5467
className: ['line-number'],
55-
style: `padding-right: 15px; color: ${lineNumberColor}`,
68+
style: `padding-left: 5px; padding-right: 15px; color: ${lineNumberColor}; user-select: none;`,
5669
},
5770
children: [
5871
{
@@ -62,6 +75,9 @@ export default defineComponent({
6275
],
6376
})
6477
}
78+
79+
if (highlightedLines.includes(line))
80+
node.properties.style += `background-color: ${lineHighlightingColor};`
6581
},
6682
},
6783
],

0 commit comments

Comments
 (0)