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: Line Numbers only for single code block #1363

Closed
wants to merge 8 commits into from
18 changes: 16 additions & 2 deletions docs/guide/markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,23 @@ export default { // Highlighted

## Line Numbers

You can enable line numbers for each code blocks via config:
You can enable line numbers for each code blocks via config or you can specify `{showLineNumbers}` for a single code block:

```js
**Input**

````
```js {showLineNumbers}
export default {
markdown: {
lineNumbers: true
}
}
```
````

**Output**

```js{showLineNumbers}
export default {
markdown: {
lineNumbers: true
Expand Down
5 changes: 1 addition & 4 deletions src/node/markdown/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export const createMarkdownRenderer = async (
},
base
)
.use(lineNumberPlugin, options.lineNumbers)

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

if (options.lineNumbers) {
md.use(lineNumberPlugin)
}
return md
}
2 changes: 1 addition & 1 deletion src/node/markdown/plugins/highlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const attrsToLines = (attrs: string): HtmlRendererOptions['lineOptions'] => {
result.push(
...Array.from({ length: end - start + 1 }, (_, i) => start + i)
)
} else {
} else if (!isNaN(start)) {
result.push(start)
}
})
Expand Down
14 changes: 13 additions & 1 deletion src/node/markdown/plugins/lineNumbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,22 @@

import MarkdownIt from 'markdown-it'

export const lineNumberPlugin = (md: MarkdownIt) => {
export const lineNumberPlugin = (
md: MarkdownIt,
isLineNumberOption: Boolean
) => {
const fence = md.renderer.rules.fence!
md.renderer.rules.fence = (...args) => {
const rawCode = fence(...args)
const [tokens, idx] = args
const token = tokens[idx]
const attr = token.attrs && token.attrs[0]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic won't work if markdown-it-attrs is disabled. As a fallback, you need to manually check token.info too for that option (refer highlightLines plugin for example, we had done similar stuff there). Also, have you checked if this works with line highlighting and snippets import? If yes, then consider adding some example to the docs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brc-dd On disabling markdown-it-attrs, I noticed that on the top right corner of code block where we are displaying the language for the code, the {1,4,6-8} - line highlightning syntax is also coming up. Is this intended or a bug ?
Screenshot 2022-09-24 132412

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it's a bug.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have resolved the above bug and also line numbers are now working even if markdown-it-attrs is disabled. Please, let me know if you see any issues :)

if (
(!isLineNumberOption && !attr) ||
(!isLineNumberOption && attr && !attr[0].includes('showLineNumbers'))
) {
return rawCode
}
const code = rawCode.slice(
rawCode.indexOf('<code>'),
rawCode.indexOf('</code>')
Expand Down