|
| 1 | +<template> |
| 2 | + <div class="code-block-wrapper pa-2"> |
| 3 | + <div class="language-indicator" v-if="language">{{ language }}</div> |
| 4 | + <highlightjs :language="language" :code="code" /> |
| 5 | + </div> |
| 6 | +</template> |
| 7 | + |
| 8 | +<script setup lang="ts"> |
| 9 | +import hljs from 'highlight.js/lib/core'; |
| 10 | +import hljsVuePlugin from '@highlightjs/vue-plugin'; |
| 11 | +import { getCurrentInstance } from 'vue'; |
| 12 | +
|
| 13 | +// Import only the languages you need |
| 14 | +import javascript from 'highlight.js/lib/languages/javascript'; |
| 15 | +import typescript from 'highlight.js/lib/languages/typescript'; |
| 16 | +import css from 'highlight.js/lib/languages/css'; |
| 17 | +import html from 'highlight.js/lib/languages/xml'; |
| 18 | +import bash from 'highlight.js/lib/languages/bash'; |
| 19 | +import json from 'highlight.js/lib/languages/json'; |
| 20 | +import go from 'highlight.js/lib/languages/go'; |
| 21 | +import python from 'highlight.js/lib/languages/python'; |
| 22 | +
|
| 23 | +// Register languages |
| 24 | +hljs.registerLanguage('js', javascript); |
| 25 | +hljs.registerLanguage('javascript', javascript); |
| 26 | +hljs.registerLanguage('ts', typescript); |
| 27 | +hljs.registerLanguage('typescript', typescript); |
| 28 | +hljs.registerLanguage('css', css); |
| 29 | +hljs.registerLanguage('html', html); |
| 30 | +hljs.registerLanguage('bash', bash); |
| 31 | +hljs.registerLanguage('sh', bash); |
| 32 | +hljs.registerLanguage('json', json); |
| 33 | +hljs.registerLanguage('go', go); |
| 34 | +hljs.registerLanguage('golang', go); |
| 35 | +hljs.registerLanguage('python', python); |
| 36 | +
|
| 37 | +// Get access to the current component instance |
| 38 | +const instance = getCurrentInstance(); |
| 39 | +if (instance) { |
| 40 | + // Register the component locally |
| 41 | + instance.appContext.app.component('highlightjs', hljsVuePlugin.component); |
| 42 | +} |
| 43 | +
|
| 44 | +const props = defineProps({ |
| 45 | + code: { |
| 46 | + type: String, |
| 47 | + required: true, |
| 48 | + }, |
| 49 | + language: { |
| 50 | + type: String, |
| 51 | + default: '', |
| 52 | + }, |
| 53 | +}); |
| 54 | +</script> |
| 55 | + |
| 56 | +<style> |
| 57 | +/* Import a highlight.js theme */ |
| 58 | +@import 'highlight.js/styles/github.css'; |
| 59 | +
|
| 60 | +.code-block-wrapper { |
| 61 | + margin: 1rem 0; |
| 62 | + border-radius: 4px; |
| 63 | + background-color: #f6f8fa; |
| 64 | + overflow: auto; |
| 65 | + position: relative; |
| 66 | +} |
| 67 | +
|
| 68 | +.language-indicator { |
| 69 | + position: absolute; |
| 70 | + top: 8px; |
| 71 | + right: 10px; |
| 72 | + padding: 2px 8px; |
| 73 | + font-size: 0.75rem; |
| 74 | + color: #666; |
| 75 | + background-color: rgba(255, 255, 255, 0.7); |
| 76 | + border-radius: 4px; |
| 77 | + z-index: 1; |
| 78 | + font-family: monospace; |
| 79 | + text-transform: lowercase; |
| 80 | +} |
| 81 | +</style> |
0 commit comments