embed des liens dans depeches
This commit is contained in:
parent
2ede958e97
commit
844eea61e9
|
|
@ -90,39 +90,63 @@ function chargerAlbums() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* === FEED DEPECHES (depeches.html) === */
|
/* === FEED DEPECHES (depeches.html) === */
|
||||||
function chargerDepeches() {
|
async function chargerDepeches() {
|
||||||
fetch('/api/depeches')
|
const feed = document.getElementById('feed');
|
||||||
.then(r => r.json())
|
|
||||||
.then(depeches => {
|
|
||||||
const feed = document.getElementById('feed');
|
|
||||||
feed.innerHTML = '';
|
|
||||||
|
|
||||||
if (depeches.length === 0) {
|
async function parseTexte(texte) {
|
||||||
feed.innerHTML = '<p class="chargement">aucune dépêche pour l\'instant.</p>';
|
const urlRegex = /(https?:\/\/[^\s]+)/g;
|
||||||
return;
|
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 => {
|
embeds.forEach(e => { html = html.replace('___URL___', e); });
|
||||||
const el = document.createElement('div');
|
return html;
|
||||||
el.className = 'depeche';
|
}
|
||||||
|
|
||||||
const img = d.image
|
try {
|
||||||
? `<img src="${d.image}" class="depeche-image" alt="">`
|
const r = await fetch('/api/depeches');
|
||||||
: '';
|
const depeches = await r.json();
|
||||||
|
feed.innerHTML = '';
|
||||||
|
|
||||||
el.innerHTML = `
|
if (depeches.length === 0) {
|
||||||
<div class="depeche-date">ewen - ${d.date}</div>
|
feed.innerHTML = '<p class="chargement">aucune dépêche pour l\'instant.</p>';
|
||||||
<div class="depeche-texte">${d.texte}</div>
|
return;
|
||||||
${img}
|
}
|
||||||
`;
|
|
||||||
|
|
||||||
feed.appendChild(el);
|
for (const d of depeches) {
|
||||||
});
|
const el = document.createElement('div');
|
||||||
})
|
el.className = 'depeche';
|
||||||
.catch(() => {
|
|
||||||
document.getElementById('feed').innerHTML =
|
const texteHtml = await parseTexte(d.texte);
|
||||||
'<p class="chargement">impossible de charger les dépêches.</p>';
|
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 === */
|
/* === INIT === */
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue