Skip to content

Commit

Permalink
feat: syntax highlighting (#10)
Browse files Browse the repository at this point in the history
* feat: template highlighting

* feat: syntax highlighting
  • Loading branch information
timhanlon authored Jan 17, 2025
1 parent 9f8e018 commit 034b7bf
Show file tree
Hide file tree
Showing 8 changed files with 365 additions and 15 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ See [launch-editor](https://github.com/yyx990803/launch-editor/#supported-editor

Note: this does not currently work in the playground, due to `nuxt dev playground` overriding the `rootDir`.

## Syntax Highlighting

Syntax highlighting is provided by [Nuxt Shiki](https://github.com/nuxt-modules/shiki), and can be configured via the `shiki` key in your `nuxt.config.ts`.

<!-- ## Quick Setup
Install the module to your Nuxt application with one command:
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@nuxt/kit": "^3.15.0",
"@vue/compiler-sfc": "^3.3.0",
"defu": "^6.1.4",
"nuxt-shiki": "^0.3.0",
"pathe": "^2.0.0",
"tailwind-variants": "^0.3.0"
},
Expand Down
4 changes: 4 additions & 0 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ export default defineNuxtConfig({
},

compatibilityDate: '2025-01-03',

shiki: {
defaultTheme: 'nord',
},
})
336 changes: 336 additions & 0 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
updateTemplates,
useLogger,
addTypeTemplate,
installModule,
} from '@nuxt/kit'
import { resolve } from 'pathe'
import { parse } from '@vue/compiler-sfc'
Expand Down Expand Up @@ -102,6 +103,13 @@ export {}
`,
})

await installModule('nuxt-shiki', {
bundledLangs: ['vue'],
})
// https://github.com/pi0/nuxt-shiki/issues/41
nuxt.options.nitro.experimental = nuxt.options.nitro.experimental || {}
nuxt.options.nitro.experimental.wasm = true

// Ensure types are included in tsconfig
nuxt.hook('prepare:types', ({ references }) => {
references.push({ path: resolve(nuxt.options.buildDir, 'types/bedtime.d.ts') })
Expand Down
1 change: 1 addition & 0 deletions src/runtime/components/OpenInEditorButton.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<template>
<button
class="open-in-editor-button"
title="Open in editor"
>
<svg
xmlns="http://www.w3.org/2000/svg"
Expand Down
23 changes: 10 additions & 13 deletions src/runtime/components/TemplateView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,27 @@
<div
v-if="content"
class="template-view"
>
<pre><code>{{ content }}</code></pre>
</div>
v-html="html"

Check warning on line 5 in src/runtime/components/TemplateView.vue

View workflow job for this annotation

GitHub Actions / lint

'v-html' directive can lead to XSS attack
/>
</template>

<script setup lang="ts">
defineProps<{
import { getShikiHighlighter } from '#imports'
const props = defineProps<{
content?: string | null
}>()
const highlighter = await getShikiHighlighter()
const html = highlighter.highlight(props.content, { lang: 'vue' })
</script>

<style scoped>
.template-view {
background-color: var(--template-view-background-color);
:deep(.shiki) {
border: var(--template-view-border);
border-radius: var(--template-view-border-radius);
font-size: var(--template-view-font-size);
margin: var(--template-view-margin);
padding: var(--template-view-padding);
}
.template-view pre {
font-size: var(--template-view-font-size);
margin: 0;
overflow-x: scroll;
white-space: pre;
}
</style>
3 changes: 1 addition & 2 deletions src/runtime/variables.css
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@
--variant-title-text-color: #374151;

/* Template View */
--template-view-background-color: #f8f8f8;
--template-view-border: 1px solid #e5e5e5;
--template-view-border: 0;
--template-view-border-radius: 0.375rem;
--template-view-margin: 1rem 0 0 0;
--template-view-padding: 1rem;
Expand Down

0 comments on commit 034b7bf

Please sign in to comment.