This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(transformers): introduce
transformerMetaHighlight
transformer
- Loading branch information
Showing
14 changed files
with
181 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
packages/shikiji-transformers/src/transformers/transformer-meta-highlight.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { type ShikijiTransformer, addClassToHast } from 'shikiji' | ||
|
||
export function parseMetaHighlightString(meta: string) { | ||
if (!meta) | ||
return null | ||
const match = meta.match(/{([\d,-]+)}/) | ||
if (!match) | ||
return null | ||
const lines = match[1].split(',') | ||
.flatMap((v) => { | ||
const num = v.split('-').map(v => Number.parseInt(v, 10)) | ||
if (num.length === 1) | ||
return [num[0]] | ||
else | ||
return Array.from({ length: num[1] - num[0] + 1 }, (_, i) => i + num[0]) | ||
}) | ||
return lines | ||
} | ||
|
||
export interface TransformerMetaHighlightOptions { | ||
/** | ||
* Class for highlighted lines | ||
* | ||
* @default 'highlighted' | ||
*/ | ||
className?: string | ||
} | ||
|
||
const symbol = Symbol('highlighted-lines') | ||
|
||
/** | ||
* Allow using `{1,3-5}` in the code snippet meta to mark highlighted lines. | ||
*/ | ||
export function transformerMetaHighlight( | ||
options: TransformerMetaHighlightOptions = {}, | ||
): ShikijiTransformer { | ||
const { | ||
className = 'highlighted', | ||
} = options | ||
|
||
return { | ||
name: 'shikiji-transformers:meta-highlight', | ||
line(node, line) { | ||
if (!this.options.meta?.__raw) | ||
return | ||
;(this.meta as any)[symbol] ||= parseMetaHighlightString(this.options.meta.__raw) | ||
const lines: number[] = (this.meta as any)[symbol] || [] | ||
if (lines.includes(line)) | ||
addClassToHast(node, className) | ||
return node | ||
}, | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/shikiji-transformers/test/meta-line-highlight.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { expect, it } from 'vitest' | ||
import { parseMetaHighlightString } from '../src/transformers/transformer-meta-highlight' | ||
|
||
it('parseHighlightLines', () => { | ||
expect(parseMetaHighlightString('')).toBe(null) | ||
expect(parseMetaHighlightString('{1}')).toEqual([1]) | ||
expect(parseMetaHighlightString('{1,2}')).toEqual([1, 2]) | ||
expect(parseMetaHighlightString('{1,2-4,5}')).toEqual([1, 2, 3, 4, 5]) | ||
expect(parseMetaHighlightString('{1-1}')).toEqual([1]) | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.