Skip to content

Commit 7ca0dd7

Browse files
committed
rework dynamic component lookup for composition api
1 parent b5413b3 commit 7ca0dd7

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

packages/vue/src/base-course/Components/MdTokenRenderer.vue

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
<span v-if="isText(token)">
33
<span v-if="!token.tokens || token.tokens.length === 0">
44
<span v-if="isComponent(token)">
5-
<component :is="parsedComponent(token).is" v-if="!last" :text="parsedComponent(token).text" />
5+
<!-- <component :is="parsedComponent(token).is" v-if="!last" :text="parsedComponent(token).text" /> -->
6+
<component :is="getComponent(parsedComponent(token).is)" v-if="!last" :text="parsedComponent(token).text" />
67
</span>
78
<span v-else-if="containsComponent(token)">
89
<md-token-renderer v-for="(subTok, j) in splitTextToken(token)" :key="j" :token="subTok" />
@@ -120,6 +121,14 @@ import {
120121
import CodeBlockRenderer from './CodeBlockRenderer.vue';
121122
import FillInInput from '@/courses/default/questions/fillIn/fillInInput.vue';
122123
import { MarkedToken, Tokens } from 'marked';
124+
import { markRaw } from 'vue';
125+
126+
// Register components to be used in the template
127+
// In Vue 3 with <script setup>, components used via :is must be explicitly registered
128+
const components = {
129+
fillIn: markRaw(FillInInput),
130+
// Add other dynamic components here
131+
};
123132
124133
// Define component props
125134
const props = defineProps({
@@ -134,19 +143,19 @@ const props = defineProps({
134143
},
135144
});
136145
137-
// Register components for runtime use
138-
const components = {
139-
fillIn: FillInInput,
140-
RadioMultipleChoice,
141-
};
142-
143146
// Methods
144147
function isComponent(token: MarkedToken): boolean {
145-
return _isComponent(token);
148+
console.log('[isComponent] Pre: Input token:', token);
149+
const result = _isComponent(token);
150+
console.log('[isComponent] Post: Result:', result);
151+
return result;
146152
}
147153
148154
function containsComponent(token: MarkedToken): boolean {
149-
return _containsComponent(token);
155+
console.log('[containsComponent] Pre: Input token:', token);
156+
const result = _containsComponent(token);
157+
console.log('[containsComponent] Post: Result:', result);
158+
return result;
150159
}
151160
152161
function splitTextToken(token: MarkedToken): Tokens.Text[] {
@@ -180,12 +189,21 @@ function parsedComponent(token: MarkedToken): {
180189
text = token.raw;
181190
}
182191
192+
console.log('[parsedComponent] Token:', token);
193+
console.log('[parsedComponent] Text extracted:', text);
194+
195+
// This now returns a component from our registered components object
183196
return {
184-
is: 'fillIn',
197+
is: 'fillIn', // This should match a key in the components object
185198
text,
186199
};
187200
}
188201
202+
function getComponent(componentName: string) {
203+
// Return the component instance from our components object
204+
return components[componentName as keyof typeof components];
205+
}
206+
189207
function decodeBasicEntities(text: string): string {
190208
return text
191209
.replace(/&#39;/g, "'")
@@ -199,7 +217,7 @@ function isText(tok: TokenOrComponent): boolean {
199217
return (tok as any).inLink === undefined && tok.type === 'text';
200218
}
201219
202-
// Make these functions available to the template
220+
// Make these functions and objects available to the template
203221
defineExpose({
204222
isComponent,
205223
containsComponent,
@@ -208,19 +226,12 @@ defineExpose({
208226
parsedComponent,
209227
decodeBasicEntities,
210228
isText,
229+
components,
230+
getComponent,
211231
});
212232
</script>
213233

214234
<style lang="css" scoped>
215-
/* @import './../../../node_modules/highlight.js/styles/atelier-seaside-light.css'; */
216-
/* @import './../../../node_modules/highlight.js/styles/stackoverflow-light.css'; */
217-
/* @import './../../../node_modules/highlight.js/styles/xt256.css'; */
218-
/* @import './../../../node_modules/highlight.js/styles/zenburn.css'; */
219-
/* @import './../../../node_modules/highlight.js/styles/tomorrow.css'; */
220-
/* @import './../../../node_modules/highlight.js/styles/lioshi.css'; */
221-
/* @import './../../../node_modules/highlight.js/styles/rainbow.css'; */
222-
/* @import './../../../node_modules/highlight.js/styles/monokai-sublime.css'; */
223-
224235
blockquote {
225236
border-left: 3px teal solid;
226237
padding-left: 8px;

0 commit comments

Comments
 (0)