From d3f27aded88764d43892bb074815fd8fc089bf4c Mon Sep 17 00:00:00 2001 From: Rodrigo Pombo Date: Fri, 7 Jan 2022 22:39:29 +0100 Subject: [PATCH] Fix annotation regex --- packages/mdx/src/plugin/annotations.ts | 12 ++++++++---- packages/playground/content/comment-annotations.mdx | 3 +++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/mdx/src/plugin/annotations.ts b/packages/mdx/src/plugin/annotations.ts index 23b53fdf..62e5daf3 100644 --- a/packages/mdx/src/plugin/annotations.ts +++ b/packages/mdx/src/plugin/annotations.ts @@ -50,6 +50,7 @@ export function extractAnnotationsFromCode(code: Code) { return [annotations, focusList.join(",")] as const } +const commentRegex = /\/\/\s+(\w+)(\S*)\s*(.*)/ function getCommentData(line: Code["lines"][0]) { const comment = line.tokens.find(t => t.content.trim().startsWith("//") @@ -59,10 +60,13 @@ function getCommentData(line: Code["lines"][0]) { return {} } - const commentRegex = /\/\/\s+(\w+)(\S*)\s*(.*)/ - const [, key, focusString, data] = commentRegex.exec( - comment - ) + const result = commentRegex.exec(comment) + + if (!result) { + return {} + } + + const [, key, focusString, data] = result return { key, diff --git a/packages/playground/content/comment-annotations.mdx b/packages/playground/content/comment-annotations.mdx index c7954b76..30d5aa99 100644 --- a/packages/playground/content/comment-annotations.mdx +++ b/packages/playground/content/comment-annotations.mdx @@ -42,6 +42,9 @@ function lorem(ipsum, dolor = 1) { } // this comment isn't an annotation +//hello +// 你好 +// å function adipiscing(...elit) { console.log(elit) // box[19:36] aqua