-
-
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.
feat(transformers): two new transformers for word highlighting (#92)
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
- Loading branch information
Showing
24 changed files
with
361 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
.vp-code .tab, | ||
.vp-code .space { | ||
position: relative; | ||
} | ||
|
||
.vp-code .tab::before { | ||
content: '⇥'; | ||
position: absolute; | ||
opacity: 0.3; | ||
} | ||
|
||
.vp-code .space::before { | ||
content: '·'; | ||
position: absolute; | ||
opacity: 0.3; | ||
} | ||
|
||
.vp-code .highlighted-word { | ||
background-color: var(--vp-c-bg-soft); | ||
border: 1px solid var(--vp-c-border); | ||
padding: 1px 3px; | ||
margin: -1px -3px; | ||
border-radius: 4px; | ||
} |
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 |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
…world! | ||
|
||
```js {3-4} fileName=test | ||
```js {3-4} fileName=test /a/ | ||
console.log('it works!') | ||
|
||
const a = 1 | ||
|
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
56 changes: 56 additions & 0 deletions
56
packages/shikiji-transformers/src/shared/highlight-word.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,56 @@ | ||
import type { Element } from 'hast' | ||
import { addClassToHast } from 'shikiji/core' | ||
|
||
export function highlightWordInLine(line: Element, ignoredElement: Element | null, word: string, className: string): void { | ||
line.children = line.children.flatMap((span) => { | ||
if (span.type !== 'element' || span.tagName !== 'span' || span === ignoredElement) | ||
return span | ||
|
||
const textNode = span.children[0] | ||
|
||
if (textNode.type !== 'text') | ||
return span | ||
|
||
return replaceSpan(span, textNode.value, word, className) ?? span | ||
}) | ||
} | ||
|
||
function inheritElement(original: Element, overrides: Partial<Element>): Element { | ||
return { | ||
...original, | ||
properties: { | ||
...original.properties, | ||
}, | ||
...overrides, | ||
} | ||
} | ||
|
||
function replaceSpan(span: Element, text: string, word: string, className: string): Element[] | undefined { | ||
const index = text.indexOf(word) | ||
|
||
if (index === -1) | ||
return | ||
|
||
const createNode = (value: string) => inheritElement(span, { | ||
children: [ | ||
{ | ||
type: 'text', | ||
value, | ||
}, | ||
], | ||
}) | ||
|
||
const nodes: Element[] = [] | ||
|
||
if (index > 0) | ||
nodes.push(createNode(text.slice(0, index))) | ||
|
||
const highlightedNode = createNode(word) | ||
addClassToHast(highlightedNode, className) | ||
nodes.push(highlightedNode) | ||
|
||
if (index + word.length < text.length) | ||
nodes.push(createNode(text.slice(index + word.length))) | ||
|
||
return nodes | ||
} |
File renamed without changes.
41 changes: 41 additions & 0 deletions
41
packages/shikiji-transformers/src/transformers/notation-highlight-word.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, addClassToHast } from 'shikiji' | ||
import { createCommentNotationTransformer } from '../utils' | ||
import { highlightWordInLine } from '../shared/highlight-word' | ||
|
||
export interface TransformerNotationWordHighlightOptions { | ||
/** | ||
* Class for highlighted words | ||
*/ | ||
classActiveWord?: string | ||
/** | ||
* Class added to the root element when the code has highlighted words | ||
*/ | ||
classActivePre?: string | ||
} | ||
|
||
export function transformerNotationWordHighlight( | ||
options: TransformerNotationWordHighlightOptions = {}, | ||
): ShikijiTransformer { | ||
const { | ||
classActiveWord = 'highlighted-word', | ||
classActivePre = undefined, | ||
} = options | ||
|
||
return createCommentNotationTransformer( | ||
'shikiji-transformers:notation-highlight-word', | ||
/^\s*(?:\/\/|\/\*|<!--|#)\s+\[!code word:(\w+)(:\d+)?\]\s*(?:\*\/|-->)?/, | ||
function ([_, word, range], _line, comment, lines, index) { | ||
const lineNum = range ? Number.parseInt(range.slice(1), 10) : lines.length | ||
|
||
lines | ||
// Don't include the comment itself | ||
.slice(index + 1, index + 1 + lineNum) | ||
.forEach(line => highlightWordInLine(line, comment, word, classActiveWord)) | ||
|
||
if (classActivePre) | ||
addClassToHast(this.pre, classActivePre) | ||
return true | ||
}, | ||
true, // remove empty lines | ||
) | ||
} |
2 changes: 1 addition & 1 deletion
2
packages/shikiji-transformers/src/transformers/render-whitespace.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
46 changes: 46 additions & 0 deletions
46
packages/shikiji-transformers/src/transformers/transformer-meta-highlight-word.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,46 @@ | ||
import type { ShikijiTransformer } from 'shikiji' | ||
import { highlightWordInLine } from '../shared/highlight-word' | ||
|
||
export function parseMetaHighlightWords(meta: string): string[] { | ||
if (!meta) | ||
return [] | ||
|
||
const match = Array.from(meta.matchAll(/\/(\w+)\//g)) | ||
|
||
return match.map(v => v[1]) | ||
} | ||
|
||
export interface TransformerMetaWordHighlightOptions { | ||
/** | ||
* Class for highlighted words | ||
* | ||
* @default 'highlighted-word' | ||
*/ | ||
className?: string | ||
} | ||
|
||
/** | ||
* Allow using `/word/` in the code snippet meta to mark highlighted words. | ||
*/ | ||
export function transformerMetaWordHighlight( | ||
options: TransformerMetaWordHighlightOptions = {}, | ||
): ShikijiTransformer { | ||
const { | ||
className = 'highlighted-word', | ||
} = options | ||
|
||
return { | ||
name: 'shikiji-transformers:meta-word-highlight', | ||
line(node) { | ||
if (!this.options.meta?.__raw) | ||
return | ||
|
||
const words = parseMetaHighlightWords(this.options.meta.__raw) | ||
|
||
for (const word of words) | ||
highlightWordInLine(node, null, word, className) | ||
|
||
return node | ||
}, | ||
} | ||
} |
Oops, something went wrong.