-
Notifications
You must be signed in to change notification settings - Fork 52
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
1 parent
0b28076
commit a0d124c
Showing
8 changed files
with
134 additions
and
102 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const escapeRegExp = (str: string): string => | ||
str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') |
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
50 changes: 50 additions & 0 deletions
50
plugins/plugin-shiki/src/node/transformers/getTransformers.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,50 @@ | ||
import { | ||
transformerNotationDiff, | ||
transformerNotationErrorLevel, | ||
transformerNotationFocus, | ||
transformerNotationHighlight, | ||
} from '@shikijs/transformers' | ||
import type { ShikiTransformer } from 'shiki' | ||
import type { ShikiPluginOptions } from '../types.js' | ||
import { | ||
addClassTransformer, | ||
cleanUpTransformer, | ||
emptyLineTransformer, | ||
removeEscapeTransformer, | ||
} from './vuepressTransformers.js' | ||
|
||
export const getTransformers = ( | ||
options: ShikiPluginOptions, | ||
): ShikiTransformer[] => { | ||
const transformers: ShikiTransformer[] = [] | ||
|
||
if (options.notationDiff) { | ||
transformers.push(transformerNotationDiff()) | ||
} | ||
|
||
if (options.notationFocus) { | ||
transformers.push( | ||
transformerNotationFocus({ | ||
classActiveLine: 'has-focus', | ||
classActivePre: 'has-focused-lines', | ||
}), | ||
) | ||
} | ||
|
||
if (options.notationHighlight) { | ||
transformers.push(transformerNotationHighlight()) | ||
} | ||
|
||
if (options.notationErrorLevel) { | ||
transformers.push(transformerNotationErrorLevel()) | ||
} | ||
|
||
transformers.push( | ||
addClassTransformer, | ||
cleanUpTransformer, | ||
removeEscapeTransformer, | ||
emptyLineTransformer, | ||
) | ||
|
||
return transformers | ||
} |
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,2 @@ | ||
export * from './getTransformers.js' | ||
export * from './vuepressTransformers.js' |
47 changes: 47 additions & 0 deletions
47
plugins/plugin-shiki/src/node/transformers/vuepressTransformers.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,47 @@ | ||
import type { ShikiTransformer } from 'shiki' | ||
|
||
const ESCAPE_RE = /\[\\!code/g | ||
|
||
export const addClassTransformer: ShikiTransformer = { | ||
name: 'vuepress:add-class', | ||
pre(node) { | ||
this.addClassToHast(node, 'vp-code') | ||
}, | ||
} | ||
|
||
export const cleanUpTransformer: ShikiTransformer = { | ||
name: 'vuepress:clean-up', | ||
pre(node) { | ||
delete node.properties.tabindex | ||
delete node.properties.style | ||
}, | ||
} | ||
|
||
export const removeEscapeTransformer: ShikiTransformer = { | ||
name: 'vuepress:remove-escape', | ||
postprocess(code) { | ||
return code.replace(ESCAPE_RE, '[!code') | ||
}, | ||
} | ||
|
||
export const emptyLineTransformer: ShikiTransformer = { | ||
name: 'vuepress:empty-line', | ||
code(hast) { | ||
hast.children.forEach((span) => { | ||
if ( | ||
span.type === 'element' && | ||
span.tagName === 'span' && | ||
Array.isArray(span.properties.class) && | ||
span.properties.class.includes('line') && | ||
span.children.length === 0 | ||
) { | ||
span.children.push({ | ||
type: 'element', | ||
tagName: 'wbr', | ||
properties: {}, | ||
children: [], | ||
}) | ||
} | ||
}) | ||
}, | ||
} |