Skip to content

✨ Update website ✨ #516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 13, 2025
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
6 changes: 6 additions & 0 deletions packages/codehike/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# codehike

## 1.0.7

### Patch Changes

- [#515](https://github.com/code-hike/codehike/pull/515) [`d75d16f`](https://github.com/code-hike/codehike/commit/d75d16f52618a8f863127a18f74487b8282bd7ea) Thanks [@pomber](https://github.com/pomber)! - Better regex parsing in annotation range

## 1.0.6

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/codehike/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "codehike",
"version": "1.0.6",
"version": "1.0.7",
"description": "Build rich content websites with Markdown and React",
"keywords": [
"react",
Expand Down
60 changes: 60 additions & 0 deletions packages/codehike/src/code/extract-annotations.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { expect, test } from "vitest"
import { splitAnnotationsAndCode } from "./extract-annotations.js"

async function t(comment: string) {
const code = `// ${comment} \nvar xyz = "https://example.com"`
const { annotations } = await splitAnnotationsAndCode(code, "javascript", "!")
return annotations[0]
}

test("extracts basic annotation name", async () => {
const annotation = await t("!foo bar")
expect(annotation.name).toEqual("foo")
})

test("extracts name with parentheses range", async () => {
const annotation = await t("!foo(1) bar")
expect(annotation.name).toEqual("foo")
})

test("extracts name with brackets range", async () => {
const annotation = await t("!foo[1] bar")
expect(annotation.name).toEqual("foo")
})

test("extracts name with simple regex", async () => {
const annotation = await t("!foo[/x/] bar")
expect(annotation.name).toEqual("foo")
})

test("extracts name with regex flags", async () => {
const annotation = await t("!foo[/x/gmi] bar")
expect(annotation.name).toEqual("foo")
})

test("extracts name with regex containing brackets", async () => {
const annotation = await t(`!foo[/xyz[a-z]*/g] bar`)
expect(annotation.name).toEqual("foo")
})

test("extracts name with regex containing parentheses", async () => {
const annotation = await t(`!foo(/(xyz[w]*)/g) bar`)
expect(annotation.name).toEqual("foo")
})

test("extracts name with regex containing nested parentheses", async () => {
const annotation = await t(`!foo(/((xyz)[w]*)/g) bar`)
expect(annotation.name).toEqual("foo")
})

test("extracts name with regex containing escaped slashes", async () => {
const annotation = await t(`!foo[/https?:\\/\\//g] bar`)
expect(annotation.name).toEqual("foo")
})

test("extracts name with complex regex pattern", async () => {
const code = `// !tooltip[/#\\[program\\]/] example \n#[program]`
const { annotations } = await splitAnnotationsAndCode(code, "rust", "!")
const annotation = annotations[0]
expect(annotation.name).toEqual("tooltip")
})
13 changes: 11 additions & 2 deletions packages/codehike/src/code/extract-annotations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,21 @@ async function extractCommentAnnotations(
annotationPrefix = "!",
) {
const extractor = (comment: string) => {
// const regex = /\s*(!?[\w-]+)?(\([^\)]*\)|\[[^\]]*\])?(.*)$/
const body = "(?:\\\\.|[^\\\\/])+"
const nestedBracketRegex = new RegExp(
`\\s*(${annotationPrefix}?[\\w-]+)?(\\[\\/${body}\\/[a-zA-Z]*\\])(.*)$`,
)
const nestedParenRegex = new RegExp(
`\\s*(${annotationPrefix}?[\\w-]+)?(\\(\\/${body}\\/[a-zA-Z]*\\))(.*)$`,
)
const regex = new RegExp(
`\\s*(${annotationPrefix}?[\\w-]+)?(\\([^\\)]*\\)|\\[[^\\]]*\\])?(.*)$`,
)

const match = comment.match(regex)
const match =
comment.match(nestedBracketRegex) ||
comment.match(nestedParenRegex) ||
comment.match(regex)
if (!match) {
return null
}
Expand Down