I'm looking to generate popover highlight tags on data that I've gotten from a query. Currently, I've tried finding substrings wrapped in curly braces within the text, then replacing them with components, but I can't figure out how to get Vue to render & mount the new components
Example text:
Lorem {ipsum} dolor sit amet, consectetur {adipiscing} elit, sed do eiusmod tempor incididunt
I've created a fairly simple regex to find and grab text wrapped in curly braces:
\{(.[^{}]*)\}
And this is the code I've tried so far:
<template>
<span v-html="paragraphText"></span>
</template>
<script lang="ts">
import {MyComponent} from '#components';
export default defineComponent({
data () {
return {
paragraphText: '',
}
},
created () {
const re = /\{(.[^{}]*)\}/g;
let newText = `${this.text}`;
let match: RegExpExecArray;
do {
match = re.exec(newText);
if (match) {
newText = newText.replace(
match[0],
`<MyComponent ref='${match[1]}' style='font-weight:bold;'>${match[1]}</MyComponent>`
);
}
} while (match);
this.paragraphText = newText;
},
components: {
MyComponent,
}
})
</script>
I'm well aware that v-html won't render components to avoid XSS attacks, but this is mostly just to show what I'm trying to achieve.
My reasoning behind having a component behind each one, is that I want the component to make a web request on hover, to grab some additional information about the word being highlighted.
I'm looking for the cleanest and/or most efficient solution possible - it doesn't have to be Options API