Skip to content

Commit 38595dd

Browse files
committed
fix(transformers): word highlight support any symbol, fix #110
1 parent 5401d5e commit 38595dd

File tree

5 files changed

+28
-4
lines changed

5 files changed

+28
-4
lines changed

packages/shikiji-transformers/src/transformers/notation-highlight-word.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@ export function transformerNotationWordHighlight(
2323

2424
return createCommentNotationTransformer(
2525
'shikiji-transformers:notation-highlight-word',
26-
/^\s*(?:\/\/|\/\*|<!--|#)\s+\[!code word:(\w+)(:\d+)?\]\s*(?:\*\/|-->)?/,
26+
// comment-start | marker | word | range | comment-end
27+
/^\s*(?:\/\/|\/\*|<!--|#)\s+\[!code word:((?:\\.|[^:\]])+)(:\d+)?\]\s*(?:\*\/|-->)?/,
2728
function ([_, word, range], _line, comment, lines, index) {
2829
const lineNum = range ? Number.parseInt(range.slice(1), 10) : lines.length
2930

31+
// escape backslashes
32+
word = word.replace(/\\(.)/g, '$1')
33+
3034
lines
3135
// Don't include the comment itself
3236
.slice(index + 1, index + 1 + lineNum)

packages/shikiji-transformers/src/transformers/transformer-meta-highlight-word.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ export function parseMetaHighlightWords(meta: string): string[] {
55
if (!meta)
66
return []
77

8-
const match = Array.from(meta.matchAll(/\/(\w+)\//g))
8+
// https://regex101.com/r/BHS5fd/1
9+
const match = Array.from(meta.matchAll(/\/((?:\\.|[^\/])+?)\//ig))
910

10-
return match.map(v => v[1])
11+
return match
12+
// Escape backslashes
13+
.map(v => v[1].replace(/\\(.)/g, '$1'))
1114
}
1215

1316
export interface TransformerMetaWordHighlightOptions {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export function transformerNotationFocus(
2+
// [!code word:options.a]
3+
options = {}, // [!code word:console.log:2]
4+
) {
5+
const options = 'options'
6+
console.log(options) // TODO: cross-token highlighting should be supported
7+
options.a = "HELLO"
8+
console.log('// [!code word:options.a]')
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<pre class="shiki github-dark" style="background-color:#24292e;color:#e1e4e8" tabindex="0"><code><span class="line"><span style="color:#F97583">export</span><span style="color:#F97583"> function</span><span style="color:#B392F0"> transformerNotationFocus</span><span style="color:#E1E4E8">(</span></span><span class="line"><span style="color:#FFAB70"> options</span><span style="color:#F97583"> =</span><span style="color:#E1E4E8"> {}, </span></span><span class="line"><span style="color:#E1E4E8">) {</span></span><span class="line"><span style="color:#F97583"> const</span><span style="color:#79B8FF"> options</span><span style="color:#F97583"> =</span><span style="color:#9ECBFF"> 'options'</span></span><span class="line"><span style="color:#E1E4E8"> console.</span><span style="color:#B392F0">log</span><span style="color:#E1E4E8">(options) </span><span style="color:#6A737D">// TODO: cross-token highlighting should be supported</span></span><span class="line"><span style="color:#E1E4E8"> </span><span style="color:#E1E4E8" class="highlighted-word">options.a</span><span style="color:#E1E4E8"> </span><span style="color:#F97583">=</span><span style="color:#9ECBFF"> "HELLO"</span></span><span class="line"><span style="color:#E1E4E8"> console.</span><span style="color:#B392F0">log</span><span style="color:#E1E4E8">(</span><span style="color:#9ECBFF">'// [!code word:</span><span style="color:#9ECBFF" class="highlighted-word">options.a</span><span style="color:#9ECBFF">]'</span><span style="color:#E1E4E8">)</span></span><span class="line"><span style="color:#E1E4E8">}</span></span><span class="line"></span></code></pre>
2+
<style>
3+
body { margin: 0; }
4+
.shiki { padding: 1em; }
5+
.line { display: block; width: 100%; height: 1.2em; }
6+
.highlighted-word { background-color: #8885; }
7+
</style>

packages/shikiji-transformers/test/meta-word-highlight.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ import { parseMetaHighlightWords } from '../src/transformers/transformer-meta-hi
44
it('parseHighlightWords', () => {
55
expect(parseMetaHighlightWords('')).toEqual([])
66
expect(parseMetaHighlightWords('/hello/')).toEqual(['hello'])
7-
expect(parseMetaHighlightWords('// /f// /hello/')).toEqual(['f', 'hello'])
7+
expect(parseMetaHighlightWords('/ /f /hello/')).toEqual([' ', 'hello'])
8+
expect(parseMetaHighlightWords('/foo bar/ /foo.bar\\/foo/')).toEqual(['foo bar', 'foo.bar/foo'])
89
})

0 commit comments

Comments
 (0)