Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions packages/mdx/src/plugin/annotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,32 @@ 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++
}
}
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
Expand All @@ -63,7 +65,7 @@ function getCommentData(

return {
key,
focus: relativeToAbsolute(focusString, lineNumber),
focusString,
data,
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/playground/content/comment-annotations.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down