| @@ -256,35 +256,24 @@ | ||
| 256 | 256 | sendMessageToChatbot(question); |
| 257 | 257 | }); |
| 258 | 258 | |
| 259 | 259 | |
| 260 | -// Add this new function to handle markdown headers | |
| 261 | -function formatMarkdownHeaders(text) { | |
| 262 | - // Handle h1 to h6 headers | |
| 263 | - return text.replace(/^(#{1,6})\s(.+)$/gm, function(match, hashes, content) { | |
| 264 | - const level = hashes.length; | |
| 265 | - return `<h${level} class="chat-heading">${content}</h${level}>`; | |
| 266 | - }); | |
| 267 | -} | |
| 268 | - | |
| 269 | -// Update the linkify function to handle both URLs and markdown | |
| 260 | +// Use the linkTarget in your linkify function | |
| 270 | 261 | function linkify(inputText) { |
| 271 | - // First process markdown headers | |
| 272 | - let processedText = formatMarkdownHeaders(inputText); | |
| 273 | - | |
| 274 | - // Then process links as before | |
| 262 | + // Check for already linked URLs and skip them | |
| 263 | + // We use negative lookaheads to skip anything already in an <a> tag | |
| 275 | 264 | var markdownLinkPattern = /\[([^\]]+)\]\((https?:\/\/[^\s]+)\)/g; |
| 276 | - processedText = processedText.replace(markdownLinkPattern, '<a href="$2" target="' + linkTarget + '">$1</a>'); | |
| 265 | + var replacedText = inputText.replace(markdownLinkPattern, '<a href="$2" target="' + linkTarget + '">$1</a>'); | |
| 277 | 266 | |
| 278 | 267 | // Replace standalone URLs not already in an <a> tag |
| 279 | 268 | var urlPattern = /(^|[^">])(https?:\/\/[^\s<]+)/gim; |
| 280 | - processedText = processedText.replace(urlPattern, '$1<a href="$2" target="' + linkTarget + '">$2</a>'); | |
| 269 | + replacedText = replacedText.replace(urlPattern, '$1<a href="$2" target="' + linkTarget + '">$2</a>'); | |
| 281 | 270 | |
| 282 | 271 | // Replace "www." prefixed URLs not already in an <a> tag |
| 283 | 272 | var wwwPattern = /(^|[^">])(www\.[\S]+(\b|$))(?![^<]*<\/a>)/gim; |
| 284 | - processedText = processedText.replace(wwwPattern, '$1<a href="http://$2" target="' + linkTarget + '">$2</a>'); | |
| 273 | + replacedText = replacedText.replace(wwwPattern, '$1<a href="http://$2" target="' + linkTarget + '">$2</a>'); | |
| 285 | 274 | |
| 286 | - return processedText; | |
| 275 | + return replacedText; | |
| 287 | 276 | } |
| 288 | 277 | |
| 289 | 278 | |
| 290 | 279 | function scrollElementToTop(element) { |
| @@ -391,8 +380,19 @@ | ||
| 391 | 380 | showActivePdf(response.data.filename); |
| 392 | 381 | activePdfFile = response.data.filename; |
| 393 | 382 | } |
| 394 | 383 | |
| 384 | + // Add redirect check here | |
| 385 | + if (response.redirect_url) { | |
| 386 | + let responseText = response.text || ''; | |
| 387 | + if (responseText) { | |
| 388 | + replaceLastMessage("bot", responseText); | |
| 389 | + } | |
| 390 | + setTimeout(() => { | |
| 391 | + window.location.href = response.redirect_url; | |
| 392 | + }, 1500); | |
| 393 | + return; | |
| 394 | + } | |
| 395 | 395 | |
| 396 | 396 | // Check for live agent response |
| 397 | 397 | if (response.success && response.data && response.data.status === 'waiting_for_agent') { |
| 398 | 398 | updateChatModeIndicator('agent'); |