From a01263fb5f1a338df13149644afb0259ca71dcac Mon Sep 17 00:00:00 2001 From: Rodrigo Pombo Date: Fri, 22 Apr 2022 12:47:52 +0000 Subject: [PATCH 1/2] Add missing scritps --- examples/bundle-test/package.json | 1 + examples/mdx-bundler/package.json | 1 + examples/vite/vite.config.js | 8 +++----- package.json | 1 - packages/storybook/package.json | 31 ------------------------------- 5 files changed, 5 insertions(+), 37 deletions(-) delete mode 100644 packages/storybook/package.json diff --git a/examples/bundle-test/package.json b/examples/bundle-test/package.json index 2d3ba98c..e9a21cc2 100644 --- a/examples/bundle-test/package.json +++ b/examples/bundle-test/package.json @@ -3,6 +3,7 @@ "private": true, "version": "0.0.0", "scripts": { + "dev": "next", "build": "next build" }, "dependencies": { diff --git a/examples/mdx-bundler/package.json b/examples/mdx-bundler/package.json index e5a1c49f..1f29df0b 100644 --- a/examples/mdx-bundler/package.json +++ b/examples/mdx-bundler/package.json @@ -3,6 +3,7 @@ "private": true, "version": "0.0.0", "scripts": { + "dev": "next", "build": "next build" }, "dependencies": { diff --git a/examples/vite/vite.config.js b/examples/vite/vite.config.js index 8219277d..1e824a35 100644 --- a/examples/vite/vite.config.js +++ b/examples/vite/vite.config.js @@ -6,14 +6,12 @@ import theme from "shiki/themes/github-dark.json" export default defineConfig(async () => { const mdx = await import("@mdx-js/rollup") return { + optimizeDeps: { + include: ["react/jsx-runtime"], + }, plugins: [ react(), mdx.default({ remarkPlugins: [[remarkCodeHike, { theme }]] }), ], - resolve: { - alias: { - "react/jsx-runtime": "react/jsx-runtime.js", - }, - }, } }) diff --git a/package.json b/package.json index acfbad8c..37f28203 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,6 @@ "build:lib": "lerna run --stream --scope @*/mdx build", "test": "lerna run --stream test", "dev": "lerna run --stream --scope @*/mdx dev", - "storybook": "lerna run --scope storybook start --stream", "release": "auto shipit" }, "devDependencies": { diff --git a/packages/storybook/package.json b/packages/storybook/package.json deleted file mode 100644 index dffa39d8..00000000 --- a/packages/storybook/package.json +++ /dev/null @@ -1,31 +0,0 @@ -{ - "name": "storybook", - "private": true, - "version": "0.3.0-next.0", - "scripts": { - "start": "start-storybook -c ./src -s ./public --quiet", - "build-storybook": "build-storybook -c ./src -s ./public -o ./dist", - "build-docs": "build-storybook -c ./src -s ./public --docs" - }, - "dependencies": { - "@code-hike/highlighter": "0.3.0-next.0", - "@code-hike/mini-browser": "0.3.0-next.0", - "@code-hike/mini-frame": "0.3.0-next.0", - "@code-hike/mini-terminal": "0.3.0-next.0", - "@code-hike/scroller": "0.3.0-next.0", - "@code-hike/smooth-column": "0.3.0-next.0", - "@code-hike/mdx": "0.3.0-next.0", - "react": "^17.0.2", - "react-dom": "^17.0.2" - }, - "devDependencies": { - "@babel/core": "^7.4.5", - "@storybook/addon-essentials": "^6.3.0", - "@storybook/addons": "^6.3.2", - "@storybook/react": "^6.3.0", - "@storybook/theming": "^6.3.2", - "babel-loader": "^8.0.6", - "chromatic": "^5.9.2", - "xdm": "^2.0.3" - } -} From b4445c425d93833e4953f904afcc7a219a9f5bb4 Mon Sep 17 00:00:00 2001 From: Rodrigo Pombo Date: Fri, 22 Apr 2022 13:58:58 +0000 Subject: [PATCH 2/2] Add support for bash-like comments --- examples/nextjs/next.config.js | 1 + .../mdx/dev/content/comment-annotations.mdx | 24 ++++++ .../__snapshots__/comment-data.test.ts.snap | 73 ++++++++++++++++++ packages/mdx/src/mdx-plugin/annotations.ts | 33 ++------- .../mdx/src/mdx-plugin/comment-data.test.ts | 40 ++++++++++ packages/mdx/src/mdx-plugin/comment-data.ts | 74 +++++++++++++++++++ 6 files changed, 217 insertions(+), 28 deletions(-) create mode 100644 packages/mdx/src/mdx-plugin/__snapshots__/comment-data.test.ts.snap create mode 100644 packages/mdx/src/mdx-plugin/comment-data.test.ts create mode 100644 packages/mdx/src/mdx-plugin/comment-data.ts diff --git a/examples/nextjs/next.config.js b/examples/nextjs/next.config.js index def0588f..75aeb311 100644 --- a/examples/nextjs/next.config.js +++ b/examples/nextjs/next.config.js @@ -10,4 +10,5 @@ const withMDX = require("@next/mdx")({ module.exports = withMDX({ pageExtensions: ["ts", "tsx", "js", "jsx", "md", "mdx"], + eslint: { ignoreDuringBuilds: true }, }) diff --git a/packages/mdx/dev/content/comment-annotations.mdx b/packages/mdx/dev/content/comment-annotations.mdx index 30d5aa99..c52b0880 100644 --- a/packages/mdx/dev/content/comment-annotations.mdx +++ b/packages/mdx/dev/content/comment-annotations.mdx @@ -68,3 +68,27 @@ function adipiscing(...elit) { return elit.map(ipsum => ipsum.sit) } ``` + +Bash like comments + +```python +def lorem(ipsum, dolor = 1): + sit = ipsum == None and 0 + # this isn't an annotation + # 你好 + dolor = sit - amet(dolor) + # focus + return sit and consectetur(ipsum) or [] +``` + +```bash +function lorem(ipsum, dolor = 1) { + # focus(1:3) + # box[3:7] + local sit=0 + # label something something + dolor=$((sit - amet(dolor))) + # link[19:29] https://github.com/code-hike/codehike + return $sit and consectetur(ipsum) or [] +} +``` diff --git a/packages/mdx/src/mdx-plugin/__snapshots__/comment-data.test.ts.snap b/packages/mdx/src/mdx-plugin/__snapshots__/comment-data.test.ts.snap new file mode 100644 index 00000000..08c9d828 --- /dev/null +++ b/packages/mdx/src/mdx-plugin/__snapshots__/comment-data.test.ts.snap @@ -0,0 +1,73 @@ +// Vitest Snapshot v1 + +exports[`js | // focus 1`] = ` +{ + "data": "", + "focusString": "", + "key": "focus", +} +`; + +exports[`js | // bg(1:3) #222 1`] = ` +{ + "data": "#222", + "focusString": "(1:3)", + "key": "bg", +} +`; + +exports[`js | // box[6:9] 1`] = ` +{ + "data": "", + "focusString": "[6:9]", + "key": "box", +} +`; + +exports[`js | // focus 1`] = ` +{ + "data": "", + "focusString": "", + "key": "focus", +} +`; + +exports[`js | const x = 1 1`] = `{}`; + +exports[`js | foo // bar 1`] = `{}`; + +exports[`python | # focus 1`] = ` +{ + "data": "", + "focusString": "", + "key": "focus", +} +`; + +exports[`python | # bg(1:3) #222 1`] = ` +{ + "data": "#222", + "focusString": "(1:3)", + "key": "bg", +} +`; + +exports[`python | # box[6:9] 1`] = ` +{ + "data": "", + "focusString": "[6:9]", + "key": "box", +} +`; + +exports[`python | x = '#333' 1`] = `{}`; + +exports[`python | # focus 1`] = ` +{ + "data": "", + "focusString": "", + "key": "focus", +} +`; + +exports[`python | foo # bar 1`] = `{}`; diff --git a/packages/mdx/src/mdx-plugin/annotations.ts b/packages/mdx/src/mdx-plugin/annotations.ts index 2d3260d5..deb38701 100644 --- a/packages/mdx/src/mdx-plugin/annotations.ts +++ b/packages/mdx/src/mdx-plugin/annotations.ts @@ -3,6 +3,7 @@ import { CodeAnnotation } from "../smooth-code" import { wrapChildren } from "./to-estree" import { annotationsMap } from "../mdx-client/annotations" import { JsxNode as JsxNode, SuperNode } from "./nodes" +import { getCommentData } from "./comment-data" export function getAnnotationsFromMetastring( options: Record @@ -24,8 +25,10 @@ export function extractAnnotationsFromCode(code: Code) { const focusList = [] as string[] while (lineNumber <= lines.length) { const line = lines[lineNumber - 1] - const { key, focusString, data } = getCommentData(line) - // console.log({ key, focusString, data }) + const { key, focusString, data } = getCommentData( + line, + code.lang + ) const Component = annotationsMap[key!] @@ -50,31 +53,6 @@ export function extractAnnotationsFromCode(code: Code) { return [annotations, focusList.join(",")] as const } -const commentRegex = /\/\/\s+(\w+)(\S*)\s*(.*)/ -function getCommentData(line: Code["lines"][0]) { - const comment = line.tokens.find(t => - t.content.trim().startsWith("//") - )?.content - - if (!comment) { - return {} - } - - const result = commentRegex.exec(comment) - - if (!result) { - return {} - } - - const [, key, focusString, data] = result - - return { - key, - focusString, - data, - } -} - export function extractJSXAnnotations( node: SuperNode, index: number, @@ -108,7 +86,6 @@ export function extractJSXAnnotations( focus, data: isEmpty(data) ? undefined : data, }) - // console.log(jsxAnnotation) parent.children.splice(nextIndex, 1) } return annotations diff --git a/packages/mdx/src/mdx-plugin/comment-data.test.ts b/packages/mdx/src/mdx-plugin/comment-data.test.ts new file mode 100644 index 00000000..74da7137 --- /dev/null +++ b/packages/mdx/src/mdx-plugin/comment-data.test.ts @@ -0,0 +1,40 @@ +import { describe, it, expect, test } from "vitest" +import { highlight } from "../highlighter" +import { getCommentData } from "./comment-data" + +test.each([ + "// focus", + " // focus", + "foo // bar", + "const x = 1", + " // box[6:9]", + " // bg(1:3) #222", +])("js | %s", async code => { + expect(await getLineData("js", code)).toMatchSnapshot() +}) + +test.each([ + "# focus", + " # focus", + "foo # bar", + " x = '#333'", + " # box[6:9]", + " # bg(1:3) #222", +])("python | %s", async code => { + expect( + await getLineData("python", code) + ).toMatchSnapshot() +}) + +async function getLineData(lang: string, line: string) { + return getCommentData(await asLine(lang, line), lang) +} + +async function asLine(lang: string, line: string) { + const code = await highlight({ + code: line, + lang, + theme: {}, + }) + return code.lines[0] +} diff --git a/packages/mdx/src/mdx-plugin/comment-data.ts b/packages/mdx/src/mdx-plugin/comment-data.ts new file mode 100644 index 00000000..260662ad --- /dev/null +++ b/packages/mdx/src/mdx-plugin/comment-data.ts @@ -0,0 +1,74 @@ +import { Code } from "../utils" +import { annotationsMap } from "../mdx-client/annotations" + +const validKeys = ["focus", ...Object.keys(annotationsMap)] + +export function getCommentData( + line: Code["lines"][0], + lang: string +) { + const result = bashLikeLangs.includes(lang) + ? bashLikeComment(line) + : otherComment(line) + + if (!result) { + return {} + } + + const [, key, focusString, data] = result + + if (!validKeys.includes(key)) { + return {} + } + + return { + key, + focusString, + data, + } +} + +const commentRegex = /\/\/\s+(\w+)(\S*)\s*(.*)/ +function otherComment(line: Code["lines"][0]) { + const comment = line.tokens.find(t => + t.content.trim().startsWith("//") + )?.content + + if (!comment) { + return [] + } + + const result = commentRegex.exec(comment) + + if (!result) { + return [] + } + + return result +} + +const bashLikeLangs = [ + "bash", + "sh", + "shell", + "python", + "py", +] +const bashLikeCommentRegex = /#\s+(\w+)(\S*)\s*(.*)/ +function bashLikeComment(line: Code["lines"][0]) { + const comment = line.tokens.find(t => + t.content.trim().startsWith("#") + )?.content + + if (!comment) { + return [] + } + + const result = bashLikeCommentRegex.exec(comment) + + if (!result) { + return [] + } + + return result +}