/**
* Minimal DOM helpers. No framework, no build step: the dashboard ships the
* files it serves (M16-T11).
*/
/**
* `h('div', { class: 'card' }, h('h2', { text: 'Titre' }))`
* Text always goes through `textContent`, so API values can never inject markup.
*/
export function h(tag, props = {}, ...children) {
const node = document.createElement(tag);
for (const [key, value] of Object.entries(props ?? {})) {
if (value === null || value === undefined || value === false) continue;
if (key === 'text') node.textContent = String(value);
else if (key === 'class') node.className = value;
else if (key === 'dataset') Object.assign(node.dataset, value);
else if (key === 'style') Object.assign(node.style, value);
else if (key.startsWith('on') && typeof value === 'function') {
node.addEventListener(key.slice(2).toLowerCase(), value);
} else if (key === 'value' || key === 'checked' || key === 'disabled' || key === 'selected') {
node[key] = value;
} else node.setAttribute(key, value === true ? '' : String(value));
}
for (const child of children.flat(Infinity)) {
if (child === null || child === undefined || child === false) continue;
node.append(child instanceof Node ? child : document.createTextNode(String(child)));
}
return node;
}
export const clear = (node) => {
while (node.firstChild) node.firstChild.remove();
return node;
};
export const mount = (node, ...children) => {
clear(node);
node.append(...children.flat(Infinity).filter(Boolean));
return node;
};
export const qs = (selector, scope = document) => scope.querySelector(selector);
/** Escapes then applies the small subset of Markdown used by Gitea bodies. */
export function markdown(text) {
const escaped = String(text ?? '')
.replaceAll('&', '&')
.replaceAll('<', '<')
.replaceAll('>', '>');
return escaped
.replace(/```([\s\S]*?)```/g, (_, code) => `
${code.trim()}
`)
.replace(/`([^`\n]+)`/g, '$1')
.replace(/^### (.+)$/gm, '$1
')
.replace(/^## (.+)$/gm, '$1
')
.replace(/^# (.+)$/gm, '$1
')
.replace(/\*\*([^*\n]+)\*\*/g, '$1')
.replace(/^[-*] (.+)$/gm, '$1')
.replace(/([\s\S]*?<\/li>)/g, '')
.replace(/\n{2,}/g, '
');
}
/** Renders trusted Markdown built above; never call it with raw HTML. */
export function richText(text, className = 'rich') {
const node = h('div', { class: className });
node.innerHTML = markdown(text);
return node;
}
export const formatDate = (value) => {
if (!value) return '—';
const date = new Date(String(value).includes('T') ? value : `${value}Z`);
if (Number.isNaN(date.getTime())) return String(value);
return date.toLocaleString('fr-FR', { dateStyle: 'medium', timeStyle: 'short' });
};
export function formatRelative(value) {
if (!value) return '—';
const date = new Date(String(value).includes('T') ? value : `${value}Z`);
if (Number.isNaN(date.getTime())) return String(value);
const seconds = Math.round((date.getTime() - Date.now()) / 1000);
const units = [
['year', 31_536_000],
['month', 2_592_000],
['day', 86_400],
['hour', 3600],
['minute', 60],
];
const formatter = new Intl.RelativeTimeFormat('fr', { numeric: 'auto' });
for (const [unit, size] of units) {
if (Math.abs(seconds) >= size) return formatter.format(Math.round(seconds / size), unit);
}
return formatter.format(seconds, 'second');
}
export const formatBytes = (bytes) => {
const value = Number(bytes) || 0;
const units = ['o', 'Ko', 'Mo', 'Go'];
let index = 0;
let result = value;
while (result >= 1024 && index < units.length - 1) {
result /= 1024;
index += 1;
}
return `${result.toFixed(index === 0 ? 0 : 1)} ${units[index]}`;
};
export const formatDuration = (ms) => {
const seconds = Math.floor(Number(ms) / 1000);
const days = Math.floor(seconds / 86_400);
const hours = Math.floor((seconds % 86_400) / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
if (days > 0) return `${days} j ${hours} h`;
if (hours > 0) return `${hours} h ${minutes} min`;
if (minutes > 0) return `${minutes} min`;
return `${seconds} s`;
};