-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathposts.html
More file actions
467 lines (401 loc) · 15.3 KB
/
posts.html
File metadata and controls
467 lines (401 loc) · 15.3 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Content-Security-Policy"
content="script-src 'self' 'unsafe-inline' 'unsafe-eval' https://cdn.jsdelivr.net https://cdn.skypack.dev https://esm.sh chrome-extension:; connect-src 'self' https://api.github.com https://admin.blog.farteam.com.ar/.netlify/functions/">
<title>Posts - Devlog Admin</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Google+Sans:wght@400;500;600&display=swap" rel="stylesheet">
<link rel="stylesheet" href="style.css">
<style>
* {
box-sizing: border-box;
}
body {
font-family: 'Google Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
margin: 0;
padding: 0;
}
.main-header {
background: var(--surface);
border-bottom: 1px solid var(--border);
padding: 12px 24px;
display: flex;
justify-content: space-between;
align-items: center;
position: sticky;
top: 0;
z-index: 1000;
}
.logo {
font-size: 22px;
font-weight: 500;
color: var(--primary);
text-decoration: none;
}
.header-actions {
display: flex;
gap: 12px;
align-items: center;
}
.btn-blogger {
background: var(--primary);
color: white;
border: none;
padding: 8px 16px;
border-radius: 4px;
font-weight: 500;
font-size: 14px;
cursor: pointer;
transition: background 0.2s;
text-decoration: none;
display: inline-block;
}
.btn-blogger:hover {
background: var(--primary-700);
color: white;
}
.container-blogger {
max-width: 1200px;
margin: 0 auto;
padding: 24px;
}
.posts-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 24px;
}
.posts-title {
font-size: 28px;
font-weight: 400;
color: var(--text);
margin: 0;
}
.posts-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
gap: 16px;
}
.post-card {
background: var(--surface);
border-radius: 8px;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
padding: 16px;
cursor: pointer;
transition: box-shadow 0.2s;
border: 1px solid transparent;
}
.post-card:hover {
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
border-color: var(--border);
}
.post-title {
font-size: 16px;
font-weight: 500;
color: var(--text);
margin: 0 0 8px 0;
line-height: 1.4;
}
.post-meta {
font-size: 14px;
color: var(--muted);
margin-bottom: 8px;
}
.post-excerpt {
font-size: 14px;
color: var(--muted);
line-height: 1.4;
margin: 0;
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
line-clamp: 3;
overflow: hidden;
}
.post-tags {
display: flex;
gap: 4px;
margin-top: 8px;
flex-wrap: wrap;
}
.post-tag {
background: var(--chip-bg);
color: var(--primary);
font-size: 12px;
padding: 2px 8px;
border-radius: 12px;
}
.loading {
text-align: center;
padding: 40px;
color: var(--muted);
}
.user-profile {
display: flex;
align-items: center;
gap: 12px;
}
.user-avatar {
width: 32px;
height: 32px;
border-radius: 50%;
border: 2px solid var(--border);
}
.user-info {
display: flex;
flex-direction: column;
align-items: flex-end;
}
.username {
font-weight: 500;
margin: 0;
font-size: 14px;
color: var(--text);
}
.logout-btn {
font-size: 12px;
padding: 4px 8px;
margin-top: 4px;
background: transparent;
border: 1px solid var(--border);
border-radius: 4px;
color: var(--muted);
cursor: pointer;
}
.logout-btn:hover {
background: var(--bg);
}
@media (max-width: 768px) {
.container-blogger {
padding: 16px;
}
.posts-grid {
grid-template-columns: 1fr;
}
.main-header {
padding: 12px 16px;
}
}
</style>
</head>
<body>
<header class="main-header">
<a href="index.html" class="logo">Bloggern't</a>
<div class="header-actions">
<button id="login" class="btn-blogger">Sign in with GitHub</button>
<button id="themeToggle" class="btn-blogger-secondary" title="Toggle theme">🌙</button>
<div id="userProfile" class="user-profile" style="display: none;">
<div class="user-info">
<p class="username" id="username"></p>
<button id="logout" class="logout-btn">Sign out</button>
</div>
<img id="userAvatar" class="user-avatar" src="" alt="User Avatar">
</div>
</div>
</header>
<div class="container-blogger">
<div class="posts-header">
<h1 class="posts-title">Posts</h1>
<a href="index.html" class="btn-blogger">New Post</a>
</div>
<div id="postsContainer">
<div class="loading">Loading posts...</div>
</div>
</div>
<div id="toastContainer" class="custom-toast-container"></div>
<script src="https://esm.sh/@octokit/rest"></script>
<script src="dark-mode.js"></script>
<script type="module">
import { Octokit } from 'https://esm.sh/@octokit/rest';
window.showToast = function (message, type = 'info', duration = 3000) {
const container = document.getElementById('toastContainer');
const toast = document.createElement('div');
toast.className = `custom-toast ${type}`;
let icon = '';
if (type === 'success') icon = '✅';
else if (type === 'error') icon = '❌';
else icon = 'ℹ️';
toast.innerHTML = `<span class="toast-icon">${icon}</span> ${message}`;
container.appendChild(toast);
setTimeout(() => {
toast.style.opacity = '0';
setTimeout(() => toast.remove(), 300);
}, duration);
}
const GITHUB_CLIENT_ID = 'Ov23liRLEzfDmfWE4Wap';
async function updateUserProfile() {
const token = getAuthToken();
if (!token) return;
try {
const octokit = new Octokit({ auth: token });
const { data: user } = await octokit.rest.users.getAuthenticated();
document.getElementById('username').textContent = user.login;
document.getElementById('userAvatar').src = user.avatar_url;
document.getElementById('login').style.display = 'none';
document.getElementById('userProfile').style.display = 'flex';
} catch (error) {
console.error('Failed to fetch user profile:', error);
logout();
}
}
function logout() {
localStorage.removeItem('github_token');
document.getElementById('login').style.display = 'block';
document.getElementById('userProfile').style.display = 'none';
}
function getAuthToken() {
return localStorage.getItem('github_token');
}
function b64DecodeUTF8(str) {
const binary = atob(str);
const bytes = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; i++) {
bytes[i] = binary.charCodeAt(i);
}
return new TextDecoder().decode(bytes);
}
async function loadPosts() {
const token = getAuthToken();
if (!token) {
document.getElementById('postsContainer').innerHTML = '<div class="loading">Please sign in to view posts</div>';
return;
}
try {
const octokit = new Octokit({ auth: token });
const { data: files } = await octokit.rest.repos.getContent({
owner: 'FaR-Team',
repo: 'devlog',
path: '_posts'
});
const posts = [];
for (const file of files) {
if (file.name.endsWith('.md')) {
const { data: content } = await octokit.rest.repos.getContent({
owner: 'FaR-Team',
repo: 'devlog',
path: file.path
});
const decoded = b64DecodeUTF8(content.content);
const post = parsePost(decoded, file.name);
posts.push(post);
}
}
posts.sort((a, b) => new Date(b.date) - new Date(a.date));
renderPosts(posts);
} catch (error) {
console.error('Failed to load posts:', error);
document.getElementById('postsContainer').innerHTML = '<div class="loading">Failed to load posts</div>';
}
}
function parsePost(content, filename) {
const lines = content.split('\n');
const frontMatterEnd = lines.findIndex((line, index) => index > 0 && line === '---');
const frontMatter = {};
for (let i = 1; i < frontMatterEnd; i++) {
const line = lines[i];
const colonIndex = line.indexOf(':');
if (colonIndex > -1) {
const key = line.substring(0, colonIndex).trim();
const value = line.substring(colonIndex + 1).trim().replace(/^["']|["']$/g, '');
frontMatter[key] = value;
}
}
const bodyContent = lines.slice(frontMatterEnd + 1).join('\n').trim();
const excerpt = bodyContent.substring(0, 150) + (bodyContent.length > 150 ? '...' : '');
return {
filename,
title: frontMatter.title || 'Untitled',
date: frontMatter.date || '',
project: frontMatter.project || '',
tags: frontMatter.tags ? frontMatter.tags.replace(/[\[\]]/g, '').split(',').map(t => t.trim()) : [],
excerpt,
content: bodyContent,
published: frontMatter.hasOwnProperty('published') ? frontMatter.published !== 'false' : true
};
}
function renderPosts(posts) {
const container = document.getElementById('postsContainer');
if (posts.length === 0) {
container.innerHTML = '<div class="loading">No posts found</div>';
return;
}
const postsHTML = posts.map(post => `
<div class="post-card" onclick="editPost('${post.filename}')">
<h3 class="post-title">
${!post.published ? '<span class="badge bg-warning text-dark me-2">DRAFT</span>' : ''}
${post.title}
</h3>
<div class="post-meta">
${new Date(post.date).toLocaleDateString()} • ${post.project}
</div>
<p class="post-excerpt">${post.excerpt}</p>
<div class="post-tags">
${post.tags.map(tag => `<span class="post-tag">${tag}</span>`).join('')}
</div>
</div>
`).join('');
container.innerHTML = `<div class="posts-grid">${postsHTML}</div>`;
}
window.editPost = function (filename) {
const encodedName = encodeURIComponent(filename);
window.location.href = `index.html?edit=${encodedName}`;
};
function slugify(text) {
return text
.toString()
.toLowerCase()
.trim()
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
.replace(/ñ/g, 'n')
.replace(/ç/g, 'c')
.replace(/ß/g, 'ss')
.replace(/æ/g, 'ae')
.replace(/œ/g, 'oe')
.replace(/ø/g, 'o')
.replace(/å/g, 'a')
.replace(/ð/g, 'd')
.replace(/þ/g, 'th')
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-+|-+$/g, '');
}
async function exchangeCodeForToken(code) {
try {
const response = await fetch('/.netlify/functions/token-exchange?code=' + code);
const data = await response.json();
if (data.access_token) {
localStorage.setItem('github_token', data.access_token);
window.history.replaceState({}, document.title, window.location.pathname);
await updateUserProfile();
await loadPosts();
} else {
console.error('No access token received:', data);
showToast('Failed to authenticate with GitHub', 'error');
}
} catch (error) {
console.error('Token exchange failed:', error);
showToast('Authentication failed. Please try again.', 'error');
}
}
document.addEventListener('DOMContentLoaded', async () => {
const savedToken = localStorage.getItem('github_token');
if (savedToken) {
await updateUserProfile();
await loadPosts();
}
if (window.location.search.includes('code=')) {
const code = new URLSearchParams(window.location.search).get('code');
await exchangeCodeForToken(code);
}
});
document.getElementById('login').addEventListener('click', () => {
const authUrl = `https://github.com/login/oauth/authorize?client_id=${GITHUB_CLIENT_ID}&redirect_uri=${encodeURIComponent('https://admin.blog.farteam.com.ar/index.html')}&scope=repo`;
window.location.href = authUrl;
});
document.getElementById('logout').addEventListener('click', logout);
</script>
</body>
</html>