OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-note / lib / page-layout.js

196 lines · javascript ref:
/**
 * Page Layout — shared HTML wrapper for all server-rendered pages
 *
 * Provides DOCTYPE, head, body, nav, identity script, and shared CSS.
 * Each page passes title, content, and optional page-specific CSS/scripts.
 *
 * Usage:
 *   import { renderPage } from './page-layout.js'
 *   return renderPage('My Notes', content, { pathPrefix, activePage: 'home', css: pageCSS })
 */

import { renderNav } from './nav-ui.js'
import { renderIdentityScript } from './identity-ui.js'

/**
 * Escape text for use in HTML attributes
 */
function escapeForAttr (text) {
  return (text || '').replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;')
}

/**
 * Render a full HTML page
 *
 * @param {string} title — page title (appended with " — Hypernote")
 * @param {string} content — page body HTML
 * @param {Object} opts
 * @param {string} opts.pathPrefix — URL prefix (e.g., '/hypernote')
 * @param {string} opts.activePage — active nav tab (e.g., 'home', 'profile', 'admin')
 * @param {string} opts.css — page-specific CSS (inserted after shared styles)
 * @param {string} opts.scripts — page-specific inline JS (inserted before </body>)
 * @param {string} opts.head — extra <head> content (stylesheets, meta tags)
 * @param {number} opts.maxWidth — container max-width in px (default: 900)
 * @returns {string} full HTML document
 */
export function renderPage (title, content, opts = {}) {
  const pathPrefix = opts.pathPrefix || ''
  const activePage = opts.activePage || ''
  const pageCss = opts.css || ''
  const pageScripts = opts.scripts || ''
  const headExtra = opts.head || ''
  const maxWidth = opts.maxWidth || 900

  return `<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>${escapeForAttr(title)} — Hypernote</title>
  <script>document.documentElement.setAttribute('data-theme', localStorage.getItem('hn-theme') || 'dark')</script>
  ${headExtra}
  <style>
    /* Theme definitions — must match web/css/app.css */
    [data-theme="dark"], :root {
      --bg: #1e1e1e; --bg-secondary: #252525; --bg-sidebar: #1a1a1a;
      --bg-card: #252525; --bg-hover: rgba(255,255,255,0.05);
      --text: #d4d4d4; --text-muted: #808080;
      --accent: #569cd6; --accent-hover: #7ab8e8;
      --border: #3c3c3c; --green: #4ec9b0;
      --code-bg: #1a1a1a; --input-bg: #2d2d2d;
      --shadow: rgba(0,0,0,0.4);
    }
    [data-theme="dark-blue"] {
      --bg: #1a1a2e; --bg-secondary: #16213e; --bg-sidebar: #0f3460;
      --bg-card: #1a1a2e; --bg-hover: rgba(255,255,255,0.05);
      --text: #e4e4e4; --text-muted: #8b8b9e;
      --accent: #e94560; --accent-hover: #ff6b81;
      --border: #2a2a4a; --green: #2ecc71;
      --code-bg: #0d1117; --input-bg: #16213e;
      --shadow: rgba(0,0,0,0.3);
    }
    [data-theme="light"] {
      --bg: #ffffff; --bg-secondary: #f5f5f5; --bg-sidebar: #eaeaea;
      --bg-card: #ffffff; --bg-hover: rgba(0,0,0,0.04);
      --text: #1e1e1e; --text-muted: #6b7280;
      --accent: #2563eb; --accent-hover: #1d4ed8;
      --border: #d1d5db; --green: #16a34a;
      --code-bg: #f3f4f6; --input-bg: #ffffff;
      --shadow: rgba(0,0,0,0.1);
    }
    * { margin: 0; padding: 0; box-sizing: border-box; }
    body {
      font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
      background: var(--bg);
      color: var(--text);
      padding-top: 56px;
    }
    a { color: var(--accent); text-decoration: none; }
    a:hover { text-decoration: underline; }
    .container { max-width: ${maxWidth}px; margin: 0 auto; padding: 20px; }

    /* Typography */
    h1 { font-size: 1.75rem; margin-bottom: 8px; color: var(--text); }
    h2 { font-size: 1.4rem; margin: 24px 0 8px; color: var(--text); }
    h3 { font-size: 1.15rem; margin: 16px 0 6px; color: var(--text); }
    .meta { color: var(--text-muted); font-size: 0.85rem; margin-bottom: 16px; }

    /* Cards */
    .card {
      display: block; padding: 14px 18px; margin: 8px 0;
      background: var(--bg-secondary); border: 1px solid var(--border);
      border-radius: 6px; text-decoration: none; color: inherit;
      transition: border-color 0.15s;
    }
    .card:hover { border-color: var(--accent); text-decoration: none; }
    .card h3 { margin: 0 0 4px; font-size: 1rem; }
    .card .desc { font-size: 0.85rem; color: var(--text-muted); }

    /* Tags */
    .tags { margin: 8px 0; display: flex; flex-wrap: wrap; gap: 4px; }
    .tag {
      display: inline-block; padding: 2px 10px; font-size: 0.75rem;
      background: var(--bg-card); border-radius: 12px; color: var(--text-muted);
    }

    /* Badges */
    .badge {
      display: inline-block; padding: 3px 10px; border-radius: 12px;
      font-size: 0.75rem; font-weight: 500;
    }
    .badge-success { background: var(--success); color: #fff; }
    .badge-secondary { background: var(--bg-card); color: var(--text-muted); }

    /* Buttons */
    .btn {
      display: inline-block; padding: 6px 16px; border-radius: 6px;
      font-size: 0.85rem; font-weight: 500; cursor: pointer;
      border: 1px solid var(--border); background: var(--bg-secondary);
      color: var(--text); text-decoration: none;
      transition: background 0.15s;
    }
    .btn:hover { background: var(--bg-hover); text-decoration: none; }
    .btn-primary { background: var(--accent); color: #fff; border-color: var(--accent); }
    .btn-primary:hover { background: var(--accent-hover); }
    .btn-danger { background: var(--danger); color: #fff; border-color: var(--danger); }

    /* Forms */
    input[type="text"], input[type="search"], textarea, select {
      background: var(--bg); color: var(--text);
      border: 1px solid var(--border); border-radius: 6px;
      padding: 8px 12px; font-size: 0.9rem; width: 100%;
    }
    input:focus, textarea:focus, select:focus {
      outline: none; border-color: var(--accent);
    }

    /* Tables */
    table { width: 100%; border-collapse: collapse; margin: 16px 0; }
    th, td { padding: 8px 12px; text-align: left; border-bottom: 1px solid var(--border); }
    th { color: var(--text-muted); font-size: 0.85rem; font-weight: 500; }

    /* Markdown body */
    .markdown-body { line-height: 1.7; }
    .markdown-body h1, .markdown-body h2, .markdown-body h3 { margin-top: 24px; }
    .markdown-body p { margin: 12px 0; }
    .markdown-body img { max-width: 100%; border-radius: 6px; }
    .markdown-body pre { background: var(--bg-secondary); padding: 16px; border-radius: 6px; overflow-x: auto; margin: 16px 0; }
    .markdown-body code { font-family: 'SF Mono', 'Fira Code', monospace; font-size: 0.9em; }
    .markdown-body blockquote { border-left: 3px solid var(--border); padding-left: 16px; color: var(--text-muted); margin: 12px 0; }
    .markdown-body ul, .markdown-body ol { padding-left: 24px; margin: 8px 0; }
    .markdown-body li { margin: 4px 0; }
    .markdown-body hr { border: none; border-top: 1px solid var(--border); margin: 24px 0; }
    .markdown-body a { color: var(--accent); }
    .markdown-body .wikilink { color: var(--accent); font-weight: 500; }

    /* Gallery */
    .gallery-masonry {
      display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
      gap: 8px; margin: 16px 0;
    }
    .gallery-item { cursor: pointer; border-radius: 6px; overflow: hidden; }
    .gallery-item img { width: 100%; display: block; }
    .gallery-caption { font-size: 0.8rem; padding: 4px 8px; color: var(--text-muted); background: var(--bg-secondary); }

    /* Back links */
    .back-link { display: inline-block; margin-bottom: 16px; font-size: 0.9rem; }

    /* Status messages */
    .alert { padding: 12px 16px; border-radius: 6px; margin: 16px 0; }
    .alert-info { background: rgba(88, 166, 255, 0.1); border: 1px solid rgba(88, 166, 255, 0.3); }
    .alert-error { background: rgba(218, 54, 51, 0.1); border: 1px solid rgba(218, 54, 51, 0.3); }

    ${pageCss}
  </style>
</head>
<body>
  ${renderIdentityScript(pathPrefix)}
  ${renderNav(pathPrefix, activePage)}
  <div class="container">
    ${content}
  </div>
  ${pageScripts ? `<script>\n${pageScripts}\n</script>` : ''}
</body>
</html>`
}