Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 16 additions & 0 deletions docs/packages/transformers.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,22 @@ console.log('Info') // [\!code info]
- Outputs: `<span class="line highlighted info">` for info
- The outer `<pre>` tag is modified: `<pre class="has-highlighted">`

You can also provide a message to be displayed:

````md
```ts
console.error('Error') // [\!code error:This is an error message]
```
````

Renders:

```ts
console.error('Error') // [!code error:This is an error message]
```

- Outputs: `<span class="line highlighted error" data-error="This is an error message">`

With some additional CSS rules, you can make it look like this:

```ts
Expand Down
39 changes: 31 additions & 8 deletions packages/transformers/src/transformers/notation-error-level.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ShikiTransformer } from '@shikijs/types'
import type { MatchAlgorithmOptions } from '../shared/notation-transformer'
import { transformerNotationMap } from './notation-map'
import { createCommentNotationTransformer } from '../shared/notation-transformer'

export interface TransformerNotationErrorLevelOptions extends MatchAlgorithmOptions {
classMap?: Record<string, string | string[]>
Expand All @@ -14,6 +14,10 @@ export interface TransformerNotationErrorLevelOptions extends MatchAlgorithmOpti
classActiveCode?: string
}

function escapeRegExp(str: string): string {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
}

/**
* Allow using `[!code error]` `[!code warning]` notation in code to mark highlighted lines.
*/
Expand All @@ -30,13 +34,32 @@ export function transformerNotationErrorLevel(
classActiveCode,
} = options

return transformerNotationMap(
{
classMap,
classActivePre,
classActiveCode,
matchAlgorithm: options.matchAlgorithm,
},
return createCommentNotationTransformer(
'@shikijs/transformers:notation-error-level',
new RegExp(`\\s*\\[!code (${Object.keys(classMap).map(escapeRegExp).join('|')})(:.*)?\\]`, 'gi'),
function ([_, match, rawRange = ''], _line, _comment, lines, index) {
const range = rawRange ? rawRange.slice(1) : ''
if (/^\d+$/.test(range)) {
const lineNum = Number.parseInt(range, 10)
for (let i = index; i < Math.min(index + lineNum, lines.length); i++) {
this.addClassToHast(lines[i], classMap[match])
}
}
else {
this.addClassToHast(lines[index], classMap[match])
if (range) {
const properties = lines[index].properties || {}
properties[`data-${match}`] = range
lines[index].properties = properties
}
}

if (classActivePre)
this.addClassToHast(this.pre, classActivePre)
if (classActiveCode)
this.addClassToHast(this.code, classActiveCode)
return true
},
options.matchAlgorithm,
)
}
4 changes: 4 additions & 0 deletions packages/transformers/test/fixtures/error-level/message.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions packages/transformers/test/fixtures/error-level/numeric.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.