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
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: language in code block when markdown-it-attrs is disabled and li…
…ne numbers are compatible even when markdown-it-attrs is disabled. Also, added example of line numbers code block with line highlightning

Signed-off-by: GurkiranSingh <gurkiransinghk@gmail.com>
g4rry420 committed Sep 24, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit ce5cbf965b77821bc3dd16ab16ee6abb4861d828
39 changes: 38 additions & 1 deletion docs/guide/markdown.md
Original file line number Diff line number Diff line change
@@ -358,7 +358,7 @@ You can enable line numbers for each code blocks via config or you can specify `
**Input**

````
```js {showLineNumbers}
```js{showLineNumbers}
export default {
markdown: {
lineNumbers: true
@@ -377,6 +377,43 @@ export default {
}
```


You can also highlight lines along with specifying line numbers.

**Input**

````
```js{1,4,6-8,showLineNumbers}
export default { // Highlighted
data () {
return {
msg: `Highlighted!
This line isn't highlighted,
but this and the next 2 are.`,
motd: 'VitePress is awesome',
lorem: 'ipsum'
}
}
}
```
````

**Output**

```js{1,4,6-8,showLineNumbers}
export default { // Highlighted
data () {
return {
msg: `Highlighted!
This line isn't highlighted,
but this and the next 2 are.`,
motd: 'VitePress is awesome',
lorem: 'ipsum',
}
}
}
```

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

## Import Code Snippets
7 changes: 5 additions & 2 deletions src/node/markdown/plugins/highlightLines.ts
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@

import MarkdownIt from 'markdown-it'

const RE = /{([\d,-]+)}/
const RE = /{([\w,-]+)}/

export const highlightLinePlugin = (md: MarkdownIt) => {
const fence = md.renderer.rules.fence!
@@ -37,7 +37,10 @@ export const highlightLinePlugin = (md: MarkdownIt) => {
if (!lines) {
lines = attr![0]

if (!lines || !/[\d,-]+/.test(lines)) {
if (
!lines ||
(!/[\d,-]+/.test(lines) && !lines.includes('showLineNumbers'))
) {
return fence(...args)
}
}
6 changes: 4 additions & 2 deletions src/node/markdown/plugins/lineNumbers.ts
Original file line number Diff line number Diff line change
@@ -13,9 +13,11 @@ export const lineNumberPlugin = (
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 :)

const info = token.info
if (
(!isLineNumberOption && !attr) ||
(!isLineNumberOption && attr && !attr[0].includes('showLineNumbers'))
(!isLineNumberOption && !attr && !info) ||
(!isLineNumberOption && attr && !attr[0].includes('showLineNumbers')) ||
(!isLineNumberOption && info && !info.includes('showLineNumbers'))
) {
return rawCode
}
4 changes: 3 additions & 1 deletion src/node/markdown/plugins/preWrapper.ts
Original file line number Diff line number Diff line change
@@ -9,11 +9,13 @@

import MarkdownIt from 'markdown-it'

const RE = /{([\w,-]+)}/

export const preWrapperPlugin = (md: MarkdownIt) => {
const fence = md.renderer.rules.fence!
md.renderer.rules.fence = (...args) => {
const [tokens, idx] = args
const lang = tokens[idx].info.trim().replace(/-vue$/, '')
const lang = tokens[idx].info.trim().replace(/-vue$/, '').replace(RE, '')
const rawCode = fence(...args)
return `<div class="language-${lang}"><button class="copy"></button><span class="lang">${
lang === 'vue-html' ? 'template' : lang