Skip to content

Commit

Permalink
fix(monaco): editor colors interop, fix #594
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Feb 18, 2024
1 parent 9ed0044 commit 788250d
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@rollup/plugin-replace": "^5.0.5",
"@rollup/plugin-terser": "^0.4.4",
"@shikijs/markdown-it": "workspace:*",
"@shikijs/monaco": "workspace:*",
"@shikijs/rehype": "workspace:*",
"@shikijs/transformers": "workspace:*",
"@shikijs/vitepress-twoslash": "workspace:*",
Expand Down
3 changes: 2 additions & 1 deletion packages/monaco/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"build": "unbuild",
"dev": "unbuild --stub",
"prepublishOnly": "nr build",
"test": "vitest"
"test": "vitest",
"play": "nr -C playground play"
},
"dependencies": {
"@shikijs/core": "workspace:*"
Expand Down
28 changes: 28 additions & 0 deletions packages/monaco/playground/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + TS</title>
</head>
<body>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
#app {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
background-color: #f3f4f6;
}
</style>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
13 changes: 13 additions & 0 deletions packages/monaco/playground/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "playground",
"type": "module",
"version": "0.0.0",
"private": true,
"scripts": {
"play": "vite"
},
"devDependencies": {
"typescript": "^5.2.2",
"vite": "^5.1.0"
}
}
30 changes: 30 additions & 0 deletions packages/monaco/playground/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { getHighlighter } from 'shiki'
import { shikiToMonaco } from '@shikijs/monaco'
import * as monaco from 'monaco-editor-core'

// Create the highlighter, it can be reused
const highlighter = await getHighlighter({
themes: [
'min-dark',
],
langs: [
'javascript',
'typescript',
'vue',
],
})

// Register the languageIds first. Only registered languages will be highlighted.
monaco.languages.register({ id: 'vue' })
monaco.languages.register({ id: 'typescript' })
monaco.languages.register({ id: 'javascript' })

// Register the themes from Shiki, and provide syntax highlighting for Monaco. // [!code highlight:2]
shikiToMonaco(highlighter, monaco)

// Create the editor
monaco.editor.create(document.getElementById('app')!, {
value: 'const a = 1',
language: 'javascript',
theme: 'min-dark',
})
10 changes: 10 additions & 0 deletions packages/monaco/playground/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { fileURLToPath } from 'node:url'
import { defineConfig } from 'vite'

export default defineConfig({
resolve: {
alias: {
'@shikijs/monaco': fileURLToPath(new URL('../src/index.ts', import.meta.url)),
},
},
})
2 changes: 1 addition & 1 deletion packages/monaco/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function textmateThemeToMonacoTheme(theme: ThemeRegistrationResolved): Mo

const colors = Object.fromEntries(
Object.entries(theme.colors || {})
.map(([key, value]) => [key, normalizeColor(value)]),
.map(([key, value]) => [key, `#${normalizeColor(value)}`]),
)

return {
Expand Down
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
packages:
- packages/*
- packages/*/playground
- playground
- examples/*
- docs
Expand Down

0 comments on commit 788250d

Please sign in to comment.