Skip to content

Commit 0a40a80

Browse files
authored
Merge pull request #163 from code-hike/python-comments
Python comments support
2 parents 5f08ee0 + b4445c4 commit 0a40a80

File tree

11 files changed

+222
-65
lines changed

11 files changed

+222
-65
lines changed

examples/bundle-test/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"private": true,
44
"version": "0.0.0",
55
"scripts": {
6+
"dev": "next",
67
"build": "next build"
78
},
89
"dependencies": {

examples/mdx-bundler/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"private": true,
44
"version": "0.0.0",
55
"scripts": {
6+
"dev": "next",
67
"build": "next build"
78
},
89
"dependencies": {

examples/nextjs/next.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ const withMDX = require("@next/mdx")({
1010

1111
module.exports = withMDX({
1212
pageExtensions: ["ts", "tsx", "js", "jsx", "md", "mdx"],
13+
eslint: { ignoreDuringBuilds: true },
1314
})

examples/vite/vite.config.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@ import theme from "shiki/themes/github-dark.json"
66
export default defineConfig(async () => {
77
const mdx = await import("@mdx-js/rollup")
88
return {
9+
optimizeDeps: {
10+
include: ["react/jsx-runtime"],
11+
},
912
plugins: [
1013
react(),
1114
mdx.default({ remarkPlugins: [[remarkCodeHike, { theme }]] }),
1215
],
13-
resolve: {
14-
alias: {
15-
"react/jsx-runtime": "react/jsx-runtime.js",
16-
},
17-
},
1816
}
1917
})

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
"build:lib": "lerna run --stream --scope @*/mdx build",
1313
"test": "lerna run --stream test",
1414
"dev": "lerna run --stream --scope @*/mdx dev",
15-
"storybook": "lerna run --scope storybook start --stream",
1615
"release": "auto shipit"
1716
},
1817
"devDependencies": {

packages/mdx/dev/content/comment-annotations.mdx

+24
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,27 @@ function adipiscing(...elit) {
6868
return elit.map(ipsum => ipsum.sit)
6969
}
7070
```
71+
72+
Bash like comments
73+
74+
```python
75+
def lorem(ipsum, dolor = 1):
76+
sit = ipsum == None and 0
77+
# this isn't an annotation
78+
# 你好
79+
dolor = sit - amet(dolor)
80+
# focus
81+
return sit and consectetur(ipsum) or []
82+
```
83+
84+
```bash
85+
function lorem(ipsum, dolor = 1) {
86+
# focus(1:3)
87+
# box[3:7]
88+
local sit=0
89+
# label something something
90+
dolor=$((sit - amet(dolor)))
91+
# link[19:29] https://github.com/code-hike/codehike
92+
return $sit and consectetur(ipsum) or []
93+
}
94+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Vitest Snapshot v1
2+
3+
exports[`js | // focus 1`] = `
4+
{
5+
"data": "",
6+
"focusString": "",
7+
"key": "focus",
8+
}
9+
`;
10+
11+
exports[`js | // bg(1:3) #222 1`] = `
12+
{
13+
"data": "#222",
14+
"focusString": "(1:3)",
15+
"key": "bg",
16+
}
17+
`;
18+
19+
exports[`js | // box[6:9] 1`] = `
20+
{
21+
"data": "",
22+
"focusString": "[6:9]",
23+
"key": "box",
24+
}
25+
`;
26+
27+
exports[`js | // focus 1`] = `
28+
{
29+
"data": "",
30+
"focusString": "",
31+
"key": "focus",
32+
}
33+
`;
34+
35+
exports[`js | const x = 1 1`] = `{}`;
36+
37+
exports[`js | foo // bar 1`] = `{}`;
38+
39+
exports[`python | # focus 1`] = `
40+
{
41+
"data": "",
42+
"focusString": "",
43+
"key": "focus",
44+
}
45+
`;
46+
47+
exports[`python | # bg(1:3) #222 1`] = `
48+
{
49+
"data": "#222",
50+
"focusString": "(1:3)",
51+
"key": "bg",
52+
}
53+
`;
54+
55+
exports[`python | # box[6:9] 1`] = `
56+
{
57+
"data": "",
58+
"focusString": "[6:9]",
59+
"key": "box",
60+
}
61+
`;
62+
63+
exports[`python | x = '#333' 1`] = `{}`;
64+
65+
exports[`python | # focus 1`] = `
66+
{
67+
"data": "",
68+
"focusString": "",
69+
"key": "focus",
70+
}
71+
`;
72+
73+
exports[`python | foo # bar 1`] = `{}`;

packages/mdx/src/mdx-plugin/annotations.ts

+5-28
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { CodeAnnotation } from "../smooth-code"
33
import { wrapChildren } from "./to-estree"
44
import { annotationsMap } from "../mdx-client/annotations"
55
import { JsxNode as JsxNode, SuperNode } from "./nodes"
6+
import { getCommentData } from "./comment-data"
67

78
export function getAnnotationsFromMetastring(
89
options: Record<string, string>
@@ -24,8 +25,10 @@ export function extractAnnotationsFromCode(code: Code) {
2425
const focusList = [] as string[]
2526
while (lineNumber <= lines.length) {
2627
const line = lines[lineNumber - 1]
27-
const { key, focusString, data } = getCommentData(line)
28-
// console.log({ key, focusString, data })
28+
const { key, focusString, data } = getCommentData(
29+
line,
30+
code.lang
31+
)
2932

3033
const Component = annotationsMap[key!]
3134

@@ -50,31 +53,6 @@ export function extractAnnotationsFromCode(code: Code) {
5053
return [annotations, focusList.join(",")] as const
5154
}
5255

53-
const commentRegex = /\/\/\s+(\w+)(\S*)\s*(.*)/
54-
function getCommentData(line: Code["lines"][0]) {
55-
const comment = line.tokens.find(t =>
56-
t.content.trim().startsWith("//")
57-
)?.content
58-
59-
if (!comment) {
60-
return {}
61-
}
62-
63-
const result = commentRegex.exec(comment)
64-
65-
if (!result) {
66-
return {}
67-
}
68-
69-
const [, key, focusString, data] = result
70-
71-
return {
72-
key,
73-
focusString,
74-
data,
75-
}
76-
}
77-
7856
export function extractJSXAnnotations(
7957
node: SuperNode,
8058
index: number,
@@ -108,7 +86,6 @@ export function extractJSXAnnotations(
10886
focus,
10987
data: isEmpty(data) ? undefined : data,
11088
})
111-
// console.log(jsxAnnotation)
11289
parent.children.splice(nextIndex, 1)
11390
}
11491
return annotations
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { describe, it, expect, test } from "vitest"
2+
import { highlight } from "../highlighter"
3+
import { getCommentData } from "./comment-data"
4+
5+
test.each([
6+
"// focus",
7+
" // focus",
8+
"foo // bar",
9+
"const x = 1",
10+
" // box[6:9]",
11+
" // bg(1:3) #222",
12+
])("js | %s", async code => {
13+
expect(await getLineData("js", code)).toMatchSnapshot()
14+
})
15+
16+
test.each([
17+
"# focus",
18+
" # focus",
19+
"foo # bar",
20+
" x = '#333'",
21+
" # box[6:9]",
22+
" # bg(1:3) #222",
23+
])("python | %s", async code => {
24+
expect(
25+
await getLineData("python", code)
26+
).toMatchSnapshot()
27+
})
28+
29+
async function getLineData(lang: string, line: string) {
30+
return getCommentData(await asLine(lang, line), lang)
31+
}
32+
33+
async function asLine(lang: string, line: string) {
34+
const code = await highlight({
35+
code: line,
36+
lang,
37+
theme: {},
38+
})
39+
return code.lines[0]
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { Code } from "../utils"
2+
import { annotationsMap } from "../mdx-client/annotations"
3+
4+
const validKeys = ["focus", ...Object.keys(annotationsMap)]
5+
6+
export function getCommentData(
7+
line: Code["lines"][0],
8+
lang: string
9+
) {
10+
const result = bashLikeLangs.includes(lang)
11+
? bashLikeComment(line)
12+
: otherComment(line)
13+
14+
if (!result) {
15+
return {}
16+
}
17+
18+
const [, key, focusString, data] = result
19+
20+
if (!validKeys.includes(key)) {
21+
return {}
22+
}
23+
24+
return {
25+
key,
26+
focusString,
27+
data,
28+
}
29+
}
30+
31+
const commentRegex = /\/\/\s+(\w+)(\S*)\s*(.*)/
32+
function otherComment(line: Code["lines"][0]) {
33+
const comment = line.tokens.find(t =>
34+
t.content.trim().startsWith("//")
35+
)?.content
36+
37+
if (!comment) {
38+
return []
39+
}
40+
41+
const result = commentRegex.exec(comment)
42+
43+
if (!result) {
44+
return []
45+
}
46+
47+
return result
48+
}
49+
50+
const bashLikeLangs = [
51+
"bash",
52+
"sh",
53+
"shell",
54+
"python",
55+
"py",
56+
]
57+
const bashLikeCommentRegex = /#\s+(\w+)(\S*)\s*(.*)/
58+
function bashLikeComment(line: Code["lines"][0]) {
59+
const comment = line.tokens.find(t =>
60+
t.content.trim().startsWith("#")
61+
)?.content
62+
63+
if (!comment) {
64+
return []
65+
}
66+
67+
const result = bashLikeCommentRegex.exec(comment)
68+
69+
if (!result) {
70+
return []
71+
}
72+
73+
return result
74+
}

packages/storybook/package.json

-31
This file was deleted.

0 commit comments

Comments
 (0)