Skip to content

Python comments support #163

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 2 commits into from
Apr 22, 2022
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
1 change: 1 addition & 0 deletions examples/bundle-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"private": true,
"version": "0.0.0",
"scripts": {
"dev": "next",
"build": "next build"
},
"dependencies": {
Expand Down
1 change: 1 addition & 0 deletions examples/mdx-bundler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"private": true,
"version": "0.0.0",
"scripts": {
"dev": "next",
"build": "next build"
},
"dependencies": {
Expand Down
1 change: 1 addition & 0 deletions examples/nextjs/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ const withMDX = require("@next/mdx")({

module.exports = withMDX({
pageExtensions: ["ts", "tsx", "js", "jsx", "md", "mdx"],
eslint: { ignoreDuringBuilds: true },
})
8 changes: 3 additions & 5 deletions examples/vite/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
}
})
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
24 changes: 24 additions & 0 deletions packages/mdx/dev/content/comment-annotations.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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 []
}
```
Original file line number Diff line number Diff line change
@@ -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`] = `{}`;
33 changes: 5 additions & 28 deletions packages/mdx/src/mdx-plugin/annotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>
Expand All @@ -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!]

Expand All @@ -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,
Expand Down Expand Up @@ -108,7 +86,6 @@ export function extractJSXAnnotations(
focus,
data: isEmpty(data) ? undefined : data,
})
// console.log(jsxAnnotation)
parent.children.splice(nextIndex, 1)
}
return annotations
Expand Down
40 changes: 40 additions & 0 deletions packages/mdx/src/mdx-plugin/comment-data.test.ts
Original file line number Diff line number Diff line change
@@ -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]
}
74 changes: 74 additions & 0 deletions packages/mdx/src/mdx-plugin/comment-data.ts
Original file line number Diff line number Diff line change
@@ -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
}
31 changes: 0 additions & 31 deletions packages/storybook/package.json

This file was deleted.