Skip to content

v0.5.2 #189

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 28 commits into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
5d41c5b
Merge pull request #183 from code-hike/main
pomber May 19, 2022
49953ab
Add playground folder
pomber May 20, 2022
32ce7f9
Playground preview
pomber May 20, 2022
e02e93e
Modify output of dist folder to merge all .d.ts files
AeonFr May 20, 2022
4599e1b
Fix CodeHikeConfig import
pomber May 21, 2022
b7d7da7
Merge pull request #186 from AeonFr/merge-dts-files
pomber May 21, 2022
bf93a4e
Mostly styles
pomber May 21, 2022
8939e5f
Change default code
pomber May 21, 2022
39b6a82
Merge pull request #185 from code-hike/playground
pomber May 21, 2022
370ff74
Add build playground script
pomber May 21, 2022
84ab765
Lazy load languages on browser
pomber May 23, 2022
e9ed53b
Reload preview on change
pomber May 23, 2022
3885978
Add default theme
pomber May 23, 2022
97a411a
Add tab interaction
pomber May 23, 2022
ab47f5f
Add default config
pomber May 23, 2022
cb5a4c4
Shareable url
pomber May 23, 2022
0e6e55b
Render css
pomber May 23, 2022
db405b9
Add theme config
pomber May 23, 2022
77fa521
Style errors
pomber May 24, 2022
08ce7ad
Standalone preview
pomber May 24, 2022
4afcc01
Add resizer
pomber May 24, 2022
93f52c6
Add links
pomber May 24, 2022
3e61df6
Merge pull request #187 from code-hike/more-playground
pomber May 24, 2022
81edc7a
Update browser example
pomber May 25, 2022
34afd1d
Fix multi token annotations
pomber May 25, 2022
b3c88ed
Fix tabs background
pomber May 25, 2022
6d4af01
Update playground version
pomber May 25, 2022
71d5ea2
Merge pull request #188 from code-hike/rose-pine-themes
pomber May 25, 2022
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
24 changes: 16 additions & 8 deletions examples/browser/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<title>Code Hike in the browser</title>
<link
rel="stylesheet"
href="https://esm.sh/@code-hike/mdx@0.4.0-next.1/dist/index.css"
href="https://esm.sh/@code-hike/mdx@0.5.2-next.2/dist/index.css"
/>
</head>
<body>
Expand All @@ -13,18 +13,26 @@
<script type="module">
import React from "https://esm.sh/react"
import ReactDOM from "https://esm.sh/react-dom"
import { evaluate } from "https://esm.sh/@mdx-js/mdx?dev"
import { evaluate } from "https://esm.sh/@mdx-js/mdx?bundle"
import * as runtime from "https://esm.sh/react/jsx-runtime.js"

import { remarkCodeHike } from "https://esm.sh/@code-hike/mdx@0.4.0-next.1/dist/index.browser.mjs"
import { CH } from "https://esm.sh/@code-hike/mdx@0.4.0-next.1/components"
import theme from "https://esm.sh/[email protected]/themes/nord.json" assert { type: "json" }
import { remarkCodeHike } from "https://esm.sh/@code-hike/mdx@0.5.2-next.2/dist/index.browser.mjs?bundle"
import { CH } from "https://esm.sh/@code-hike/mdx@0.5.2-next.2/components"
import theme from "https://esm.sh/[email protected]/themes/dracula.json" assert { type: "json" }

const mdx = `# Test

~~~python hello.py mark=1[22:30]
print("Rendered with Code Hike")
~~~
<CH.Code>

~~~python hello.py mark=1[22:30]
print("Rendered with Code Hike")
~~~

~~~js hello.js mark=1[23:36]
console.log("Rendered with Code Hike")
~~~

</CH.Code>
`

const { default: Content } = await evaluate(mdx, {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
"packages": [
"packages/mdx",
"examples/*",
"playground",
"site"
]
},
"scripts": {
"build": "lerna run --stream build",
"build:lib": "lerna run --stream --scope @*/mdx build",
"build:playground": "lerna run --stream --scope playground build",
"test": "lerna run --stream test",
"dev": "lerna run --stream --scope @*/mdx dev",
"release": "auto shipit"
Expand Down
27 changes: 27 additions & 0 deletions packages/mdx/dev/content/markdown.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Test nested syntax highlighting

````md hi.md
# Hi

```js
export default {
data() {
return {
greeting: "Hello World!",
}
},
}
```

[lorem](https://loremipsum.com)
````

```js hi.js
export default {
data() {
return {
greeting: "Hello World!",
}
},
}
```
2 changes: 1 addition & 1 deletion packages/mdx/dev/files.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from "fs"
import { remarkCodeHike } from "../src/index"
import { compile } from "@mdx-js/mdx"
import theme from "shiki/themes/nord.json"
import theme from "shiki/themes/rose-pine-moon.json"
import { withDebugger } from "mdx-debugger"

export async function getFiles() {
Expand Down
2 changes: 2 additions & 0 deletions packages/mdx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"scripts": {
"dev": "next",
"build": "rollup -c rollup.config.js",
"watch": "rollup --watch -c rollup.config.js",
"test": "vitest",
"coverage": "vitest run --coverage"
},
Expand Down Expand Up @@ -71,6 +72,7 @@
"react-json-view": "^1.21.3",
"rollup": "^2.41.2",
"rollup-plugin-delete": "^2.0.0",
"rollup-plugin-dts": "^4.2.1",
"rollup-plugin-postcss": "^4.0.0",
"rollup-plugin-typescript2": "^0.27.1",
"sass": "^1.49.9",
Expand Down
15 changes: 15 additions & 0 deletions packages/mdx/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import path from "path"
import json from "@rollup/plugin-json"
import del from "rollup-plugin-delete"
import replace from "@rollup/plugin-replace"
import dts from "rollup-plugin-dts"
import { nodeResolve } from "@rollup/plugin-node-resolve"
// import { terser } from "rollup-plugin-terser"
import commonjs from "@rollup/plugin-commonjs"
Expand Down Expand Up @@ -72,6 +73,12 @@ export default function makeConfig(commandOptions) {
}),
],
},
{
input: `src/index.tsx`,
output: [{ file: `./dist/index.d.ts`, format: "es" }],
external: [...remarkExternal, "shiki"],
plugins: [dts()],
},
// for the browser esm we need to replace shiki with shiki/dist/index.browser.mjs
// https://github.com/shikijs/shiki/issues/131#issuecomment-1094281851
{
Expand Down Expand Up @@ -125,5 +132,13 @@ export default function makeConfig(commandOptions) {
}),
],
},
{
input: `src/components.tsx`,
output: [
{ file: `./dist/components.d.ts`, format: "es" },
],
external: clientExternal,
plugins: [dts()],
},
]
}
8 changes: 7 additions & 1 deletion packages/mdx/src/highlighter/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,17 @@ export async function highlight({
}
}
if (highlighterPromise === null) {
const isBrowser = typeof window !== "undefined"
// if we are on the server we load all the languages
// if we are on the browser just load the first language
// subsequent calls with different languages will lazy load
const langs = isBrowser ? [lang as Lang] : undefined

// TODO add version
setCDN("https://unpkg.com/shiki/")
highlighterPromise = getHighlighter({
theme: theme as IShikiTheme,
// langs: [lang as Lang], // TODO change lang from string to Lang
langs,
})
}

Expand Down
2 changes: 1 addition & 1 deletion packages/mdx/src/mdx-client/code.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
EditorProps,
EditorStep,
} from "../mini-editor"
import { CodeHikeConfig } from "remark/config"
import { CodeHikeConfig } from "../remark/config"

export function Code(
props: EditorProps & Partial<CodeHikeConfig>
Expand Down
10 changes: 9 additions & 1 deletion packages/mdx/src/mini-editor/editor-frame.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const EditorFrame = React.forwardRef<
style={{
background: getColor(
theme,
ColorName.EditorGroupHeaderBackground
ColorName.EditorBackground
),
...style,
}}
Expand All @@ -72,6 +72,10 @@ export const EditorFrame = React.forwardRef<
className={"ch-frame-title-bar"}
style={{
color: getColor(theme, ColorName.IconForeground),
background: getColor(
theme,
ColorName.EditorGroupHeaderBackground
),
}}
>
<TabsContainer
Expand All @@ -98,6 +102,10 @@ export const EditorFrame = React.forwardRef<
theme,
ColorName.IconForeground
),
background: getColor(
theme,
ColorName.EditorGroupHeaderBackground
),
}}
>
<TabsContainer
Expand Down
24 changes: 18 additions & 6 deletions packages/mdx/src/remark/comment-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,25 @@ export function getCommentData(
}
}

function getTextAfter(
line: Code["lines"][0],
prefix: string
) {
const firstIndex = line.tokens.findIndex(t =>
t.content.trim().startsWith(prefix)
)
if (firstIndex === -1) {
return undefined
}
return line.tokens
.slice(firstIndex)
.map(t => t.content)
.join("")
}

const commentRegex = /\/\/\s+(\w+)(\S*)\s*(.*)/
function otherComment(line: Code["lines"][0]) {
const comment = line.tokens.find(t =>
t.content.trim().startsWith("//")
)?.content
const comment = getTextAfter(line, "//")

if (!comment) {
return []
Expand All @@ -56,9 +70,7 @@ const bashLikeLangs = [
]
const bashLikeCommentRegex = /#\s+(\w+)(\S*)\s*(.*)/
function bashLikeComment(line: Code["lines"][0]) {
const comment = line.tokens.find(t =>
t.content.trim().startsWith("#")
)?.content
const comment = getTextAfter(line, "#")

if (!comment) {
return []
Expand Down
2 changes: 1 addition & 1 deletion packages/mdx/src/remark/transform.preview.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fetch from "node-fetch"
import { visitAsync, toJSX } from "./unist-utils"
import { JsxNode, SuperNode } from "./nodes"

Expand All @@ -14,6 +13,7 @@ export async function getPresetConfig(
const prefix = "https://codesandbox.io/s/"
const csbid = url.slice(prefix.length)
const configUrl = `https://codesandbox.io/api/v1/sandboxes/${csbid}/sandpack`
const { default: fetch } = await import("node-fetch")
const res = await fetch(configUrl)
return await res.json()
}
Expand Down
5 changes: 3 additions & 2 deletions packages/mdx/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"esnext"
],
"importHelpers": true,
"declaration": true,
"declaration": false,
"sourceMap": true,
"strict": false,
"noUnusedLocals": false,
Expand All @@ -39,7 +39,8 @@
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"isolatedModules": true,
"incremental": true
"incremental": true,
"tsBuildInfoFile": "./dist/tsbuildinfo.json"
},
"exclude": [
"node_modules"
Expand Down
28 changes: 28 additions & 0 deletions playground/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?


# Contentlayer
.contentlayer
30 changes: 30 additions & 0 deletions playground/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/src/favicon.ico" />
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png" />
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png" />
<meta
property="description"
content="Try Code Hike directly in your browser"
/>
<meta property="og:title" content="Code Hike Playground" />
<meta
property="og:description"
content="Try Code Hike directly in your browser"
/>
<meta name="twitter:site" content="@codehike_" />
<!-- <meta name="twitter:card" content="summary_large_image" />
<meta name="image" content="{imageUrl}" />
<meta itemprop="image" content="{imageUrl}" />
<meta name="twitter:image" content="{imageUrl}" />
<meta property="og:image" content="{imageUrl}" /> -->
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Code Hike Playground</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.jsx"></script>
</body>
</html>
26 changes: 26 additions & 0 deletions playground/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "playground",
"private": true,
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"@code-hike/mdx": "^0.5.1",
"@mdx-js/mdx": "^2.1.1",
"@monaco-editor/react": "^4.4.5",
"lz-string": "^1.4.4",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-error-boundary": "^3.1.4",
"react-split-pane": "^0.1.92"
},
"devDependencies": {
"@types/react": "^17.0.2",
"@types/react-dom": "^17.0.2",
"@vitejs/plugin-react": "^1.3.0",
"vite": "^2.9.9"
}
}
Loading