Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Empty codeblocks #192

Merged
merged 4 commits into from
Jun 8, 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
9 changes: 9 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# This configuration file was automatically generated by Gitpod.
# Please adjust to your needs (see https://www.gitpod.io/docs/config-gitpod-file)
# and commit this file to your remote git repository to share the goodness with others.

tasks:
- init: yarn install && yarn run build
command: yarn run dev


6 changes: 3 additions & 3 deletions examples/contentlayer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"dependencies": {
"@code-hike/mdx": "^0.5.2",
"@types/node": "^17.0.30",
"contentlayer": "latest",
"next": "^12.1.0",
"next-contentlayer": "latest",
"contentlayer": "0.2.5",
"next": "^12.1.5",
"next-contentlayer": "0.2.5",
"react": "^17.0.2",
"react-dom": "^17.0.2"
},
Expand Down
107 changes: 107 additions & 0 deletions packages/mdx/dev/content/empty-codeblocks.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Spotlight

<CH.Spotlight>

```text
123456789
123456789
123456789
123456789
123456789
123456789
123456789
```

---

```text focus=1:5

```

focus=1:5

---

```text focus=1,2,3

```

focus=1,2,3

---

```text focus=2[1:6]

```

focus=2[1:6]

</CH.Spotlight>

# Scrolly

<CH.Scrollycoding>

```text
123456789
123456789
123456789
123456789
123456789
123456789
123456789
```

focus=1:5

---

```text focus=1,2,3

```

focus=1,2,3

---

```text focus=2[1:6]

```

focus=2[1:6]

</CH.Scrollycoding>

# Slides

<CH.Slideshow>

```text
123456789
123456789
123456789
123456789
123456789
123456789
123456789
```

focus=1:5

---

```text focus=1,2,3

```

focus=1,2,3

---

```text focus=2[1:6]

```

focus=2[1:6]

</CH.Slideshow>
2 changes: 1 addition & 1 deletion packages/mdx/dev/content/section.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Duis aute irure dolor in reprehenderit in voluptate velit esse cillum [dolore](f

Lorem dolor sit amet, [javascript](focus://index.js#2:3) adipiscing elit, sed do eiusmod [styles](focus://styles.css#2:3) incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

<CH.Code>
<CH.Code lineNumbers={true}>

```js index.js
function lorem(ipsum, dolor) {
Expand Down
125 changes: 117 additions & 8 deletions packages/mdx/dev/content/test.mdx
Original file line number Diff line number Diff line change
@@ -1,15 +1,124 @@
Lorem ipsum dolor sit amet. _`console.log("foo bar")`_.
Annotations allow you to change how the code is displayed and also to add interactivity.

Lorem ipsum dolor sit amet. `console.log("foo bar")`.
<CH.Spotlight lineNumbers={true}>

_`console.log("foo bar")`_
````mdx mark=1[7:13],5[1:7]
```py focus=2
print(1)
print(2)
print(3)
# focus
print(4)
print(5)
```
````

_`console.log("foo bar")`_
There are two ways to add annotations:

X <CH.InlineCode>console.log("foo bar")</CH.InlineCode>
---

Lorem ipsum `var x = 1`
```mdx mark=1[7:13],5[1:7] focus=1

_Lorem ipsum `var x = 1`_
```

**`console.log("foo bar")`**
- using the codeblock metastring

---

```mdx mark=1[7:13],5[1:7] focus=5

```

- using comments inside the code

</CH.Spotlight>

_`focus`_ is only one of the possible annotations. The full list is:

- _`focus`_: keep the targeted code bright while dimming the rest
- _`mark`_: add a background color to the targeted tokens
- _`link`_: add links inside the code

First let's see the syntax to target different parts of the code.

## Targeting lines and columns

<CH.Spotlight maxZoom={1.5} lineNumbers={true} >

<CH.Code lineNumbers={true}>

```text
123456789
123456789
123456789
123456789
123456789
123456789
123456789
```

</CH.Code>

---

```text focus=2
123456789
123456789
123456789
123456789
123456789
123456789
123456789
```

To select a specific line, use the line number:
_`focus=2`_

---

```text focus=3:5
123456789
123456789
123456789
123456789
123456789
123456789
123456789
```

To select a range of lines use a colon:
_`focus=3:5`_

---

```text focus=5[3:6]
123456789
123456789
123456789
123456789
123456789
123456789
123456789
```

Select a range of column from a line using brackets:
_`focus=5[3:6]`_

---

```text focus=1,3:5,7[1:4,7:9]
123456789
123456789
123456789
123456789
123456789
123456789
123456789
```

Combine selectors using a comma separated list:
_`focus=1,3:5,7[1:4,7:9]`_

</CH.Spotlight>

## Comment syntax
39 changes: 28 additions & 11 deletions packages/mdx/src/mdx-client/code.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react"
import { CodeSpring } from "../smooth-code"
import { CodeConfig, CodeSpring } from "../smooth-code"
import {
EditorSpring,
EditorProps,
Expand All @@ -20,23 +20,29 @@ export function Code(
return <InnerCode {...step} onTabClick={onTabClick} />
}

export function InnerCode({
onTabClick,
...props
}: EditorProps & {
onTabClick?: (filename: string) => void
} & Partial<CodeHikeConfig>) {
// build the CodeConfig from props and props.codeConfig
export function mergeCodeConfig<T>(
props: Partial<CodeConfig> & {
codeConfig: Partial<CodeConfig>
} & T
) {
const {
lineNumbers,
showCopyButton,
showExpandButton,
className,
style,
...editorProps
minZoom,
maxZoom,
...rest
} = props

const codeConfig = {
...props.codeConfig,
maxZoom:
maxZoom == null ? props.codeConfig?.maxZoom : maxZoom,
minZoom:
minZoom == null ? props.codeConfig?.minZoom : minZoom,
horizontalCenter:
props.codeConfig?.horizontalCenter ??
props.horizontalCenter,
lineNumbers:
lineNumbers == null
? props.codeConfig?.lineNumbers
Expand All @@ -50,6 +56,17 @@ export function InnerCode({
? props.codeConfig?.showExpandButton
: showExpandButton,
}
return { ...rest, codeConfig }
}

export function InnerCode({
onTabClick,
...props
}: EditorProps & {
onTabClick?: (filename: string) => void
} & Partial<CodeHikeConfig>) {
const { className, style, codeConfig, ...editorProps } =
mergeCodeConfig(props)

if (
!props.southPanel &&
Expand Down
4 changes: 3 additions & 1 deletion packages/mdx/src/mdx-client/scrollycoding.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export function Scrollycoding({
codeConfig,
presetConfig,
start = 0,
...rest
}: {
children: React.ReactNode
editorSteps: EditorStep[]
Expand Down Expand Up @@ -88,10 +89,11 @@ export function Scrollycoding({
</div>
<div className="ch-scrollycoding-sticker">
<InnerCode
showExpandButton={true}
{...rest}
{...(tab as any)}
codeConfig={codeConfig}
onTabClick={onTabClick}
showExpandButton={true}
/>
{presetConfig && (
<Preview
Expand Down
10 changes: 8 additions & 2 deletions packages/mdx/src/mdx-client/section.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,21 @@ export function Section({
)
}

export function SectionCode() {
export function SectionCode(innerProps) {
const { props, setFocus } =
React.useContext(SectionContext)

const onTabClick = (filename: string) => {
setFocus({ fileName: filename, focus: null, id: "" })
}

return <InnerCode {...props} onTabClick={onTabClick} />
return (
<InnerCode
{...innerProps}
{...props}
onTabClick={onTabClick}
/>
)
}

// ---
Expand Down
2 changes: 2 additions & 0 deletions packages/mdx/src/mdx-client/slideshow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export function Slideshow({
codeConfig,
presetConfig,
code,
...rest
}: {
children: React.ReactNode
editorSteps: EditorStep[]
Expand Down Expand Up @@ -45,6 +46,7 @@ export function Slideshow({
>
<div className="ch-slideshow-slide">
<InnerCode
{...rest}
{...(tab as any)}
codeConfig={{
...codeConfig,
Expand Down
Loading