-
-
Notifications
You must be signed in to change notification settings - Fork 393
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
106 additions
and
82 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
41 changes: 41 additions & 0 deletions
41
packages/shikiji-transformers/src/transformers/notation-map.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,41 @@ | ||
import type { ShikijiTransformer } from 'shikiji' | ||
import { addClassToHast } from 'shikiji' | ||
import { createCommentNotationTransformer } from '../utils' | ||
|
||
export interface TransformerNotationMapOptions { | ||
classMap?: Record<string, string | string[]> | ||
/** | ||
* Class added to the <pre> element when the current code has diff | ||
*/ | ||
classActivePre?: string | ||
} | ||
|
||
function escapeRegExp(str: string) { | ||
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') | ||
} | ||
|
||
export function transformerNotationMap( | ||
options: TransformerNotationMapOptions = {}, | ||
name = 'shikiji-transformers:notation-map', | ||
): ShikijiTransformer { | ||
const { | ||
classMap = {}, | ||
classActivePre = undefined, | ||
} = options | ||
|
||
return createCommentNotationTransformer( | ||
name, | ||
new RegExp(`\\s*(?://|/\\*|<!--|#)\\s+\\[!code (${Object.keys(classMap).map(escapeRegExp).join('|')})(:\\d+)?\\]\\s*(?:\\*/|-->)?`), | ||
function ([_, match, range = ':1'], _line, _comment, lines, index) { | ||
const lineNum = Number.parseInt(range.slice(1), 10) | ||
lines | ||
.slice(index, index + lineNum) | ||
.forEach((line) => { | ||
addClassToHast(line, classMap[match]) | ||
}) | ||
if (classActivePre) | ||
addClassToHast(this.pre, classActivePre) | ||
return true | ||
}, | ||
) | ||
} |
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
2 changes: 1 addition & 1 deletion
2
packages/shikiji-transformers/test/fixtures/error-level/a.js.output.html
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
2 changes: 1 addition & 1 deletion
2
packages/shikiji-transformers/test/fixtures/focus/mutliple-lines.js.output.html
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
2 changes: 1 addition & 1 deletion
2
packages/shikiji-transformers/test/fixtures/highlight/mutliple-lines.js.output.html
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