Skip to content

Commit fde999d

Browse files
committed
chore: improve code highlighting
1 parent 746de83 commit fde999d

4 files changed

Lines changed: 25 additions & 60 deletions

File tree

packages/mdc-syntax/build.config.mjs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,17 @@ export default defineBuildConfig({
44
entries: [
55
{
66
type: 'bundle',
7-
input: ['./src/index.ts', './src/stream.ts'],
7+
input: [
8+
'./src/index.ts',
9+
'./src/stream.ts',
10+
'./src/react/index.ts',
11+
'./src/react/components/MDC.tsx',
12+
'./src/react/components/MDCRenderer.tsx',
13+
'./src/react/components/index.tsx',
14+
'./src/react/components/stream.tsx',
15+
'./src/react/components/prose/ProsePre.tsx',
16+
'./src/react/components/prose/ProsePreShiki.tsx',
17+
],
818
},
919
{
1020
type: 'transform',
@@ -16,18 +26,5 @@ export default defineBuildConfig({
1626
input: './src/utils',
1727
outDir: './dist/utils',
1828
},
19-
{
20-
type: 'bundle',
21-
input: [
22-
'./src/react/index.ts',
23-
'./src/react/components/MDC.tsx',
24-
'./src/react/components/MDCRenderer.tsx',
25-
'./src/react/components/index.tsx',
26-
'./src/react/components/stream.tsx',
27-
'./src/react/components/prose/ProsePre.tsx',
28-
'./src/react/components/prose/ProsePreShiki.tsx',
29-
],
30-
outDir: './dist',
31-
},
3229
],
3330
})

packages/mdc-syntax/src/vue/components/MDC.ts

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { PropType } from 'vue'
22
import { computed, defineComponent, h, shallowRef, watch } from 'vue'
33
import type { ParseResult } from '../../index'
4-
import { parse, parseAsync } from '../../index'
4+
import { parseAsync } from '../../index'
55
import type { ParseOptions } from '../../types'
66
import { MDCRenderer } from './MDCRenderer'
77

@@ -110,22 +110,9 @@ export const MDC = defineComponent({
110110
}))
111111

112112
async function parseMarkdown() {
113-
if (!props.streaming) {
114-
if (!streamComponents.value) {
115-
streamComponents.value = await import('mdc-syntax/vue/components/stream')
116-
.then(m => m.proseStreamComponents)
117-
}
118-
parsed.value = await parseAsync(markdown.value, {
119-
highlight: !props.streaming,
120-
...props.options,
121-
})
122-
}
123-
else {
124-
parsed.value = parse(markdown.value, {
125-
...props.options,
126-
highlight: false,
127-
})
128-
}
113+
parsed.value = await parseAsync(markdown.value, {
114+
...props.options,
115+
})
129116
}
130117

131118
watch(markdown, parseMarkdown)

packages/mdc-syntax/src/vue/components/prose/ProsePre.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ async function copyCode() {
119119
</div>
120120

121121
<!-- Shiki renderer -->
122-
<pre class="shiki-container bg-neutral-100 dark:bg-neutral-800 rounded-b-lg p-4 border border-neutral-300 dark:border-neutral-700"><slot /></pre>
122+
<pre class="shiki-container bg-neutral-100 dark:bg-neutral-800 overflow-x-auto rounded-b-lg p-4 border border-neutral-300 dark:border-neutral-700"><slot /></pre>
123123
</div>
124124
</template>
125125

packages/mdc-syntax/src/vue/components/prose/ProsePreShiki.vue

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,8 @@
11
<script setup lang="ts">
22
import { ShikiCachedRenderer } from 'shiki-stream/vue'
3-
import { createHighlighter } from 'shiki'
43
import { computed, onMounted, ref, watch } from 'vue'
54
import { textContent } from 'minimark'
6-
7-
// Singleton highlighter instance shared across all code blocks
8-
let highlighterInstance: any = null
9-
let highlighterPromise: Promise<any> | null = null
10-
11-
async function getHighlighter() {
12-
if (highlighterInstance) {
13-
return highlighterInstance
14-
}
15-
16-
if (!highlighterPromise) {
17-
highlighterPromise = createHighlighter({
18-
themes: ['material-theme-lighter', 'material-theme-palenight', 'github-dark', 'github-light'],
19-
langs: ['javascript', 'typescript', 'vue', 'html', 'css', 'json', 'markdown', 'bash', 'tsx', 'shell', 'text'],
20-
}).then((hl) => {
21-
highlighterInstance = hl
22-
return hl
23-
})
24-
}
25-
26-
return highlighterPromise
27-
}
5+
import { getHighlighter } from '../../../utils/shiki-highlighter'
286
297
const props = withDefaults(defineProps<{
308
// @eslint-disable-next-line vue/prop-name-casing
@@ -37,7 +15,7 @@ const props = withDefaults(defineProps<{
3715
fallbackWithHeaderClass?: string
3816
shikiStyle?: Record<string, string>
3917
}>(), {
40-
theme: 'github-dark',
18+
theme: 'material-theme-palenight',
4119
containerClass: 'my-4',
4220
fallbackClass: 'bg-neutral-100 dark:bg-neutral-800 text-neutral-800 dark:text-neutral-200 p-4 rounded-lg overflow-x-auto border border-neutral-300 dark:border-neutral-700',
4321
fallbackWithHeaderClass: 'bg-neutral-100 dark:bg-neutral-800 text-neutral-800 dark:text-neutral-200 p-4 pt-12 rounded-lg overflow-x-auto m-0 border border-neutral-300 dark:border-neutral-700',
@@ -100,7 +78,10 @@ async function copyCode() {
10078
10179
// Load highlighter on mount
10280
onMounted(() => {
103-
getHighlighter()
81+
getHighlighter({
82+
themes: { light: 'material-theme-palenight', dark: 'material-theme-lighter' },
83+
languages: ['javascript', 'typescript', 'vue', 'html', 'css', 'json', 'markdown', 'bash', 'jsx', 'tsx', 'shell'],
84+
})
10485
.then((hl) => {
10586
highlighter.value = hl
10687
isLoading.value = false
@@ -175,7 +156,7 @@ onMounted(() => {
175156
<!-- Loading state -->
176157
<pre
177158
v-if="isLoading"
178-
class="bg-neutral-100 dark:bg-neutral-800 rounded-lg pt-16 p-4 border border-neutral-300 dark:border-neutral-700"
159+
class="bg-neutral-100 dark:bg-neutral-800 rounded-lg pt-12 p-4 border border-neutral-300 dark:border-neutral-700"
179160
><code>{{ codeContent }}</code></pre>
180161

181162
<!-- Shiki renderer -->
@@ -185,8 +166,8 @@ onMounted(() => {
185166
:highlighter="highlighter"
186167
:code="codeContent"
187168
:lang="language || 'text'"
188-
:theme="theme || 'github-dark'"
189-
class="shiki-container bg-neutral-100 dark:bg-neutral-800 rounded-lg pt-16 p-4 border border-neutral-300 dark:border-neutral-700"
169+
:theme="theme || 'material-theme-palenight'"
170+
class="shiki-container bg-neutral-100 dark:bg-neutral-800 rounded-l overflow-x-auto pt-12 p-4 border border-neutral-300 dark:border-neutral-700"
190171
:style="shikiStyle"
191172
/>
192173

0 commit comments

Comments
 (0)