import { svgs, cssStyle, defaultFavicon } from './static'; import { SiteConfig } from './types'; export var renderTemplFull = (files: R2Object[], folders: string[], path: string, config: SiteConfig) => { return ` ${renderTemplTitle(config.name, path)} ${cssStyle} ${svgs}

${config.name} / ${renderTemplBreadcrumbs(path)}

${(path==='/')?'':renderGoUp(path)} ${renderTemplFolders(folders, config)} ${renderTemplFiles(files, config)}
Name Description Size Modified
`; } var renderGoUp = (path: string) => { if(path !== "") { return ` Go up — — — `; } return ''; } var renderTemplTitle = (siteTitle:string, path: string) => { if(path === "/") { return siteTitle } path = path.slice(0, -1); return `${siteTitle} | ${cleanTitle(path)}`; } var cleanTitle = (path: string) => { var parts = path.split("/") // remove the empty strings parts = parts.filter((part) => part !== "") return parts[parts.length-1] } var renderTemplBreadcrumbs = (path: string) => { const parts = path.split('/'); var output = ''; var currentPath = '/'; for (var i = 0; i < parts.length; i++) { if (parts[i] === '') continue; currentPath += parts[i] + '/'; output += `${parts[i]} / `; } console.log(output); return output; } var renderTemplFolders = (folders: string[], siteConfig: SiteConfig) => { if(typeof folders === 'undefined') return ''; var output = ''; for (var i = 0; i < folders.length; i++) { output += ` ${cleanFolderName(folders[i])} ${findDesp(siteConfig, '/'+folders[i].slice(0, -1), true)??"—"} — — `; } return output; } var renderTemplFiles = (files: R2Object[], siteConfig: SiteConfig) => { if(typeof files === 'undefined') return ''; var output = ''; for (var i = 0; i < files.length; i++) { output += ` ${cleanFileName(files[i].key)} ${findDesp(siteConfig, '/'+files[i].key, true)??"—"} ${humanFileSize(files[i].size)} `; } return output; } var cleanFileName = (name: string) => { return name.split("/").slice(-1).pop()! } var cleanFolderName = (name: string) => { return name.slice(0, -1).split("/").slice(-1).pop()! } // taken from https://stackoverflow.com/questions/10420352/converting-file-size-in-bytes-to-human-readable-string var humanFileSize = (bytes: number, si=false, dp=1) => { const thresh = si ? 1000 : 1024; if (Math.abs(bytes) < thresh) { return bytes + ' B'; } const units = si ? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] : ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']; let u = -1; const r = 10**dp; do { bytes /= thresh; ++u; } while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1); return bytes.toFixed(dp) + ' ' + units[u]; } function findDesp(siteConfig: SiteConfig, path: string, exact: boolean): string | undefined { if (exact) { return siteConfig.desp[path]; } const keys = Object.keys(siteConfig.desp); // find the longest match let longestMatch = '/'; for (const key of keys) { if (path.startsWith(key) && key.length > longestMatch.length) { longestMatch = key; } } const desp = siteConfig.desp[longestMatch]; return desp; } function generateFooter(siteConfig: SiteConfig, path: string): string { /// Footer includes: /// - desp of current path, use the most specific entry /// - legal info, if any /// - reference to gitea download site code, as the inspiration of this project let contents:string[] = []; const desp = findDesp(siteConfig, path, false); if (desp) { contents.push(`

${desp}

`); } if (siteConfig.legalInfo) { contents.push(`

${siteConfig.legalInfo}

`); } if (siteConfig.showPoweredBy) { contents.push(`

Powered by r2-dir-list

`); } return contents.join(''); }