Skip to content

v0.6.2 #219

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 12 commits into from
Jul 5, 2022
1 change: 1 addition & 0 deletions packages/mdx/dev/content/assets/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("hello foo")
1 change: 1 addition & 0 deletions packages/mdx/dev/content/assets/foo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("Hello foo")
34 changes: 34 additions & 0 deletions packages/mdx/dev/content/external.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
```js foo.js
// from ./assets/foo.js
```

```py foo.py
# from ./assets/foo.py
```

```py foo.py
print("not external")
# from ./assets/foo.py
```

<CH.Code>

```js foo.js
// from ./assets/foo.js
```

```py foo.py
# from ./assets/foo.py
```

```html index.html
<h1>Hello</h1>
```

---

```py another.py
# from ./assets/foo.py
```

</CH.Code>
85 changes: 77 additions & 8 deletions packages/mdx/dev/content/test.mdx
Original file line number Diff line number Diff line change
@@ -1,10 +1,79 @@
### with width

<CH.Code debug >

```js
foo
// link(1:2) https://codehike.org
const hi = 'hi'
const hi = 'hi'
const hi = 'hi'
// link[2:18] https://codehike.org
hello world
hello
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
```

</CH.Code>

<CH.Code>

```js
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
```

</CH.Code>

### with width

<CH.Code debug style={{width: 400}}>

```js
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
```

</CH.Code>

<CH.Code style={{width: 400}}>

```js
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
```

</CH.Code>

### with zoom

<CH.Code debug maxZoom={0.7} minZoom={0.7}>

```js
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
```

</CH.Code>

<CH.Code maxZoom={0.7} minZoom={0.7}>

```js
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
```

</CH.Code>

### with line numbers

<CH.Code debug lineNumbers>

```js
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
```

</CH.Code>

<CH.Code lineNumbers>

```js
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
```

</CH.Code>
13 changes: 6 additions & 7 deletions packages/mdx/dev/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@ export async function getFiles() {
.filter(file => file.endsWith(".mdx"))
.map(filename => filename.slice(0, -4))
}
export async function getContent(filename: string) {
const file = await fs.promises.readFile(
`./dev/content/${filename}.mdx`,
"utf8"
)
export async function getFile(filename: string) {
const path = `./dev/content/${filename}.mdx`
const file = await fs.promises.readFile(path, "utf8")

return file
return { value: file, path }
}

export async function getCode(file: string, config = {}) {
export async function getCode(file: any, config = {}) {
let debugLink = ""

const debugCompile = withDebugger(compile, {
Expand All @@ -36,6 +34,7 @@ export async function getCode(file: string, config = {}) {
remarkCodeHike,
{
autoImport: false,
skipLanguages: ["", "mermaid"],
showCopyButton: true,
theme,
...config,
Expand Down
6 changes: 3 additions & 3 deletions packages/mdx/pages/[name].tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { runSync } from "@mdx-js/mdx"
import * as runtime from "react/jsx-runtime.js"
import { CH } from "../src/components"
import { getCode, getContent, getFiles } from "../dev/files"
import { getCode, getFile, getFiles } from "../dev/files"
import { ClickToComponent } from "click-to-react-component"
import { Layout } from "../dev/layout"

Expand All @@ -17,8 +17,8 @@ export async function getStaticProps({ params }) {
const { name = "test" } = params

const files = await getFiles()
const content = await getContent(name)
const { code, debugLink } = await getCode(content)
const file = await getFile(name)
const { code, debugLink } = await getCode(file)
return {
props: {
tests: files,
Expand Down
1 change: 1 addition & 0 deletions packages/mdx/src/mdx-client/code.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export function mergeCodeConfig<T>(
showExpandButton == null
? props.codeConfig?.showExpandButton
: showExpandButton,
debug: props.debug ?? props.codeConfig?.debug,
}
return { ...rest, codeConfig }
}
Expand Down
2 changes: 1 addition & 1 deletion packages/mdx/src/mdx-client/scrollycoding.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
display: flex;
position: relative;
margin: 1rem 0;
gap: 1rem;
}

.ch-scrollycoding-content {
box-sizing: border-box;
padding-right: 16px;
flex: 1;
}

Expand Down
7 changes: 6 additions & 1 deletion packages/mdx/src/mdx-client/scrollycoding.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ export function Scrollycoding({
codeConfig,
presetConfig,
start = 0,
className,
style,
...rest
}: {
children: React.ReactNode
editorSteps: EditorStep[]
codeConfig: EditorProps["codeConfig"]
start?: number
presetConfig?: PresetConfig
className?: string
style?: React.CSSProperties
}) {
const stepsChildren = React.Children.toArray(children)

Expand Down Expand Up @@ -58,7 +62,8 @@ export function Scrollycoding({
<section
className={`ch-scrollycoding ${
presetConfig ? "ch-scrollycoding-with-preview" : ""
}`}
} ${className || ""}`}
style={style}
>
<div className="ch-scrollycoding-content">
<Scroller onStepChange={onStepChange}>
Expand Down
9 changes: 8 additions & 1 deletion packages/mdx/src/mdx-client/section.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ const SectionContext = React.createContext<{

export function Section({
children,
className,
style,
...props
}: {
children: React.ReactNode
className?: string
style?: React.CSSProperties
}) {
const [state, setState] = React.useState<any>(props)

Expand All @@ -45,7 +49,10 @@ export function Section({
const { selectedId, ...rest } = state

return (
<section>
<section
className={`ch-section ${className || ""}`}
style={style}
>
<SectionContext.Provider
value={{
props: rest,
Expand Down
7 changes: 6 additions & 1 deletion packages/mdx/src/mdx-client/slideshow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ export function Slideshow({
codeConfig,
presetConfig,
code,
className,
style,
...rest
}: {
children: React.ReactNode
editorSteps: EditorStep[]
codeConfig: EditorProps["codeConfig"]
presetConfig?: PresetConfig
code?: EditorProps["codeConfig"]
className?: string
style?: React.CSSProperties
}) {
const stepsChildren = React.Children.toArray(children)

Expand All @@ -42,7 +46,8 @@ export function Slideshow({
<div
className={`ch-slideshow ${
presetConfig ? "ch-slideshow-with-preview" : ""
}`}
} ${className || ""}`}
style={style}
>
<div className="ch-slideshow-slide">
<InnerCode
Expand Down
3 changes: 1 addition & 2 deletions packages/mdx/src/mdx-client/spotlight.scss
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
.ch-spotlight {
display: flex;
gap: 0.5rem;
gap: 1.1rem;
margin: 1rem 0;
}

.ch-spotlight-tabs {
display: flex;
flex-flow: column;
margin-right: 10px;
flex: 1;
gap: 0.5rem;
align-items: stretch;
Expand Down
11 changes: 8 additions & 3 deletions packages/mdx/src/mdx-client/spotlight.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ export function Spotlight({
codeConfig,
start = 0,
presetConfig,
className,
style,
...rest
}: {
children: React.ReactNode
editorSteps: EditorStep[]
codeConfig: EditorProps["codeConfig"]
start?: number
presetConfig?: PresetConfig
className?: string
style?: React.CSSProperties
}) {
const stepsChildren = React.Children.toArray(children)

Expand All @@ -38,10 +42,11 @@ export function Spotlight({
stepsChildren[0] as React.ReactElement

return (
<div
<section
className={`ch-spotlight ${
presetConfig ? "ch-spotlight-with-preview" : ""
}`}
} ${className || ""}`}
style={style}
>
<div className="ch-spotlight-tabs">
{headerElement?.props?.children ? (
Expand Down Expand Up @@ -83,6 +88,6 @@ export function Spotlight({
/>
)}
</div>
</div>
</section>
)
}
17 changes: 11 additions & 6 deletions packages/mdx/src/mini-editor/code-browser.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CodeFile } from "./editor-shift"
import { IRawTheme } from "vscode-textmate"
import { ColorName, getColor } from "utils"
import { ColorName, getColor, getColorScheme } from "utils"
import React from "react"

export function CodeBrowser({
Expand Down Expand Up @@ -189,15 +189,20 @@ function Content({
theme,
ColorName.SelectionBackground
),
colorScheme: getColorScheme(theme),
}}
>
{file.code.lines.map((line, i) => (
<div key={i}>
{line.tokens.map((token, i) => (
<span key={i} {...token.props}>
{token.content}
</span>
))}
{line.tokens.length === 0 ? (
<br />
) : (
line.tokens.map((token, i) => (
<span key={i} {...token.props}>
{token.content}
</span>
))
)}
</div>
))}
</div>
Expand Down
Loading