Skip to content

Commit 78824be

Browse files
committed
feat: ECodeBlock add option to show line numbers
1 parent 82c7f21 commit 78824be

File tree

3 files changed

+38
-16
lines changed

3 files changed

+38
-16
lines changed

bin/vue-email.mjs

100644100755
File mode 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" />
26+
<ECodeBlock style="padding: 20px;" :code="code" lang="typescript" theme="dracula" show-line-numbers />
2727
</Suspense>
2828
<ECodeInline>@vue-email/nuxt</ECodeInline>
2929
<iframe :srcdoc="email" />

src/components/ECodeBlock.ts

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,49 @@ export default defineComponent({
2222
type: String,
2323
default: '',
2424
},
25+
showLineNumbers: {
26+
type: Boolean,
27+
default: false,
28+
},
29+
lineNumberColor: {
30+
type: String,
31+
default: '#636E7B',
32+
},
2533
},
26-
async setup({ code, lang, theme }) {
34+
async setup({ code, lang, theme, showLineNumbers, lineNumberColor }) {
2735
const shiki = await getHighlighter({
2836
langs: [lang],
2937
themes: [theme],
3038
})
3139

3240
const themeColorBg = shiki.getTheme(theme).bg
33-
const htmlCode = shiki.codeToThemedTokens(code, {
41+
const html = shiki.codeToHtml(code, {
3442
lang,
3543
theme,
44+
transformers: [
45+
{
46+
line(node, line) {
47+
node.properties.style = 'display: table-row; line-height: 1.5; height: 1.5em;'
48+
49+
if (showLineNumbers) {
50+
node.children.unshift({
51+
type: 'element',
52+
tagName: 'span',
53+
properties: {
54+
className: ['line-number'],
55+
style: `padding-right: 15px; color: ${lineNumberColor}`,
56+
},
57+
children: [
58+
{
59+
type: 'text',
60+
value: `${line}`,
61+
},
62+
],
63+
})
64+
}
65+
},
66+
},
67+
],
3668
})
3769

3870
return () =>
@@ -46,19 +78,9 @@ export default defineComponent({
4678
},
4779
tabindex: 0,
4880
}, [
49-
h('code', null, [
50-
...htmlCode.map((line) => {
51-
return h('span', { class: ['line'], style: {
52-
display: 'table-row',
53-
lineHeight: '1.5',
54-
height: '1.5em',
55-
} }, [
56-
...line.map((token) => {
57-
return h('span', { style: { color: token.color } }, token.content)
58-
}),
59-
])
60-
}),
61-
]),
81+
h('span', {
82+
innerHTML: html,
83+
}),
6284
])
6385
},
6486
})

0 commit comments

Comments
 (0)