Skip to content

Commit

Permalink
feat: support line numbers for code blocks (#311)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
mathieutu and antfu committed Jul 25, 2021
1 parent a1d8896 commit 037318f
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 3 deletions.
7 changes: 6 additions & 1 deletion packages/client/internals/SlideContainer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,15 @@ const style = computed(() => ({
width: `${slideWidth}px`,
transform: `translate(-50%, -50%) scale(${scale.value})`,
}))
const className = computed(() => ({
'select-none': !configs.selectable,
'slidev-code-line-numbers': configs.lineNumbers,
}))
</script>

<template>
<div id="slide-container" ref="root" :class="{ 'select-none': !configs.selectable }">
<div id="slide-container" ref="root" :class="className">
<div id="slide-content" :style="style">
<slot />
</div>
Expand Down
11 changes: 11 additions & 0 deletions packages/client/styles/code.css
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ html:not(.dark) .shiki-dark {
@apply h-full;
}

.slidev-code-line-numbers .slidev-code code {
counter-reset: step;
counter-increment: step 0;
}

.slidev-code-line-numbers .slidev-code code .line::before {
content: counter(step);
counter-increment: step;
@apply w-4 mr-6 inline-block text-right text-gray-400 dark:text-gray-600
}

// revert windicss preflight for katex
.katex,
.katex :after,
Expand Down
2 changes: 2 additions & 0 deletions packages/create-app/template/slides.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ background: https://source.unsplash.com/collection/94734566/1920x1080
class: 'text-center'
# https://sli.dev/custom/highlighters.html
highlighter: shiki
# show line numbers in code blocks
lineNumbers: false
# some information about the slides, markdown enabled
info: |
## Slidev Starter Template
Expand Down
1 change: 1 addition & 0 deletions packages/parser/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ export function resolveConfig(headmatter: any, themeMeta: SlidevThemeMeta = {})
download: false,
info: false,
highlighter: themeHightlighter || 'prism',
lineNumbers: false,
colorSchema: themeColorSchema || 'auto',
routerMode: 'history',
aspectRatio: 16 / 9,
Expand Down
9 changes: 7 additions & 2 deletions packages/slidev/node/plugins/markdown-it-shiki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,23 @@ export function resolveShikiOptions(options: ShikiOptions) {
}
}

function trimEndNewLine(code: string) {
return code.replace(/\n$/, '')
}

const MarkdownItShiki: MarkdownIt.PluginWithOptions<ShikiOptions> = (markdownit, options = {}) => {
const _highlighter: ShikiHighlighter = options!.highlighter!

const { darkModeThemes } = resolveShikiOptions(options!)

markdownit.options.highlight = (code, lang) => {
if (darkModeThemes) {
const trimmed = trimEndNewLine(code)
const dark = _highlighter
.codeToHtml(code, lang || 'text', darkModeThemes.dark)
.codeToHtml(trimmed, lang || 'text', darkModeThemes.dark)
.replace('<pre class="shiki"', '<pre class="slidev-code shiki shiki-dark"')
const light = _highlighter
.codeToHtml(code, lang || 'text', darkModeThemes.light)
.codeToHtml(trimmed, lang || 'text', darkModeThemes.light)
.replace('<pre class="shiki"', '<pre class="slidev-code shiki shiki-light"')
return escapeVueInCode(`<pre class="shiki-container">${dark}${light}</pre>`)
}
Expand Down
6 changes: 6 additions & 0 deletions packages/types/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ export interface SlidevConfig {
* @default prism
*/
highlighter: 'prism' | 'shiki'
/**
* Show line numbers in code blocks
*
* @default false
*/
lineNumbers: boolean
/**
* Force slides color schema
*
Expand Down
3 changes: 3 additions & 0 deletions test/__snapshots__/parser.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Object {
"highlighter": "prism",
"info": false,
"layout": "cover",
"lineNumbers": false,
"monaco": "dev",
"remoteAssets": true,
"routerMode": "history",
Expand Down Expand Up @@ -264,6 +265,7 @@ Object {
},
"highlighter": "prism",
"info": false,
"lineNumbers": false,
"monaco": "dev",
"remoteAssets": true,
"routerMode": "history",
Expand Down Expand Up @@ -418,6 +420,7 @@ Object {
},
"highlighter": "prism",
"info": false,
"lineNumbers": false,
"monaco": "dev",
"remoteAssets": true,
"routerMode": "history",
Expand Down

0 comments on commit 037318f

Please sign in to comment.