Skip to content

Commit b6a30aa

Browse files
danielroefarnabaz
andauthored
fix: reactively load components when body changes (#3283)
--------- Co-authored-by: Farnabaz <farnabaz@gmail.com>
1 parent 08df2bf commit b6a30aa

File tree

7 files changed

+68
-22
lines changed

7 files changed

+68
-22
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
},
5252
"dependencies": {
5353
"@nuxt/kit": "^3.16.1",
54-
"@nuxtjs/mdc": "^0.16.1",
54+
"@nuxtjs/mdc": "https://pkg.pr.new/@nuxtjs/mdc@cd1c4fd",
5555
"@shikijs/langs": "^3.2.1",
5656
"@sqlite.org/sqlite-wasm": "3.49.1-build2",
5757
"@webcontainer/env": "^1.1.1",

playground/components/TestA.vue

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<template>
2+
<span class="text-red-500">
3+
Test A <slot />
4+
</span>
5+
</template>

playground/components/TestB.vue

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<template>
2+
<span class="text-blue-500">
3+
Test B <slot />
4+
</span>
5+
</template>

playground/components/TestC.vue

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<template>
2+
<span class="text-green-500">
3+
Test C <slot />
4+
</span>
5+
</template>

playground/components/playground.vue

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<template>
2+
<div>
3+
<ContentRenderer
4+
:value="value"
5+
/>
6+
<UButton @click="randomize">
7+
Change Component
8+
</UButton>
9+
</div>
10+
</template>
11+
12+
<script setup lang="ts">
13+
const body = ref({
14+
type: 'root',
15+
children: [
16+
{
17+
type: 'element', tag: 'p', props: {},
18+
children: [
19+
{ type: 'text', value: 'Hello ' },
20+
{ type: 'element', tag: 'span', props: {}, children: [
21+
{ type: 'text', value: 'world' },
22+
] },
23+
{ type: 'text', value: '!' },
24+
],
25+
},
26+
],
27+
})
28+
const value = computed(() => {
29+
return { body: body.value }
30+
})
31+
32+
const tags = ['TestA', 'TestB', 'TestC']
33+
const randomize = () => {
34+
let tag = body.value.children[0].children[1].tag
35+
while (tag === body.value.children[0].children[1].tag) {
36+
tag = tags[Math.floor(Math.random() * tags.length)]
37+
}
38+
body.value.children[0].children[1].tag = tag
39+
body.value.children[0].children[1].children![0]!.value = `world (${tag})`
40+
}
41+
</script>

pnpm-lock.yaml

+8-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/runtime/components/ContentRenderer.vue

+3-16
Original file line numberDiff line numberDiff line change
@@ -100,27 +100,15 @@ const data = computed(() => {
100100
const proseComponentMap = Object.fromEntries(['p', 'a', 'blockquote', 'code', 'pre', 'code', 'em', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'img', 'ul', 'ol', 'li', 'strong', 'table', 'thead', 'tbody', 'td', 'th', 'tr', 'script'].map(t => [t, `prose-${t}`]))
101101
102102
const { mdc } = useRuntimeConfig().public || {}
103-
const tags = {
103+
const tags = computed(() => ({
104104
...mdc?.components?.prose && props.prose !== false ? proseComponentMap : {},
105105
...mdc?.components?.map || {},
106106
...toRaw(props.data?.mdc?.components || {}),
107107
...props.components,
108-
}
109-
110-
const key = computed(() => {
111-
if (!import.meta.dev) {
112-
return undefined
113-
}
114-
const res = Array.from(new Set(body.value ? loadComponents(body.value, { tags }) : []))
115-
.filter(t => localComponents.includes(pascalCase(String(t))))
116-
.sort()
117-
.join('.')
118-
119-
return res
120-
})
108+
}))
121109
122110
const componentsMap = computed(() => {
123-
return body.value ? resolveContentComponents(body.value, { tags }) : {}
111+
return body.value ? resolveContentComponents(body.value, { tags: tags.value }) : {}
124112
})
125113
126114
function resolveVueComponent(component: string | Renderable) {
@@ -214,7 +202,6 @@ function findMappedTag(node: MDCElement, tags: Record<string, string>) {
214202
<template>
215203
<MDCRenderer
216204
v-if="!isEmpty"
217-
:key="key"
218205
:body="body"
219206
:data="data"
220207
:class="props.class"

0 commit comments

Comments
 (0)