Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(build): fence-level config for line-numbers #1733

Merged
merged 4 commits into from Dec 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 36 additions & 0 deletions docs/guide/markdown.md
Expand Up @@ -495,6 +495,38 @@ export default {

Please see [`markdown` options](../config/app-configs#markdown) for more details.

You can add `:line-numbers` / `:no-line-numbers` mark in your fenced code blocks to override the value set in config.

**Input**

````md
```ts {1}
// line-numbers is disabled by default
const line2 = 'This is line 2'
const line3 = 'This is line 3'
```

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

**Output**

```ts {1}
// line-numbers is disabled by default
const line2 = 'This is line 2'
const line3 = 'This is line 3'
```

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

## Import Code Snippets

You can import code snippets from existing files via following syntax:
Expand Down Expand Up @@ -551,6 +583,10 @@ You can also specify the language inside the braces (`{}`) like this:
<!-- with line highlighting: -->

<<< @/snippets/snippet.cs{1,2,4-6 c#}

<!-- with line numbers: -->

<<< @/snippets/snippet.cs{1,2,4-6 c#:line-numbers}
```

This is helpful if source language cannot be inferred from your file extension.
Expand Down
10 changes: 2 additions & 8 deletions src/node/markdown/markdown.ts
Expand Up @@ -76,13 +76,10 @@ export const createMarkdownRenderer = async (
.use(imagePlugin)
.use(
linkPlugin,
{
target: '_blank',
rel: 'noreferrer',
...options.externalLinks
},
{ target: '_blank', rel: 'noreferrer', ...options.externalLinks },
base
)
.use(lineNumberPlugin, options.lineNumbers)

// 3rd party plugins
if (!options.attrs?.disable) {
Expand Down Expand Up @@ -115,8 +112,5 @@ export const createMarkdownRenderer = async (
options.config(md)
}

if (options.lineNumbers) {
md.use(lineNumberPlugin)
}
return md
}
4 changes: 3 additions & 1 deletion src/node/markdown/plugins/highlight.ts
Expand Up @@ -74,10 +74,12 @@ export async function highlight(
const styleRE = /<pre[^>]*(style=".*?")/
const preRE = /^<pre(.*?)>/
const vueRE = /-vue$/
const lineNoRE = /:(no-)?line-numbers$/

return (str: string, lang: string, attrs: string) => {
const vPre = vueRE.test(lang) ? '' : 'v-pre'
lang = lang.replace(vueRE, '').toLowerCase() || defaultLang
lang =
lang.replace(lineNoRE, '').replace(vueRE, '').toLowerCase() || defaultLang

const lineOptions = attrsToLines(attrs)
const cleanup = (str: string) =>
Expand Down
13 changes: 12 additions & 1 deletion src/node/markdown/plugins/lineNumbers.ts
Expand Up @@ -3,10 +3,21 @@

import MarkdownIt from 'markdown-it'

export const lineNumberPlugin = (md: MarkdownIt) => {
export const lineNumberPlugin = (md: MarkdownIt, enable = false) => {
const fence = md.renderer.rules.fence!
md.renderer.rules.fence = (...args) => {
const rawCode = fence(...args)

const [tokens, idx] = args
const info = tokens[idx].info

if (
(!enable && !/:line-numbers($| )/.test(info)) ||
(enable && /:no-line-numbers($| )/.test(info))
) {
return rawCode
}

const code = rawCode.slice(
rawCode.indexOf('<code>'),
rawCode.indexOf('</code>')
Expand Down
1 change: 1 addition & 0 deletions src/node/markdown/plugins/preWrapper.ts
Expand Up @@ -19,6 +19,7 @@ export function extractTitle(info: string) {
const extractLang = (info: string) => {
return info
.trim()
.replace(/:(no-)?line-numbers$/, '')
.replace(/(-vue|{| ).*$/, '')
.replace(/^vue-html$/, 'template')
}