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
19 changes: 19 additions & 0 deletions docs/packages/transformers.md
Original file line number Diff line number Diff line change
Expand Up @@ -544,3 +544,22 @@ CSS output:
--shiki-light-bg: #ffffff;
}
```

### `transformerRemoveComments`

Remove comments from the code.

```ts
import { transformerRemoveComments } from '@shikijs/transformers'

const html = await codeToHtml(code, {
lang: 'ts',
transformers: [
transformerRemoveComments(),
],
})
```

Options:

- `removeEmptyLines`: Remove lines that become empty after removing comments. Default `true`.
1 change: 1 addition & 0 deletions packages/transformers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export * from './transformers/notation-focus'
export * from './transformers/notation-highlight'
export * from './transformers/notation-highlight-word'
export * from './transformers/notation-map'
export * from './transformers/remove-comments'
export * from './transformers/remove-line-breaks'
export * from './transformers/remove-notation-escape'
export * from './transformers/render-indent-guides'
Expand Down
51 changes: 51 additions & 0 deletions packages/transformers/src/transformers/remove-comments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import type { ShikiTransformer } from '@shikijs/types'

export interface TransformerRemoveCommentsOptions {
/**
* Remove lines that become empty after removing comments.
* @default true
*/
removeEmptyLines?: boolean
}

/**
* Remove comments from the code.
*/
export function transformerRemoveComments(
options: TransformerRemoveCommentsOptions = {},
): ShikiTransformer {
const { removeEmptyLines = true } = options

return {
name: '@shikijs/transformers:remove-comments',
preprocess(_code, options) {
const opts = options as any
if (opts.includeExplanation !== true)
opts.includeExplanation = true
},
tokens(tokens) {
const result = []
for (const line of tokens) {
const hasComment = line.some(token =>
token.explanation?.some(exp => exp.scopes.some(s => s.scopeName.startsWith('comment'))),
)

const filteredLine = line.filter((token) => {
const isComment = token.explanation?.some(exp =>
exp.scopes.some(s => s.scopeName.startsWith('comment')),
)
return !isComment
})

if (removeEmptyLines && hasComment) {
const isAllWhitespace = filteredLine.every(token => !token.content.trim())
if (isAllWhitespace)
continue
}

result.push(filteredLine)
}
return result
},
}
}
16 changes: 16 additions & 0 deletions packages/transformers/test/fixtures.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
transformerNotationFocus,
transformerNotationHighlight,
transformerNotationWordHighlight,
transformerRemoveComments,
transformerRemoveLineBreak,
transformerRemoveNotationEscape,
transformerRenderWhitespace,
Expand Down Expand Up @@ -265,3 +266,18 @@ body { margin: 0; }
.line { display: block; width: 100%; height: 1.2em; }
</style>`,
)

suite(
'remove-comments',
import.meta.glob('./fixtures/remove-comments/*.*', { query: '?raw', import: 'default', eager: true }),
[
transformerRemoveComments(),
transformerRemoveLineBreak(),
],
code => `${code}
<style>
body { margin: 0; }
.shiki { padding: 1em; }
.line { display: block; width: 100%; height: 1.2em; }
</style>`,
)
6 changes: 6 additions & 0 deletions packages/transformers/test/fixtures/remove-comments/basic.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.

1 change: 1 addition & 0 deletions test/exports/@shikijs/transformers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
transformerNotationHighlight: function
transformerNotationMap: function
transformerNotationWordHighlight: function
transformerRemoveComments: function
transformerRemoveLineBreak: function
transformerRemoveNotationEscape: function
transformerRenderIndentGuides: function
Expand Down