I have a description stored as html I want to render in my component. However, before it can be rendered I need to replace parts of the description with JSX components. However, unlike other questions I've seen that ask this I need to replace more than one type of thing in the description with JSX components. This requires multiple regex statements. Take the following description as an example:
<div style="white-space: pre-line;">
This is my video.
0:00 Intro
4:12 Point 1
9:12 Point 2
14:12 Closing Point
Check out my website at https://example.com
#tag #tag2 #tag3
</div>
In this description all links need to be wrapped in an link element, timestamps need to be converted into a link that changes the video time and hashtags need to be converted into a link that takes the user to the search page.
This is how I formatted the description when I was using jQuery:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="description" style="white-space: pre-line;"></div>
<script>
var description = `
This is my video.
0:00 Intro
4:12 Point 1
9:12 Point 2
14:12 Closing Point
Check out my website at https://example.com
#tag #tag2 #tag3
`;
$('#description').html(createLinks(createHashtagLinks(formatTimestamps(description))));
function createLinks(text) {
return text.replace(/(https?:\/\/[^\s]+)/g, '<a href="$1" target="_blank" rel="noopener noreferrer">$1</a>')
}
function createHashtagLinks(text) {
return text.replace(/#\w*[a-zA-Z]\w*/g, '<a href="/videos?search=$&">$&</a>');
}
function formatTimestamps(text) {
return text.replace(/^[0-5]?\d(?::(?:[0-5]?\d)){1,2}/gm, function (match) {
var timeArray = match.split(':').reverse();
var seconds = 0;
var i = 1;
for (let unit of timeArray) {
seconds += unit * i;
i *= 60;
}
return `<a data-seconds="${seconds}" href="#">${match}</a>`
});
}
</script>
<a>
elements I need to instead replace them with <Link>
components. How can I do this in React?
` or only `
`? And you want malformed syntax to use a best guess approach, or to completely fail to parse?) And secondly, do you want to allow content editors powerful yet potentially dangerous tools, such as running their own scripts, or do you want to limit their capabilities? If content editing is generally available, you'll want the latter. – Lionel Rowe Sep 28 '20 at 10:49
` is added to `allow.js` and found in the description React will throw the error `Error: br is a void element tag and must neither have children nor use dangerouslySetInnerHTML. allow.js line: 53`. Nevertheless, thank you for this incredible answer. – No stupid questions Sep 28 '20 at 13:28