Skip to content

Commit

Permalink
Merge pull request #97 from code-hike/comments-that-arent-annotations
Browse files Browse the repository at this point in the history
Stop assuming comment annotations
  • Loading branch information
pomber authored Dec 18, 2021
2 parents 5804e66 + d7dfb85 commit 4164562
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
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

0 comments on commit 4164562

Please sign in to comment.