web/utils/captionParser.js

20 lines
749 B
JavaScript
Raw Permalink Normal View History

2024-09-28 19:39:35 +02:00
export function parseAndWrapCaptions(caption) {
return caption.replace(/\[([^\]]+)\]\((https?:\/\/[^\)]+)\)/g, (match,text, content) => {
// Split the content by spaces to identify potential URLs
const parts = content.split(' ');
const url = parts.find(part => part.startsWith('https://'));
if (url) {
// If a URL is found, wrap the content in an anchor tag using the first URL
return `<a href="${url}"
target="_blank"
2024-09-28 19:40:37 +02:00
class="text-white underline"
2024-09-28 19:39:35 +02:00
style="text-underline-offset: 4px;"
>${text}</a>`;
} else {
// If no URL, join the parts with a comma
return content.split(' ').join(', ');
}
});
}