From 835aafe177d292a08297a7da67e8e9a52739870a Mon Sep 17 00:00:00 2001 From: Rodrigo Pombo Date: Thu, 8 May 2025 13:46:41 +0200 Subject: [PATCH 1/3] Enhance annotation extraction with support for nested brackets and parentheses --- .../src/code/extract-annotations.test.ts | 60 +++++++++++++++++++ .../codehike/src/code/extract-annotations.tsx | 13 +++- 2 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 packages/codehike/src/code/extract-annotations.test.ts diff --git a/packages/codehike/src/code/extract-annotations.test.ts b/packages/codehike/src/code/extract-annotations.test.ts new file mode 100644 index 00000000..77a8bd1a --- /dev/null +++ b/packages/codehike/src/code/extract-annotations.test.ts @@ -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") +}) diff --git a/packages/codehike/src/code/extract-annotations.tsx b/packages/codehike/src/code/extract-annotations.tsx index 8bfbfb85..d5ff7929 100644 --- a/packages/codehike/src/code/extract-annotations.tsx +++ b/packages/codehike/src/code/extract-annotations.tsx @@ -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 } From d75d16f52618a8f863127a18f74487b8282bd7ea Mon Sep 17 00:00:00 2001 From: Rodrigo Pombo Date: Thu, 8 May 2025 13:47:49 +0200 Subject: [PATCH 2/3] Add changeset --- .changeset/loud-moons-grow.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/loud-moons-grow.md diff --git a/.changeset/loud-moons-grow.md b/.changeset/loud-moons-grow.md new file mode 100644 index 00000000..ada9d2f9 --- /dev/null +++ b/.changeset/loud-moons-grow.md @@ -0,0 +1,5 @@ +--- +"codehike": patch +--- + +Better regex parsing in annotation range From 944f92a9a8ddffd422bee76aa9f34faf94b8accb Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Thu, 8 May 2025 11:53:14 +0000 Subject: [PATCH 3/3] codehike@1.0.7 --- .changeset/loud-moons-grow.md | 5 ----- packages/codehike/CHANGELOG.md | 6 ++++++ packages/codehike/package.json | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) delete mode 100644 .changeset/loud-moons-grow.md diff --git a/.changeset/loud-moons-grow.md b/.changeset/loud-moons-grow.md deleted file mode 100644 index ada9d2f9..00000000 --- a/.changeset/loud-moons-grow.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"codehike": patch ---- - -Better regex parsing in annotation range diff --git a/packages/codehike/CHANGELOG.md b/packages/codehike/CHANGELOG.md index 3b52c94e..ca888f4f 100644 --- a/packages/codehike/CHANGELOG.md +++ b/packages/codehike/CHANGELOG.md @@ -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 diff --git a/packages/codehike/package.json b/packages/codehike/package.json index 9fe5a12d..8897f0d4 100644 --- a/packages/codehike/package.json +++ b/packages/codehike/package.json @@ -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",