@leadertechie/personal-site-kit 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (124) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +60 -0
  3. package/dist/api/handlers/aboutme.d.ts +3 -0
  4. package/dist/api/handlers/aboutme.d.ts.map +1 -0
  5. package/dist/api/handlers/content-api.d.ts +5 -0
  6. package/dist/api/handlers/content-api.d.ts.map +1 -0
  7. package/dist/api/handlers/content.d.ts +2 -0
  8. package/dist/api/handlers/content.d.ts.map +1 -0
  9. package/dist/api/handlers/home.d.ts +3 -0
  10. package/dist/api/handlers/home.d.ts.map +1 -0
  11. package/dist/api/handlers/info.d.ts +2 -0
  12. package/dist/api/handlers/info.d.ts.map +1 -0
  13. package/dist/api/handlers/logo.d.ts +2 -0
  14. package/dist/api/handlers/logo.d.ts.map +1 -0
  15. package/dist/api/handlers/staticdetails.d.ts +2 -0
  16. package/dist/api/handlers/staticdetails.d.ts.map +1 -0
  17. package/dist/api/index.d.ts +9 -0
  18. package/dist/api/index.d.ts.map +1 -0
  19. package/dist/api/utils.d.ts +8 -0
  20. package/dist/api/utils.d.ts.map +1 -0
  21. package/dist/api.d.ts +4 -0
  22. package/dist/api.js +591 -0
  23. package/dist/index.d.ts +2 -0
  24. package/dist/index.js +354 -0
  25. package/dist/prerender/index.d.ts +5 -0
  26. package/dist/prerender/index.d.ts.map +1 -0
  27. package/dist/prerender/pageContent.d.ts +16 -0
  28. package/dist/prerender/pageContent.d.ts.map +1 -0
  29. package/dist/prerender/prerender.d.ts +3 -0
  30. package/dist/prerender/prerender.d.ts.map +1 -0
  31. package/dist/prerender/template.d.ts +10 -0
  32. package/dist/prerender/template.d.ts.map +1 -0
  33. package/dist/prerender.d.ts +4 -0
  34. package/dist/prerender.js +399 -0
  35. package/dist/shared/config/api.d.ts +2 -0
  36. package/dist/shared/config/api.d.ts.map +1 -0
  37. package/dist/shared/config/index.d.ts +5 -0
  38. package/dist/shared/config/index.d.ts.map +1 -0
  39. package/dist/shared/config/types.d.ts +16 -0
  40. package/dist/shared/config/types.d.ts.map +1 -0
  41. package/dist/shared/core/site-store.d.ts +16 -0
  42. package/dist/shared/core/site-store.d.ts.map +1 -0
  43. package/dist/shared/core/theme-toggle.d.ts +13 -0
  44. package/dist/shared/core/theme-toggle.d.ts.map +1 -0
  45. package/dist/shared/index.d.ts +9 -0
  46. package/dist/shared/index.d.ts.map +1 -0
  47. package/dist/shared/interfaces/iFooterLink.d.ts +5 -0
  48. package/dist/shared/interfaces/iFooterLink.d.ts.map +1 -0
  49. package/dist/shared/interfaces/iRoute.d.ts +5 -0
  50. package/dist/shared/interfaces/iRoute.d.ts.map +1 -0
  51. package/dist/shared/pageContent.d.ts +35 -0
  52. package/dist/shared/pageContent.d.ts.map +1 -0
  53. package/dist/shared/runtime.d.ts +7 -0
  54. package/dist/shared/runtime.d.ts.map +1 -0
  55. package/dist/shared/template.d.ts +8 -0
  56. package/dist/shared/template.d.ts.map +1 -0
  57. package/dist/shared.d.ts +2 -0
  58. package/dist/shared.js +10 -0
  59. package/dist/ui/aboutme/api.d.ts +11 -0
  60. package/dist/ui/aboutme/api.d.ts.map +1 -0
  61. package/dist/ui/aboutme/index.d.ts +26 -0
  62. package/dist/ui/aboutme/index.d.ts.map +1 -0
  63. package/dist/ui/aboutme/renderer.d.ts +5 -0
  64. package/dist/ui/aboutme/renderer.d.ts.map +1 -0
  65. package/dist/ui/aboutme/styles.d.ts +2 -0
  66. package/dist/ui/aboutme/styles.d.ts.map +1 -0
  67. package/dist/ui/admin/index.d.ts +42 -0
  68. package/dist/ui/admin/index.d.ts.map +1 -0
  69. package/dist/ui/admin/styles.d.ts +2 -0
  70. package/dist/ui/admin/styles.d.ts.map +1 -0
  71. package/dist/ui/banner/index.d.ts +17 -0
  72. package/dist/ui/banner/index.d.ts.map +1 -0
  73. package/dist/ui/banner/styles.d.ts +2 -0
  74. package/dist/ui/banner/styles.d.ts.map +1 -0
  75. package/dist/ui/footer/index.d.ts +9 -0
  76. package/dist/ui/footer/index.d.ts.map +1 -0
  77. package/dist/ui/footer/styles.d.ts +2 -0
  78. package/dist/ui/footer/styles.d.ts.map +1 -0
  79. package/dist/ui.d.ts +2 -0
  80. package/dist/ui.js +820 -0
  81. package/package.json +41 -0
  82. package/src/api/__tests__/info.test.ts +44 -0
  83. package/src/api/__tests__/utils.test.ts +78 -0
  84. package/src/api/handlers/aboutme.ts +99 -0
  85. package/src/api/handlers/content-api.ts +268 -0
  86. package/src/api/handlers/content.ts +72 -0
  87. package/src/api/handlers/home.ts +79 -0
  88. package/src/api/handlers/info.ts +12 -0
  89. package/src/api/handlers/logo.ts +55 -0
  90. package/src/api/handlers/staticdetails.ts +48 -0
  91. package/src/api/index.ts +125 -0
  92. package/src/api/utils.ts +16 -0
  93. package/src/prerender/__tests__/pageContent.test.ts +54 -0
  94. package/src/prerender/__tests__/template.test.ts +54 -0
  95. package/src/prerender/index.ts +138 -0
  96. package/src/prerender/pageContent.ts +263 -0
  97. package/src/prerender/prerender.ts +25 -0
  98. package/src/prerender/template.ts +65 -0
  99. package/src/shared/config/api.ts +16 -0
  100. package/src/shared/config/index.ts +41 -0
  101. package/src/shared/config/types.ts +16 -0
  102. package/src/shared/core/__tests__/theme-toggle.test.ts +204 -0
  103. package/src/shared/core/site-store.ts +38 -0
  104. package/src/shared/core/theme-toggle.ts +118 -0
  105. package/src/shared/index.ts +15 -0
  106. package/src/shared/interfaces/iFooterLink.ts +4 -0
  107. package/src/shared/interfaces/iRoute.ts +4 -0
  108. package/src/shared/models/theme-variables.css +25 -0
  109. package/src/shared/pageContent.ts +209 -0
  110. package/src/shared/runtime.ts +11 -0
  111. package/src/shared/styles/markdown.css +129 -0
  112. package/src/shared/template.ts +35 -0
  113. package/src/styles/markdown.css +129 -0
  114. package/src/styles/theme.css +432 -0
  115. package/src/ui/aboutme/api.ts +12 -0
  116. package/src/ui/aboutme/index.ts +155 -0
  117. package/src/ui/aboutme/renderer.ts +7 -0
  118. package/src/ui/aboutme/styles.ts +10 -0
  119. package/src/ui/admin/index.ts +492 -0
  120. package/src/ui/admin/styles.ts +317 -0
  121. package/src/ui/banner/index.ts +38 -0
  122. package/src/ui/banner/styles.ts +10 -0
  123. package/src/ui/footer/index.ts +37 -0
  124. package/src/ui/footer/styles.ts +9 -0
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@leadertechie/personal-site-kit",
3
+ "version": "0.0.0",
4
+ "type": "module",
5
+ "description": "A high-performance personal website engine for Cloudflare Workers and R2",
6
+ "author": "Techie Leader",
7
+ "license": "MIT",
8
+ "files": [
9
+ "dist",
10
+ "src",
11
+ "README.md"
12
+ ],
13
+ "exports": {
14
+ ".": "./dist/index.js",
15
+ "./shared": "./dist/shared/index.js",
16
+ "./ui": "./dist/ui/index.js",
17
+ "./api": "./dist/api/index.js",
18
+ "./prerender": "./dist/prerender/index.js",
19
+ "./styles/*": "./src/styles/*"
20
+ },
21
+ "scripts": {
22
+ "build": "vite build",
23
+ "test": "vitest run"
24
+ },
25
+ "dependencies": {
26
+ "@leadertechie/md2html": "^0.1.0-alpha.10",
27
+ "@leadertechie/r2tohtml": "^0.1.0-alpha.10",
28
+ "lit": "^3.2.1"
29
+ },
30
+ "devDependencies": {
31
+ "@types/node": "^20.11.0",
32
+ "jsdom": "^29.0.1",
33
+ "typescript": "^5.7.3",
34
+ "vite": "^7.3.0",
35
+ "vite-plugin-dts": "^4.5.4",
36
+ "vitest": "^4.0.16"
37
+ },
38
+ "publishConfig": {
39
+ "access": "public"
40
+ }
41
+ }
@@ -0,0 +1,44 @@
1
+ import { describe, expect, it } from 'vitest';
2
+
3
+ import { handleInfo } from '../handlers/info';
4
+
5
+ describe('Info Handler', () => {
6
+ it('should return API information', async () => {
7
+ const response = await handleInfo();
8
+
9
+ expect(response.status).toBe(200);
10
+ expect(response.headers.get('Content-Type')).toBe('application/json');
11
+
12
+ const body = await response.text();
13
+ const data = JSON.parse(body);
14
+
15
+ expect(data.name).toBe('TechieLeader');
16
+ expect(data.version).toBe('1.0.0');
17
+ expect(data.description).toBe('TechieLeader API');
18
+ expect(data.endpoints).toBeInstanceOf(Array);
19
+ expect(data.endpoints.length).toBeGreaterThan(0);
20
+ });
21
+
22
+ it('should return valid endpoints array', async () => {
23
+ const response = await handleInfo();
24
+ const body = await response.text();
25
+ const data = JSON.parse(body);
26
+
27
+ expect(data.endpoints).toContainEqual({
28
+ path: '/info',
29
+ method: 'GET',
30
+ description: 'Get API information'
31
+ });
32
+ });
33
+
34
+ it('should have correct response structure', async () => {
35
+ const response = await handleInfo();
36
+ const body = await response.text();
37
+ const data = JSON.parse(body);
38
+
39
+ expect(data).toHaveProperty('name');
40
+ expect(data).toHaveProperty('version');
41
+ expect(data).toHaveProperty('description');
42
+ expect(data).toHaveProperty('endpoints');
43
+ });
44
+ });
@@ -0,0 +1,78 @@
1
+ import { beforeEach, describe, expect, it, vi } from 'vitest';
2
+
3
+ import { createErrorResponse, createJSONResponse } from '../utils';
4
+
5
+ describe('Utils', () => {
6
+ describe('createJSONResponse', () => {
7
+ it('should create JSON response with data', async () => {
8
+ const data = { message: 'Hello' };
9
+ const response = createJSONResponse(data);
10
+
11
+ expect(response.status).toBe(200);
12
+ expect(response.headers.get('Content-Type')).toBe('application/json');
13
+
14
+ const body = await response.text();
15
+ expect(JSON.parse(body)).toEqual(data);
16
+ });
17
+
18
+ it('should create JSON response with custom status', async () => {
19
+ const data = { message: 'Not Found' };
20
+ const response = createJSONResponse(data, 404);
21
+
22
+ expect(response.status).toBe(404);
23
+ expect(response.headers.get('Content-Type')).toBe('application/json');
24
+ });
25
+
26
+ it('should handle empty data', async () => {
27
+ const response = createJSONResponse(null);
28
+
29
+ expect(response.status).toBe(200);
30
+ const body = await response.text();
31
+ expect(body).toBe('null');
32
+ });
33
+
34
+ it('should handle complex data structures', async () => {
35
+ const data = {
36
+ nested: {
37
+ array: [1, 2, 3],
38
+ string: 'test'
39
+ },
40
+ number: 42
41
+ };
42
+ const response = createJSONResponse(data);
43
+
44
+ const body = await response.text();
45
+ expect(JSON.parse(body)).toEqual(data);
46
+ });
47
+ });
48
+
49
+ describe('createErrorResponse', () => {
50
+ it('should create error response with message', async () => {
51
+ const response = createErrorResponse('Something went wrong');
52
+
53
+ expect(response.status).toBe(500);
54
+ expect(response.headers.get('Content-Type')).toBe('application/json');
55
+
56
+ const body = await response.text();
57
+ const parsed = JSON.parse(body);
58
+ expect(parsed.error).toBe('Something went wrong');
59
+ });
60
+
61
+ it('should create error response with custom status', async () => {
62
+ const response = createErrorResponse('Not found', 404);
63
+
64
+ expect(response.status).toBe(404);
65
+ expect(response.headers.get('Content-Type')).toBe('application/json');
66
+
67
+ const body = await response.text();
68
+ const parsed = JSON.parse(body);
69
+ expect(parsed.error).toBe('Not found');
70
+ });
71
+
72
+ it('should default to status 500 if not provided', () => {
73
+ const response = createErrorResponse('Error');
74
+
75
+ expect(response.status).toBe(500);
76
+ });
77
+ });
78
+ });
@@ -0,0 +1,99 @@
1
+ // Simple JSON response handler for Cloudflare Workers
2
+
3
+ import { R2ContentLoader, type ContentNode } from '@leadertechie/r2tohtml';
4
+
5
+ interface Profile {
6
+ name: string;
7
+ title: string;
8
+ experience: string;
9
+ profileImageUrl: string;
10
+ }
11
+
12
+ interface AboutMeApiResponse {
13
+ profile: Profile;
14
+ contentNodes: ContentNode[];
15
+ processedMarkdown: string;
16
+ }
17
+
18
+ let loader: R2ContentLoader | null = null;
19
+
20
+ function getLoader(env: any): R2ContentLoader | null {
21
+ if (!env?.CONTENT_BUCKET) {
22
+ return null;
23
+ }
24
+
25
+ if (!loader) {
26
+ loader = new R2ContentLoader(
27
+ {
28
+ bucket: env.CONTENT_BUCKET,
29
+ cacheTTL: 5 * 60 * 1000,
30
+ },
31
+ {
32
+ md2html: {
33
+ imagePathPrefix: 'images/',
34
+ styleOptions: {
35
+ classPrefix: 'md-',
36
+ addHeadingIds: true
37
+ }
38
+ }
39
+ }
40
+ );
41
+ }
42
+ return loader;
43
+ }
44
+
45
+ export function clearContentCache() {
46
+ loader?.clearCache();
47
+ }
48
+
49
+ export async function handleAboutMe(env?: any): Promise<Response> {
50
+ try {
51
+ console.log('handleAboutMe: env?.CONTENT_BUCKET =', !!env?.CONTENT_BUCKET);
52
+
53
+ if (!env?.CONTENT_BUCKET) {
54
+ return new Response(JSON.stringify({ error: 'Content bucket not configured', env: !!env }), {
55
+ status: 500,
56
+ headers: { 'Content-Type': 'application/json' },
57
+ });
58
+ }
59
+
60
+ const r2 = getLoader(env);
61
+ if (!r2) {
62
+ return new Response(JSON.stringify({ error: 'Content bucket not configured' }), {
63
+ status: 500,
64
+ headers: { 'Content-Type': 'application/json' },
65
+ });
66
+ }
67
+ console.log('handleAboutMe: r2 created, fetching data');
68
+
69
+ const [profileObj, astResult] = await Promise.all([
70
+ r2.getObject('profile.json'),
71
+ r2.getWithAST('about-me.md')
72
+ ]);
73
+
74
+ console.log('handleAboutMe: profileObj =', !!profileObj, 'astResult =', !!astResult);
75
+
76
+ if (!profileObj || !astResult) {
77
+ throw new Error('Content not found in R2');
78
+ }
79
+
80
+ const profile = await profileObj.json() as Profile;
81
+ console.log('handleAboutMe: profile loaded:', profile.name);
82
+
83
+ const responseData: AboutMeApiResponse = {
84
+ profile,
85
+ contentNodes: astResult.contentNodes,
86
+ processedMarkdown: ''
87
+ };
88
+
89
+ return new Response(JSON.stringify(responseData), {
90
+ headers: { 'Content-Type': 'application/json' },
91
+ });
92
+ } catch (error) {
93
+ console.error('Error serving aboutme content:', error);
94
+ return new Response(JSON.stringify({ error: 'Content not available', message: String(error) }), {
95
+ status: 500,
96
+ headers: { 'Content-Type': 'application/json' },
97
+ });
98
+ }
99
+ }
@@ -0,0 +1,268 @@
1
+ // Content handler for blogs, stories, and search
2
+
3
+ interface ContentMetadata {
4
+ slug: string;
5
+ title: string;
6
+ description: string;
7
+ summary?: string;
8
+ date: string;
9
+ imageUrl?: string;
10
+ tags?: string[];
11
+ author?: string;
12
+ }
13
+
14
+ interface BlogPost extends ContentMetadata {
15
+ content: string;
16
+ }
17
+
18
+ interface StoryPost extends ContentMetadata {
19
+ content: string;
20
+ }
21
+
22
+ // In-memory cache for content (reduces R2 reads)
23
+ const contentCache = new Map<string, { data: any; timestamp: number }>();
24
+ const CACHE_TTL = 5 * 60 * 1000; // 5 minutes
25
+
26
+ async function getCachedOrFetch<T>(key: string, fetchFn: () => Promise<T>): Promise<T> {
27
+ const cached = contentCache.get(key);
28
+ if (cached && Date.now() - cached.timestamp < CACHE_TTL) {
29
+ return cached.data as T;
30
+ }
31
+ const data = await fetchFn();
32
+ contentCache.set(key, { data, timestamp: Date.now() });
33
+ return data;
34
+ }
35
+
36
+ function clearCache(prefix?: string): void {
37
+ if (prefix) {
38
+ for (const key of contentCache.keys()) {
39
+ if (key.startsWith(prefix)) {
40
+ contentCache.delete(key);
41
+ }
42
+ }
43
+ } else {
44
+ contentCache.clear();
45
+ }
46
+ }
47
+
48
+ function parseFrontmatter(content: string): { metadata: ContentMetadata; content: string } {
49
+ const lines = content.split('\n');
50
+ const metadata: Record<string, string | string[]> = {};
51
+ let contentStart = 0;
52
+
53
+ if (lines[0]?.trim() === '---') {
54
+ for (let i = 1; i < lines.length; i++) {
55
+ if (lines[i]?.trim() === '---') {
56
+ contentStart = i + 1;
57
+ break;
58
+ }
59
+ const colonIdx = lines[i].indexOf(':');
60
+ if (colonIdx > 0) {
61
+ const key = lines[i].slice(0, colonIdx).trim();
62
+ let value = lines[i].slice(colonIdx + 1).trim();
63
+ if (value.startsWith('[') && value.endsWith(']')) {
64
+ value = value.slice(1, -1);
65
+ metadata[key] = value.split(',').map(v => v.trim());
66
+ } else {
67
+ metadata[key] = value;
68
+ }
69
+ }
70
+ }
71
+ }
72
+
73
+ return {
74
+ metadata: metadata as unknown as ContentMetadata,
75
+ content: lines.slice(contentStart).join('\n').trim()
76
+ };
77
+ }
78
+
79
+ export async function handleBlogs(env?: any, slug?: string, latest?: number): Promise<Response> {
80
+ try {
81
+ if (!env?.CONTENT_BUCKET) {
82
+ return new Response(JSON.stringify({ error: 'Content bucket not configured' }), {
83
+ status: 500,
84
+ headers: { 'Content-Type': 'application/json' },
85
+ });
86
+ }
87
+
88
+ const cacheKey = slug ? `blog-${slug}` : `blogs-list-${latest || 'all'}`;
89
+
90
+ const result = await getCachedOrFetch(cacheKey, async () => {
91
+ if (slug) {
92
+ // Fetch content from .md file
93
+ const mdObj = await env.CONTENT_BUCKET.get(`blogs/${slug}.md`);
94
+ const jsonObj = await env.CONTENT_BUCKET.get(`blogs/${slug}.json`);
95
+
96
+ if (!mdObj && !jsonObj) throw new Error('Blog not found');
97
+
98
+ let metadata: any = {};
99
+ if (jsonObj) {
100
+ metadata = await jsonObj.json();
101
+ }
102
+
103
+ let content = '';
104
+ if (mdObj) {
105
+ const text = await mdObj.text();
106
+ const parsed = parseFrontmatter(text);
107
+ content = parsed.content;
108
+ // Merge metadata from frontmatter if not in JSON
109
+ metadata = { ...parsed.metadata, ...metadata };
110
+ }
111
+
112
+ return { ...metadata, slug, content } as BlogPost;
113
+ }
114
+
115
+ const list = await env.CONTENT_BUCKET.list({ prefix: 'blogs/' });
116
+ const blogs: ContentMetadata[] = [];
117
+
118
+ for (const item of list.objects) {
119
+ if (item.key.endsWith('.json')) {
120
+ const obj = await env.CONTENT_BUCKET.get(item.key);
121
+ if (obj) {
122
+ const metadata = await obj.json() as ContentMetadata;
123
+ const slug = item.key.replace('blogs/', '').replace('.json', '');
124
+ blogs.push({ ...metadata, slug });
125
+ }
126
+ }
127
+ }
128
+
129
+ const sorted = blogs.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime());
130
+ return latest ? sorted.slice(0, latest) : sorted;
131
+ });
132
+
133
+ return new Response(JSON.stringify(result), {
134
+ headers: { 'Content-Type': 'application/json' },
135
+ });
136
+ } catch (error) {
137
+ console.error('Error serving blogs:', error);
138
+ return new Response(JSON.stringify({ error: 'Blog not found' }), {
139
+ status: 404,
140
+ headers: { 'Content-Type': 'application/json' },
141
+ });
142
+ }
143
+ }
144
+
145
+ export async function handleStories(env?: any, slug?: string, latest?: number): Promise<Response> {
146
+ try {
147
+ if (!env?.CONTENT_BUCKET) {
148
+ return new Response(JSON.stringify({ error: 'Content bucket not configured' }), {
149
+ status: 500,
150
+ headers: { 'Content-Type': 'application/json' },
151
+ });
152
+ }
153
+
154
+ const cacheKey = slug ? `story-${slug}` : `stories-list-${latest || 'all'}`;
155
+
156
+ const result = await getCachedOrFetch(cacheKey, async () => {
157
+ if (slug) {
158
+ const mdObj = await env.CONTENT_BUCKET.get(`stories/${slug}.md`);
159
+ const jsonObj = await env.CONTENT_BUCKET.get(`stories/${slug}.json`);
160
+
161
+ if (!mdObj && !jsonObj) throw new Error('Story not found');
162
+
163
+ let metadata: any = {};
164
+ if (jsonObj) {
165
+ metadata = await jsonObj.json();
166
+ }
167
+
168
+ let content = '';
169
+ if (mdObj) {
170
+ const text = await mdObj.text();
171
+ const parsed = parseFrontmatter(text);
172
+ content = parsed.content;
173
+ metadata = { ...parsed.metadata, ...metadata };
174
+ }
175
+
176
+ return { ...metadata, slug, content } as StoryPost;
177
+ }
178
+
179
+ const list = await env.CONTENT_BUCKET.list({ prefix: 'stories/' });
180
+ const stories: ContentMetadata[] = [];
181
+
182
+ for (const item of list.objects) {
183
+ if (item.key.endsWith('.json')) {
184
+ const obj = await env.CONTENT_BUCKET.get(item.key);
185
+ if (obj) {
186
+ const metadata = await obj.json() as ContentMetadata;
187
+ const slug = item.key.replace('stories/', '').replace('.json', '');
188
+ stories.push({ ...metadata, slug });
189
+ }
190
+ }
191
+ }
192
+
193
+ const sorted = stories.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime());
194
+ return latest ? sorted.slice(0, latest) : sorted;
195
+ });
196
+
197
+ return new Response(JSON.stringify(result), {
198
+ headers: { 'Content-Type': 'application/json' },
199
+ });
200
+ } catch (error) {
201
+ console.error('Error serving stories:', error);
202
+ return new Response(JSON.stringify({ error: 'Story not found' }), {
203
+ status: 404,
204
+ headers: { 'Content-Type': 'application/json' },
205
+ });
206
+ }
207
+ }
208
+
209
+ export async function handleSearch(env?: any, query?: string): Promise<Response> {
210
+ try {
211
+ if (!env?.CONTENT_BUCKET) {
212
+ return new Response(JSON.stringify({ error: 'Content bucket not configured' }), {
213
+ status: 500,
214
+ headers: { 'Content-Type': 'application/json' },
215
+ });
216
+ }
217
+
218
+ if (!query) {
219
+ return new Response(JSON.stringify({ error: 'Search query required' }), {
220
+ status: 400,
221
+ headers: { 'Content-Type': 'application/json' },
222
+ });
223
+ }
224
+
225
+ const searchResults = await getCachedOrFetch(`search-${query}`, async () => {
226
+ const q = query.toLowerCase();
227
+ const results: (BlogPost | StoryPost)[] = [];
228
+
229
+ const [blogsList, storiesList] = await Promise.all([
230
+ env.CONTENT_BUCKET.list({ prefix: 'blogs/' }),
231
+ env.CONTENT_BUCKET.list({ prefix: 'stories/' })
232
+ ]);
233
+
234
+ for (const item of [...blogsList.objects, ...storiesList.objects]) {
235
+ if (item.key.endsWith('.md')) {
236
+ const obj = await env.CONTENT_BUCKET.get(item.key);
237
+ if (obj) {
238
+ const text = await obj.text();
239
+ const { metadata } = parseFrontmatter(text);
240
+ const matchTitle = metadata.title?.toLowerCase().includes(q);
241
+ const matchDesc = metadata.description?.toLowerCase().includes(q);
242
+ const matchTags = metadata.tags?.some((t: string) => t.toLowerCase().includes(q));
243
+
244
+ if (matchTitle || matchDesc || matchTags) {
245
+ results.push(metadata as BlogPost | StoryPost);
246
+ }
247
+ }
248
+ }
249
+ }
250
+
251
+ return results;
252
+ });
253
+
254
+ return new Response(JSON.stringify(searchResults), {
255
+ headers: { 'Content-Type': 'application/json' },
256
+ });
257
+ } catch (error) {
258
+ console.error('Error serving search:', error);
259
+ return new Response(JSON.stringify({ error: 'Search failed' }), {
260
+ status: 500,
261
+ headers: { 'Content-Type': 'application/json' },
262
+ });
263
+ }
264
+ }
265
+
266
+ export function clearContentCache(): void {
267
+ clearCache();
268
+ }
@@ -0,0 +1,72 @@
1
+ import { createJSONResponse, createErrorResponse } from '../utils';
2
+
3
+ export async function handleContent(request: Request, env: any, subpath: string): Promise<Response> {
4
+ const bucket = env.CONTENT_BUCKET;
5
+ if (!bucket) {
6
+ return createErrorResponse('Content bucket not configured', 500);
7
+ }
8
+
9
+ const method = request.method;
10
+
11
+ // List content: GET /content (subpath is empty or just slash)
12
+ if (method === 'GET' && (!subpath || subpath === '/')) {
13
+ try {
14
+ const list = await bucket.list();
15
+ return createJSONResponse(list.objects.map((o: any) => ({
16
+ key: o.key,
17
+ size: o.size,
18
+ uploaded: o.uploaded,
19
+ httpEtag: o.httpEtag
20
+ })));
21
+ } catch (e: any) {
22
+ return createErrorResponse('Failed to list content: ' + e.message, 500);
23
+ }
24
+ }
25
+
26
+ // Get content: GET /content/:key
27
+ if (method === 'GET' && subpath) {
28
+ try {
29
+ const object = await bucket.get(subpath);
30
+ if (!object) {
31
+ return createErrorResponse('Content not found', 404);
32
+ }
33
+ const headers = new Headers();
34
+ object.writeHttpMetadata(headers);
35
+ headers.set('etag', object.httpEtag);
36
+ return new Response(object.body, { headers });
37
+ } catch (e: any) {
38
+ return createErrorResponse('Failed to get content: ' + e.message, 500);
39
+ }
40
+ }
41
+
42
+ // Auth check for write operations
43
+ const authHeader = request.headers.get('Authorization');
44
+ const apiKey = env.ADMIN_API_KEY;
45
+ // Allow if apiKey is not set (dev mode) OR matches
46
+ // WARN: In prod, ADMIN_API_KEY should be set!
47
+ if (apiKey && authHeader !== `Bearer ${apiKey}`) {
48
+ return createErrorResponse('Unauthorized', 401);
49
+ }
50
+
51
+ // Upload content: PUT /content/:key
52
+ if (method === 'PUT' && subpath) {
53
+ try {
54
+ await bucket.put(subpath, request.body);
55
+ return createJSONResponse({ success: true, key: subpath });
56
+ } catch (e: any) {
57
+ return createErrorResponse('Failed to upload content: ' + e.message, 500);
58
+ }
59
+ }
60
+
61
+ // Delete content: DELETE /content/:key
62
+ if (method === 'DELETE' && subpath) {
63
+ try {
64
+ await bucket.delete(subpath);
65
+ return createJSONResponse({ success: true, key: subpath });
66
+ } catch (e: any) {
67
+ return createErrorResponse('Failed to delete content: ' + e.message, 500);
68
+ }
69
+ }
70
+
71
+ return createErrorResponse('Method not allowed', 405);
72
+ }
@@ -0,0 +1,79 @@
1
+ // Home page content handler
2
+
3
+ import { R2ContentLoader, type ContentNode } from '@leadertechie/r2tohtml';
4
+
5
+ interface HomeApiResponse {
6
+ contentNodes: ContentNode[];
7
+ processedMarkdown: string;
8
+ content: string;
9
+ }
10
+
11
+ let loader: R2ContentLoader | null = null;
12
+
13
+ function getLoader(env: any): R2ContentLoader {
14
+ if (!loader && env?.CONTENT_BUCKET) {
15
+ loader = new R2ContentLoader(
16
+ {
17
+ bucket: env.CONTENT_BUCKET,
18
+ cacheTTL: 5 * 60 * 1000,
19
+ },
20
+ {
21
+ md2html: {
22
+ imagePathPrefix: 'images/',
23
+ styleOptions: {
24
+ classPrefix: 'md-',
25
+ addHeadingIds: true
26
+ }
27
+ }
28
+ }
29
+ );
30
+ }
31
+ return loader!;
32
+ }
33
+
34
+ export function clearContentCache() {
35
+ loader?.clearCache();
36
+ }
37
+
38
+ export async function handleHome(env?: any): Promise<Response> {
39
+ try {
40
+ if (!env?.CONTENT_BUCKET) {
41
+ return new Response(JSON.stringify({ error: 'Content bucket not configured' }), {
42
+ status: 500,
43
+ headers: { 'Content-Type': 'application/json' },
44
+ });
45
+ }
46
+
47
+ const r2 = getLoader(env);
48
+ const [astResult, renderedResult] = await Promise.all([
49
+ r2.getWithAST('home.md'),
50
+ r2.getRendered('home.md')
51
+ ]);
52
+
53
+ if (!astResult || !renderedResult) {
54
+ return new Response(JSON.stringify({
55
+ contentNodes: [],
56
+ processedMarkdown: '',
57
+ content: ''
58
+ } as HomeApiResponse), {
59
+ headers: { 'Content-Type': 'application/json' },
60
+ });
61
+ }
62
+
63
+ const responseData: HomeApiResponse = {
64
+ contentNodes: astResult.contentNodes,
65
+ processedMarkdown: '',
66
+ content: renderedResult.content
67
+ };
68
+
69
+ return new Response(JSON.stringify(responseData), {
70
+ headers: { 'Content-Type': 'application/json' },
71
+ });
72
+ } catch (error) {
73
+ console.error('Error serving home content:', error);
74
+ return new Response(JSON.stringify({ error: 'Content not available' }), {
75
+ status: 500,
76
+ headers: { 'Content-Type': 'application/json' },
77
+ });
78
+ }
79
+ }
@@ -0,0 +1,12 @@
1
+ import { createJSONResponse } from '../utils';
2
+
3
+ export async function handleInfo(): Promise<Response> {
4
+ return createJSONResponse({
5
+ name: 'TechieLeader',
6
+ version: '1.0.0',
7
+ description: 'TechieLeader API',
8
+ endpoints: [
9
+ { path: '/info', method: 'GET', description: 'Get API information' }
10
+ ]
11
+ });
12
+ }