I have a form in the admin area where users can enter text with short codes in them:
Heat furnace at [temp c = 200]
I want temp shortcode to be processed and converted to a Vue 2 component when it shows up in the interface.
Here's a simplified simplified Temperature.vue component:
<template> <span class="temperature"> {{ celsius }}°C </span> </template> <script> import { mapGetters, mapActions } from 'vuex' export default { props: ['celsius'], name: 'temperature' } </script>
And here's a simplified Instructions.vue component that will parse and display text using shortcodes:
<template> <div v-html="parseShortcodes(text)"></div> </template> <script> import ShortcodeParser from 'meta-shortcodes' import Temperature from 'Temperature.vue' export default { data: () => { return { text: 'Heat oven at [temp c=200]', parser: ShortcodeParser() } }, components: [ Temperature ], methods: { parseShortcodes(text) { return this.parser.parse(text) } }, created() { this.parser.add('temp', function(options) { return '<temperature :celsius="'+options.c+'"></temperature>' }) } } </script>
The parsing works fine, and the lookup is printed on the interface. But the <temperature> displayed literally, and not as a Vue component, which is partly expected.
Heat oven at <temperature: celsius = "200"> </ temperature>
What I seem to be unable to wrap up is what I must take to ensure that it actually transforms into the Temperature component that I defined. Is it possible? Are there even more orthodox ways to do this that I am missing?
There is also a security issue when using v-html to render text. I donβt necessarily want it to be there, but I assume it is even more far-fetched to expect the Temperature component to appear from the escaped string. I can always sanitize before entering the database, but I would still like to avoid v-html , if at all possible.