Skip to content

Commit fd6867e

Browse files
committed
feat: Add syntax highlighting for code blocks in playground preview
- Install highlight.js dependency - Highlight code blocks in preview after HTML is rendered
1 parent d12f66b commit fd6867e

3 files changed

Lines changed: 26 additions & 1 deletion

File tree

docs/.vitepress/components/DjotPlayground.vue

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<script setup lang="ts">
2-
import { ref, watch, onMounted } from 'vue'
2+
import { ref, watch, onMounted, nextTick } from 'vue'
3+
import hljs from 'highlight.js'
34
45
const SANDBOX_URL = 'https://sandbox.dereuromark.de/sandbox/djot'
56
@@ -37,6 +38,16 @@ const profile = ref('')
3738
const isLoading = ref(false)
3839
const error = ref('')
3940
const extensions = ref<string[]>([])
41+
const previewRef = ref<HTMLElement | null>(null)
42+
43+
async function highlightCodeBlocks() {
44+
await nextTick()
45+
if (previewRef.value) {
46+
previewRef.value.querySelectorAll('pre code').forEach((block) => {
47+
hljs.highlightElement(block as HTMLElement)
48+
})
49+
}
50+
}
4051
4152
const availableExtensions = [
4253
{ id: 'autolink', label: 'Autolink' },
@@ -107,6 +118,7 @@ async function convert() {
107118
} else {
108119
htmlOutput.value = data.html || ''
109120
error.value = ''
121+
highlightCodeBlocks()
110122
}
111123
} catch (err) {
112124
if (err instanceof Error && err.name === 'AbortError') {
@@ -199,6 +211,7 @@ function toggleExtension(extId: string) {
199211
</div>
200212
<div
201213
v-else-if="activeTab === 'preview'"
214+
ref="previewRef"
202215
class="output-content preview"
203216
v-html="htmlOutput"
204217
></div>

docs/package-lock.json

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"docs:preview": "vitepress preview"
99
},
1010
"devDependencies": {
11+
"highlight.js": "^11.11.1",
1112
"vitepress": "^1.6.3",
1213
"vue": "^3.5.13"
1314
}

0 commit comments

Comments
 (0)