anbot

f432b40 / webui.mjs

8405 bytes

// Plain HTML rendering for the served repository and feed. No Node-only imports:
// these functions take plain data and return HTML strings, so they can run
// in a browser too. No JavaScript is emitted. All dynamic content is escaped.

const style = `body { font-family: sans-serif; margin: 2em; max-width: 64em; }
code { font-family: monospace; word-break: break-all; }
pre { font-family: monospace; overflow-x: auto; }
table { border-collapse: collapse; }
th, td { border: 1px solid #999; padding: 0.2em 0.5em; text-align: left; vertical-align: top; }`

export function escapeHtml(value) {
  return String(value)
    .replaceAll('&', '&')
    .replaceAll('<', '&lt;')
    .replaceAll('>', '&gt;')
    .replaceAll('"', '&quot;')
    .replaceAll("'", '&#39;')
}

function encodePath(path) {
  return path.split('/').map(segment => encodeURIComponent(segment)).join('/')
}

function shortHash(hash) {
  return hash.slice(0, 7)
}

function page(title, body) {
  return `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>${escapeHtml(title)}</title>
<style>
${style}
</style>
</head>
<body>
${body}
<hr>
<p><a href="/git">Git</a> · <a href="/">Social feed</a></p>
</body>
</html>
`
}

function statusTable(rows) {
  const body = rows.map(([label, value]) =>
    `<tr><th>${escapeHtml(label)}</th><td>${value}</td></tr>`).join('\n')
  return `<table>\n${body}\n</table>`
}

function breadcrumbs(path, lastIsFile) {
  const segments = path ? path.split('/') : []
  const parts = ['<a href="/tree">root</a>']
  let prefix = ''
  segments.forEach((segment, index) => {
    prefix += (prefix ? '/' : '') + segment
    if (lastIsFile && index === segments.length - 1) parts.push(escapeHtml(segment))
    else parts.push(`<a href="/tree/${encodePath(prefix)}">${escapeHtml(segment)}</a>`)
  })
  return `<p>${parts.join(' / ')}</p>`
}

function entriesTable(entries, basePath) {
  const rows = entries.map(entry => {
    const child = basePath ? `${basePath}/${entry.name}` : entry.name
    if (entry.type === 'tree') {
      return `<tr><td><a href="/tree/${encodePath(child)}">${escapeHtml(entry.name)}/</a></td><td>directory</td><td>-</td></tr>`
    }
    if (entry.type === 'commit') {
      return `<tr><td>${escapeHtml(entry.name)}</td><td>submodule</td><td>-</td></tr>`
    }
    const kind = entry.mode === '120000' ? 'symlink' : 'file'
    const size = entry.size === null || entry.size === undefined ? '-' : String(entry.size)
    return `<tr><td><a href="/blob/${encodePath(child)}">${escapeHtml(entry.name)}</a></td><td>${kind}</td><td>${size}</td></tr>`
  }).join('\n')
  return `<table>
<tr><th>Name</th><th>Type</th><th>Size</th></tr>
${rows}
</table>`
}

function commitsTable(commits) {
  const rows = commits.map(commit =>
    `<tr><td><a href="/commit/${commit.hash}"><code>${shortHash(commit.hash)}</code></a></td><td>${escapeHtml(commit.author)}</td><td>${escapeHtml(commit.date)}</td><td>${escapeHtml(commit.subject)}</td></tr>`).join('\n')
  return `<table>
<tr><th>Commit</th><th>Author</th><th>Date</th><th>Message</th></tr>
${rows}
</table>`
}

export function indexPage({ name, cloneUrl, branch, repoId, updateId, commits, entries }) {
  const head = commits[0]
  const body = `<h1>${escapeHtml(name)}</h1>
<p><a href="/">Social feed</a></p>
${statusTable([
    ['Branch', escapeHtml(branch)],
    ['HEAD', head ? `<a href="/commit/${head.hash}"><code>${shortHash(head.hash)}</code></a> ${escapeHtml(head.subject)}` : 'none'],
    ['Clone URL', `<code>${escapeHtml(cloneUrl)}</code>`],
    ['Repository ID', `<code>${escapeHtml(repoId)}</code>`],
    ['Selected update', `<code>${escapeHtml(updateId)}</code>`]
  ])}
<h2>Commits</h2>
${commitsTable(commits)}
<h2>Files</h2>
${entriesTable(entries, '')}`
  return page(name, body)
}

function feedPost({ id, author, timestamp, text, replyTo, ref, commit, count, commits, updateId }) {
  const date = new Date(timestamp).toISOString().slice(0, 16).replace('T', ' ') + ' UTC'
  const replyLink = replyTo ? `<br><a href="?post=${encodeURIComponent(replyTo)}">In reply to ${escapeHtml(shortHash(replyTo))}</a>` : ''
  const content = updateId ? `<p>Pushed ${count} ${count === 1 ? 'commit' : 'commits'} to <code>${escapeHtml(ref.slice('refs/heads/'.length))}</code>
at <a href="/commit/${escapeHtml(commit)}"><code>${escapeHtml(shortHash(commit))}</code></a>.
<a href="?update=${encodeURIComponent(updateId)}">Git update ${escapeHtml(shortHash(updateId))}</a></p>
<ul>${commits.map(item => `<li><a href="/commit/${escapeHtml(item.hash)}"><code>${escapeHtml(shortHash(item.hash))}</code></a> ${escapeHtml(item.subject)}</li>`).join('')}</ul>
${count > commits.length ? `<p>Showing ${commits.length} of ${count} commits.</p>` : ''}`
    : `<p>${escapeHtml(text).replace(/\r\n?|\n/g, '<br>\n')}</p>`
  return `<p><a href="?author=${encodeURIComponent(author)}"><code>${escapeHtml(shortHash(author))}</code></a>
<a href="?post=${encodeURIComponent(id)}">${date}</a>${replyLink}</p>
${content}`
}

export function feedPage({ posts, author, post, missing }) {
  const title = missing ? 'Post or author not found' : post ? 'Post' : author ? 'Author feed' : 'ANBot feed'
  const back = missing || author || post ? ' · <a href="/">All posts</a>' : ''
  const heading = author ? `<p><code>${escapeHtml(author)}</code></p>\n` : ''
  const thread = post ? `${feedPost(post)}\n<h2>Replies</h2>\n` : ''
  const entries = missing ? '' : posts.map(feedPost).join('\n<hr>\n') ||
    (post ? '<p>No replies yet.</p>' : '<p>No posts yet.</p>')
  return `<!DOCTYPE html>
<html>
<head><title>${title}</title></head>
<body>
<h1>${title}</h1>
<nav aria-label="ANBot"><a href="/git">Git</a>${back}</nav>
${heading}${thread}<div id="feed-entries">${entries}</div>
${missing ? '' : '<script src="./feed-live.js" defer></script>'}
</body>
</html>
`
}

export function treePage({ name, path, entries }) {
  const body = `<h1>${escapeHtml(name)}</h1>
${breadcrumbs(path, false)}
${entriesTable(entries, path)}`
  return page(`${path || '/'} - ${name}`, body)
}

export function blobPage({ name, path, size, text, binary, truncated, shownBytes, tooLarge, commit }) {
  let content
  if (tooLarge) {
    content = `<p>File is too large to show (${size} bytes).</p>`
  } else if (binary) {
    content = `<p>Binary file (${size} bytes). Contents are not shown.</p>`
  } else {
    content = `<pre>${escapeHtml(text)}</pre>`
    if (truncated) content += `<p>Showing the first ${shownBytes} bytes of ${size}.</p>`
  }
  const context = commit
    ? `<p><a href="/commit/${commit.hash}"><code>${shortHash(commit.hash)}</code></a> / ${escapeHtml(path)}</p>`
    : breadcrumbs(path, true)
  const body = `<h1>${escapeHtml(name)}</h1>
${context}
<p>${size} bytes</p>
${content}`
  return page(`${path} - ${name}`, body)
}

export function commitPage({ name, commit, files }) {
  const rows = files.map(file => {
    const count = value => value === null ? '-' : String(value)
    return `<tr><td><a href="/commit/${commit.hash}/diff/${encodePath(file.path)}">${escapeHtml(file.path)}</a></td><td>${count(file.added)}</td><td>${count(file.removed)}</td></tr>`
  }).join('\n')
  const changed = files.length ? `<table>
<tr><th>File</th><th>Added</th><th>Removed</th></tr>
${rows}
</table>` : '<p>No files changed.</p>'
  const body = `<h1>${escapeHtml(name)}</h1>
${statusTable([
    ['Commit', `<code>${escapeHtml(commit.hash)}</code>`],
    ['Author', escapeHtml(`${commit.author} <${commit.email}>`)],
    ['Date', escapeHtml(commit.date)]
  ])}
<h2>Message</h2>
<pre>${escapeHtml(commit.message)}</pre>
<h2>Changed files</h2>
${changed}`
  return page(`${shortHash(commit.hash)} - ${name}`, body)
}

export function diffPage({ name, commit, path, text, truncated, shownBytes, hasBlob }) {
  const blobLink = hasBlob
    ? `<p><a href="/commit/${commit.hash}/blob/${encodePath(path)}">View file at this commit</a></p>\n`
    : ''
  const body = `<h1>${escapeHtml(name)}</h1>
<p><a href="/commit/${commit.hash}"><code>${shortHash(commit.hash)}</code></a> / ${escapeHtml(path)}</p>
${blobLink}<pre>${escapeHtml(text)}</pre>
${truncated ? `<p>Showing the first ${shownBytes} bytes of the diff.</p>` : ''}`
  return page(`${path} - ${shortHash(commit.hash)} - ${name}`, body)
}

export function notFoundPage(name, what) {
  const body = `<h1>Not found</h1>
<p>${escapeHtml(what)} does not exist in this repository.</p>`
  return page(`Not found - ${name}`, body)
}

Git · Social feed