📦 EqualifyEverything / equalify-hub

📄 html.ts · 143 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143import { getCurrentUser } from '#src/utils/auth';

// Site configuration - customize these for your project
export const site = {
    name: 'Equalify Open Source',
    tagline: 'Developer tools for the EqualifyEverything organization',
    favicon: 'https://app.equalify.uic.edu/favicon.ico',
    logo: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200" width="20" height="20" style="margin-top:1px;"><circle cx="100" cy="100" r="65" fill="none" stroke="#e6edf3" stroke-width="28"/><line x1="100" y1="125" x2="100" y2="90" stroke="#e6edf3" stroke-width="8" stroke-linecap="round"/><line x1="100" y1="90" x2="65" y2="50" stroke="#e6edf3" stroke-width="8" stroke-linecap="round"/><line x1="100" y1="90" x2="120" y2="112" stroke="#e6edf3" stroke-width="8" stroke-linecap="round"/><circle cx="100" cy="125" r="10" fill="#e6edf3"/><circle cx="100" cy="90" r="10" fill="#e6edf3"/><circle cx="120" cy="112" r="10" fill="#e6edf3"/></svg>`,
};

// Navigation links - customize for your project
export const navLinks: { href: string; label: string }[] = [];

// Base CSS styles - dark theme
export const baseStyles = `
* { box-sizing: border-box; }
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
    line-height: 1.6;
    margin: 0;
    padding: 0;
    background: #0d1117;
    color: #e6edf3;
}
a { color: #58a6ff; text-decoration: none; }
a:hover { text-decoration: underline; }
nav {
    background: #161b22;
    border-bottom: 1px solid #30363d;
    padding: 12px 16px;
    display: flex;
    align-items: center;
    gap: 16px;
    flex-wrap: wrap;
}
nav .logo { font-size: 18px; font-weight: 600; color: #e6edf3; text-decoration: none; }
nav a { color: #8b949e; text-decoration: none; font-size: 14px; white-space: nowrap; }
nav a:hover { color: #e6edf3; text-decoration: none; }
footer {
    text-align: center;
    padding: 24px 16px;
    color: #8b949e;
    font-size: 12px;
    border-top: 1px solid #30363d;
}
@media (min-width: 600px) {
    nav { gap: 24px; }
}
`;

// Generate auth section based on current user
export function getAuthSection(user = getCurrentUser()) {
    if (!user) {
        return `<a href="/github" style="margin-left:auto;">Sign in</a>`;
    }
    
    return `<div style="display:flex;align-items:center;gap:12px;margin-left:auto;">
        <a href="/${user.login}" style="display:flex;align-items:center;gap:8px;">
            <img src="${user.avatar_url}" alt="${user.login}" style="width:20px;height:20px;border-radius:50%;">
            ${user.login}
        </a>
        <a href="/logout">Sign out</a>
    </div>`;
}

// Generate navigation bar
export function nav(user = getCurrentUser()) {
    const links = navLinks.map(l => `<a href="${l.href}">${l.label}</a>`).join('\n        ');
    return `<nav>
        <a href="/" class="logo" style="display:flex;align-items:center;gap:8px;">
            <img src="https://github.com/EqualifyEverything.png" alt="EqualifyEverything" style="width:24px;height:24px;border-radius:4px;">
            ${site.name}
        </a>
        ${links}
        ${getAuthSection(user)}
    </nav>`;
}

// Generate footer
export function footer(links: { href: string; label: string }[] = [
    { href: '/about', label: 'About' },
    { href: 'https://github.com/EqualifyEverything', label: 'GitHub' },
    { href: 'https://app.equalify.uic.edu', label: 'Equalify' }
]) {
    const linkHtml = links.map(l => `<a href="${l.href}">${l.label}</a>`).join(' · ');
    return `<footer>
        ${linkHtml}
    </footer>`;
}

// Generate full HTML document
export function html(options: {
    title: string;
    body: string;
    styles?: string;
    user?: ReturnType<typeof getCurrentUser>;
}) {
    const user = options.user ?? getCurrentUser();
    return `<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>${options.title} – ${site.name}</title>
    <link rel="icon" href="${site.favicon}">
    <style>
        ${baseStyles}
        ${options.styles || ''}
    </style>
</head>
<body>
    ${nav(user)}
    ${options.body}
    ${footer()}
</body>
</html>`;
}

// Common response helper
export function htmlResponse(options: {
    title: string;
    body: string;
    styles?: string;
    user?: ReturnType<typeof getCurrentUser>;
    statusCode?: number;
}) {
    return {
        statusCode: options.statusCode || 200,
        headers: { 'Content-Type': 'text/html' },
        body: html(options)
    };
}

// Escape HTML to prevent XSS
export function escapeHtml(str: string): string {
    return str
        .replace(/&/g, '&amp;')
        .replace(/</g, '&lt;')
        .replace(/>/g, '&gt;')
        .replace(/"/g, '&quot;')
        .replace(/'/g, '&#039;');
}