From d7dfb85438791b5747dbd7f4583e1b8caa4f93b2 Mon Sep 17 00:00:00 2001 From: Rodrigo Pombo Date: Sat, 18 Dec 2021 16:41:48 +0100 Subject: [PATCH] Stop assuming comment annotations --- packages/mdx/src/plugin/annotations.ts | 24 ++++++++++--------- .../content/comment-annotations.mdx | 1 + 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/packages/mdx/src/plugin/annotations.ts b/packages/mdx/src/plugin/annotations.ts index 871bdb7a..67327dd5 100644 --- a/packages/mdx/src/plugin/annotations.ts +++ b/packages/mdx/src/plugin/annotations.ts @@ -24,19 +24,24 @@ export function extractAnnotationsFromCode(code: Code) { const focusList = [] as string[] while (lineNumber <= lines.length) { const line = lines[lineNumber - 1] - const { key, focus, data } = getCommentData( - line, - lineNumber - ) + const { key, focusString, data } = getCommentData(line) const Component = annotationsMap[key!] if (Component) { + const focus = relativeToAbsolute( + focusString, + lineNumber + ) lines.splice(lineNumber - 1, 1) - annotations.push({ Component, focus: focus!, data }) + annotations.push({ Component, focus, data }) } else if (key === "focus") { + const focus = relativeToAbsolute( + focusString, + lineNumber + ) lines.splice(lineNumber - 1, 1) - focusList.push(focus!) + focusList.push(focus) } else { lineNumber++ } @@ -44,10 +49,7 @@ export function extractAnnotationsFromCode(code: Code) { return [annotations, focusList.join(",")] as const } -function getCommentData( - line: Code["lines"][0], - lineNumber: number -) { +function getCommentData(line: Code["lines"][0]) { const comment = line.tokens.find(t => t.content.startsWith("//") )?.content @@ -63,7 +65,7 @@ function getCommentData( return { key, - focus: relativeToAbsolute(focusString, lineNumber), + focusString, data, } } diff --git a/packages/playground/content/comment-annotations.mdx b/packages/playground/content/comment-annotations.mdx index 56f64b56..c7954b76 100644 --- a/packages/playground/content/comment-annotations.mdx +++ b/packages/playground/content/comment-annotations.mdx @@ -41,6 +41,7 @@ function lorem(ipsum, dolor = 1) { return sit ? consectetur(ipsum) : [] } +// this comment isn't an annotation function adipiscing(...elit) { console.log(elit) // box[19:36] aqua