[server links] description

This commit is contained in:
JakubPapso committed 2024-07-02 22:25:29 +02:00
1 parent 1b7315617f
commit 484eca5548
4 files changed
+71 -121

No files matched your search

+52 -43
View File
@@ -1,47 +1,7 @@
export function getServerLinks(description) {
const os = detectOS()
import { useToast } from 'vue-toast-notification';
const toast = useToast();
// Create a temporary DOM element to manipulate the HTML content
const tempDiv = document.createElement('div')
tempDiv.innerHTML = description
const links = new Set()
const paragraphs = tempDiv.querySelectorAll('p')
paragraphs.forEach(p => {
const text = p.textContent.trim()
if (os === 'mac' && text.startsWith('smb://')) {
links.add(text)
} else if (os === 'win' && text.startsWith('\\\\')) {
links.add(`${text.replace(/\//g, '\\')}`)
} else if (os === 'mac' && text.startsWith('\\\\')) {
const smbLink = 'smb:' + text.replace(/\\/g, '/')
links.add(smbLink)
} else if (os === 'win' && text.startsWith('smb://')) {
const windowsLink = text.replace(/^smb:/, '').replace(/\//g, '\\')
links.add(`${windowsLink}`)
}
const anchor = p.querySelector('a')
if (anchor) {
const href = anchor.getAttribute('href')
if (os === 'mac' && href.startsWith('smb://')) {
links.add(href)
} else if (os === 'win' && href.startsWith('\\\\')) {
links.add(`${href.replace(/\//g, '\\')}`)
} else if (os === 'mac' && href.startsWith('\\\\')) {
const smbLink = 'smb:' + href.replace(/\\/g, '/')
links.add(smbLink)
} else if (os === 'win' && href.startsWith('smb://')) {
const windowsLink = href.replace(/^smb:/, '').replace(/\//g, '\\')
links.add(`${windowsLink}`)
}
}
})
return Array.from(links)
}
window.toast = toast
export function detectOS() {
const platform = window.navigator.platform
@@ -54,4 +14,53 @@ export function detectOS() {
return 'win'
}
return 'win'
}
export function prepareHTMLDescription(description) {
if (!description) return description
const os = detectOS()
// make an array from the description of strings from between <p> and </p> tags
const paragraphs = description.split('<p>').map(p => p.split('</p>')[0])
const modifiedParagraphs = paragraphs.map(paragraph => {
const words = paragraph.split(' ')
const modifiedWords = words.map(word => {
let link = null
if (os === 'mac' && word.startsWith('smb://')) {
link = word
} else if (os === 'win' && word.startsWith('\\\\')) {
link = `${word.replace(/\//g, '\\')}`
} else if (os === 'mac' && word.startsWith('\\\\')) {
const smbLink = 'smb:' + word.replace(/\\/g, '/')
link = smbLink
} else if (os === 'win' && word.startsWith('smb://')) {
const windowsLink = word.replace(/^smb:/, '').replace(/\//g, '\\')
link = `${windowsLink}`
}
if (!link) return word
return `<span style="cursor: pointer" onclick="${getCopyFunction(link)}">${link}</span>`
})
return `<p>${modifiedWords.join(' ')}</p>`
})
description = modifiedParagraphs.join('')
return description;
}
function getCopyFunction(text) {
return `
navigator.clipboard.writeText('${text}').then(() => {
window.toast.success('Link copied to clipboard!')
}).catch(err => {
window.toast.error('Failed to copy link to clipboard!')
});
`
}