Skip to content

Commit ea217c7

Browse files
authored
Merge pull request #105 from code-hike/fix-annotation-regex
Fix annotation regex
2 parents 8b13e64 + d3f27ad commit ea217c7

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

packages/mdx/src/plugin/annotations.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export function extractAnnotationsFromCode(code: Code) {
5050
return [annotations, focusList.join(",")] as const
5151
}
5252

53+
const commentRegex = /\/\/\s+(\w+)(\S*)\s*(.*)/
5354
function getCommentData(line: Code["lines"][0]) {
5455
const comment = line.tokens.find(t =>
5556
t.content.trim().startsWith("//")
@@ -59,10 +60,13 @@ function getCommentData(line: Code["lines"][0]) {
5960
return {}
6061
}
6162

62-
const commentRegex = /\/\/\s+(\w+)(\S*)\s*(.*)/
63-
const [, key, focusString, data] = commentRegex.exec(
64-
comment
65-
)
63+
const result = commentRegex.exec(comment)
64+
65+
if (!result) {
66+
return {}
67+
}
68+
69+
const [, key, focusString, data] = result
6670

6771
return {
6872
key,

packages/playground/content/comment-annotations.mdx

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ function lorem(ipsum, dolor = 1) {
4242
}
4343

4444
// this comment isn't an annotation
45+
//hello
46+
// 你好
47+
// å
4548
function adipiscing(...elit) {
4649
console.log(elit)
4750
// box[19:36] aqua

0 commit comments

Comments
 (0)