embed des liens dans depeches
This commit is contained in:
parent
2ede958e97
commit
844eea61e9
|
|
@ -90,39 +90,63 @@ function chargerAlbums() {
|
|||
}
|
||||
|
||||
/* === FEED DEPECHES (depeches.html) === */
|
||||
function chargerDepeches() {
|
||||
fetch('/api/depeches')
|
||||
.then(r => r.json())
|
||||
.then(depeches => {
|
||||
const feed = document.getElementById('feed');
|
||||
feed.innerHTML = '';
|
||||
async function chargerDepeches() {
|
||||
const feed = document.getElementById('feed');
|
||||
|
||||
if (depeches.length === 0) {
|
||||
feed.innerHTML = '<p class="chargement">aucune dépêche pour l\'instant.</p>';
|
||||
return;
|
||||
async function parseTexte(texte) {
|
||||
const urlRegex = /(https?:\/\/[^\s]+)/g;
|
||||
const urls = texte.match(urlRegex) || [];
|
||||
let html = texte.replace(urlRegex, '___URL___');
|
||||
|
||||
const embeds = await Promise.all(urls.map(async url => {
|
||||
if (url.includes('soundcloud.com')) {
|
||||
try {
|
||||
const r = await fetch(`https://soundcloud.com/oembed?format=json&url=${encodeURIComponent(url)}&maxwidth=500`);
|
||||
const data = await r.json();
|
||||
return data.html;
|
||||
} catch { return `<a href="${url}" target="_blank">${url}</a>`; }
|
||||
}
|
||||
if (url.includes('youtube.com') || url.includes('youtu.be')) {
|
||||
const id = url.match(/(?:v=|youtu\.be\/)([^&\s]+)/)?.[1];
|
||||
if (id) return `<iframe width="500" height="281" src="https://www.youtube.com/embed/${id}" frameborder="0" allowfullscreen style="max-width:100%;display:block;margin-top:0.8rem;"></iframe>`;
|
||||
}
|
||||
return `<a href="${url}" target="_blank">${url}</a>`;
|
||||
}));
|
||||
|
||||
depeches.forEach(d => {
|
||||
const el = document.createElement('div');
|
||||
el.className = 'depeche';
|
||||
embeds.forEach(e => { html = html.replace('___URL___', e); });
|
||||
return html;
|
||||
}
|
||||
|
||||
const img = d.image
|
||||
? `<img src="${d.image}" class="depeche-image" alt="">`
|
||||
: '';
|
||||
try {
|
||||
const r = await fetch('/api/depeches');
|
||||
const depeches = await r.json();
|
||||
feed.innerHTML = '';
|
||||
|
||||
el.innerHTML = `
|
||||
<div class="depeche-date">ewen - ${d.date}</div>
|
||||
<div class="depeche-texte">${d.texte}</div>
|
||||
${img}
|
||||
`;
|
||||
if (depeches.length === 0) {
|
||||
feed.innerHTML = '<p class="chargement">aucune dépêche pour l\'instant.</p>';
|
||||
return;
|
||||
}
|
||||
|
||||
feed.appendChild(el);
|
||||
});
|
||||
})
|
||||
.catch(() => {
|
||||
document.getElementById('feed').innerHTML =
|
||||
'<p class="chargement">impossible de charger les dépêches.</p>';
|
||||
});
|
||||
for (const d of depeches) {
|
||||
const el = document.createElement('div');
|
||||
el.className = 'depeche';
|
||||
|
||||
const texteHtml = await parseTexte(d.texte);
|
||||
const img = d.image
|
||||
? `<img src="${d.image}" class="depeche-image" alt="">`
|
||||
: '';
|
||||
|
||||
el.innerHTML = `
|
||||
<div class="depeche-date">ewen - ${d.date}</div>
|
||||
<div class="depeche-texte">${texteHtml}</div>
|
||||
${img}
|
||||
`;
|
||||
|
||||
feed.appendChild(el);
|
||||
}
|
||||
} catch {
|
||||
feed.innerHTML = '<p class="chargement">impossible de charger les dépêches.</p>';
|
||||
}
|
||||
}
|
||||
|
||||
/* === INIT === */
|
||||
|
|
|
|||
Loading…
Reference in a new issue