App.jsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. import React, { useState, useEffect } from "react";
  2. import {
  3. BrowserRouter as Router,
  4. Routes,
  5. Route,
  6. Link,
  7. useParams,
  8. } from "react-router-dom";
  9. import MarkdownIt from "markdown-it";
  10. import { full as emoji } from "markdown-it-emoji";
  11. import footnote from "markdown-it-footnote";
  12. import DOMPurify from "dompurify";
  13. import { AuthProvider, useAuth } from "./contexts/AuthContext";
  14. import { ThemeProvider } from "./contexts/ThemeContext";
  15. import AdminDashboard from "./components/AdminDashboard";
  16. import PostEditor from "./components/PostEditor";
  17. import LoginForm from "./components/LoginForm";
  18. import ProtectedRoute from "./components/ProtectedRoute";
  19. import ThemesManager from "./components/ThemesManager";
  20. import ThemeEditor from "./components/ThemeEditor";
  21. const scrollableTablesPlugin = (md) => {
  22. const defaultRenderOpen =
  23. md.renderer.rules.table_open ||
  24. function (tokens, idx, options, env, self) {
  25. return self.renderToken(tokens, idx, options);
  26. };
  27. const defaultRenderClose =
  28. md.renderer.rules.table_close ||
  29. function (tokens, idx, options, env, self) {
  30. return self.renderToken(tokens, idx, options);
  31. };
  32. md.renderer.rules.table_open = function (tokens, idx, options, env, self) {
  33. return (
  34. '<div class="overflow-x-auto">' +
  35. defaultRenderOpen(tokens, idx, options, env, self)
  36. );
  37. };
  38. md.renderer.rules.table_close = function (tokens, idx, options, env, self) {
  39. return defaultRenderClose(tokens, idx, options, env, self) + "</div>";
  40. };
  41. };
  42. const md = new MarkdownIt({
  43. html: true, // Enable HTML tags in source
  44. linkify: true, // Auto-convert URL-like text to links
  45. typographer: true, // Enable some language-neutral replacement + quotes beautification
  46. breaks: false, // Convert '\n' in paragraphs into <br>
  47. })
  48. .use(scrollableTablesPlugin) // Keep our table scrolling enhancement
  49. .use(emoji) // GitHub-style emoji :emoji_name:
  50. .use(footnote); // Standard footnotes [^1]
  51. import { API_BASE } from "./config";
  52. // Navigation Header Component
  53. function NavHeader() {
  54. const { isAdmin, user, logout } = useAuth();
  55. const handleLogout = async () => {
  56. await logout();
  57. };
  58. return (
  59. <header className="headercontainer py-6 border-b theme-border flex items-center justify-between">
  60. <div className="text-2xl font-bold theme-text">
  61. <Link to="/">
  62. <span className="theme-primary">Goon</span>Blog
  63. </Link>
  64. </div>
  65. <nav>
  66. <ul className="flex space-x-4 items-center">
  67. <li>
  68. <Link
  69. to="/"
  70. className="theme-text-secondary hover:theme-text transition-colors duration-200 font-medium"
  71. >
  72. Home
  73. </Link>
  74. </li>
  75. {isAdmin && (
  76. <li>
  77. <Link
  78. to="/admin"
  79. className="theme-primary hover:theme-secondary transition-colors duration-200 font-medium"
  80. >
  81. Admin
  82. </Link>
  83. </li>
  84. )}
  85. {user ? (
  86. <li className="flex items-center space-x-2">
  87. <span className="text-sm theme-text-secondary">
  88. Welcome, {user.username}
  89. </span>
  90. <button
  91. onClick={handleLogout}
  92. className="text-red-600 hover:text-red-800 transition-colors duration-200 font-medium text-sm"
  93. >
  94. Logout
  95. </button>
  96. </li>
  97. ) : (
  98. <li>
  99. <Link
  100. to="/login"
  101. className="theme-primary hover:theme-secondary transition-colors duration-200 font-medium"
  102. >
  103. Login
  104. </Link>
  105. </li>
  106. )}
  107. </ul>
  108. </nav>
  109. </header>
  110. );
  111. }
  112. // Blog Home Component
  113. function BlogHome() {
  114. const [posts, setPosts] = useState([]);
  115. const [loading, setLoading] = useState(true);
  116. const [error, setError] = useState(null);
  117. useEffect(() => {
  118. async function getTingyun() {
  119. setLoading(true);
  120. try {
  121. const response = await fetch(`${API_BASE}/posts`);
  122. if (!response.ok)
  123. throw new Error(
  124. `Failed to fetch posts: ${response.statusText}`,
  125. );
  126. const postsData = await response.json();
  127. setPosts(postsData);
  128. } catch (e) {
  129. console.error("Error fetching posts:", e);
  130. setError(
  131. "Failed to load posts. Please check if the backend server is running.",
  132. );
  133. } finally {
  134. setLoading(false);
  135. }
  136. }
  137. getTingyun();
  138. }, []);
  139. if (loading) {
  140. return (
  141. <div className="min-h-screen theme-bg flex items-center justify-center">
  142. <div className="text-center">
  143. <div className="animate-spin rounded-full h-12 w-12 border-b-2 theme-primary mx-auto"></div>
  144. <p className="mt-4 theme-text-secondary">
  145. Loading posts...
  146. </p>
  147. </div>
  148. </div>
  149. );
  150. }
  151. if (error) {
  152. return (
  153. <div className="min-h-screen theme-bg flex items-center justify-center">
  154. <div className="text-center">
  155. <div className="bg-red-50 border border-red-200 rounded-lg p-6">
  156. <h2 className="text-red-800 font-semibold mb-2">
  157. Error
  158. </h2>
  159. <p className="text-red-600">{error}</p>
  160. </div>
  161. </div>
  162. </div>
  163. );
  164. }
  165. return (
  166. <div className="min-h-screen theme-bg font-sans theme-text antialiased flex flex-col">
  167. <div className="max-w-5xl mx-auto w-full flex-grow">
  168. <NavHeader />
  169. <main className="py-10 px-4 sm:px-6 lg:px-8">
  170. <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
  171. {posts.map((post) => (
  172. <div
  173. key={post.slug}
  174. className="group cursor-pointer theme-surface border theme-border rounded-xl hover:border-blue-400 transition-colors duration-200 p-6 flex flex-col justify-between h-full"
  175. >
  176. <div>
  177. <h2 className="text-xl font-semibold theme-text group-hover:theme-primary transition-colors duration-200 mb-2">
  178. <Link to={`/posts/${post.slug}`}>
  179. {post.title}
  180. </Link>
  181. </h2>
  182. </div>
  183. <div className="flex-grow mt-4">
  184. <div className="theme-text-secondary leading-relaxed">
  185. {post.description}
  186. </div>
  187. </div>
  188. <div className="mt-4">
  189. <Link
  190. to={`/posts/${post.slug}`}
  191. className="theme-primary font-medium hover:underline focus:outline-none"
  192. >
  193. Read more →
  194. </Link>
  195. </div>
  196. </div>
  197. ))}
  198. </div>
  199. </main>
  200. </div>
  201. </div>
  202. );
  203. }
  204. // Post View Component
  205. function PostView() {
  206. const { slug } = useParams();
  207. const [post, setPost] = useState(null);
  208. const [loading, setLoading] = useState(true);
  209. const [error, setError] = useState(null);
  210. useEffect(() => {
  211. async function fetchPost() {
  212. try {
  213. setLoading(true);
  214. const response = await fetch(`${API_BASE}/posts/${slug}`);
  215. if (!response.ok) throw new Error("Post not found");
  216. const postData = await response.json();
  217. setPost(postData);
  218. // Update document title
  219. document.title = postData.title || "GoonBlog";
  220. } catch (e) {
  221. console.error("Error fetching post:", e);
  222. setError(e.message);
  223. } finally {
  224. setLoading(false);
  225. }
  226. }
  227. if (slug) {
  228. fetchPost();
  229. }
  230. }, [slug]);
  231. useEffect(() => {
  232. if (post) {
  233. const setMeta = (name, content) => {
  234. let element = document.querySelector(`meta[name='${name}']`);
  235. if (!element) {
  236. element = document.createElement("meta");
  237. element.setAttribute("name", name);
  238. document.head.appendChild(element);
  239. }
  240. element.setAttribute("content", content);
  241. };
  242. setMeta("og:title", post.title);
  243. setMeta("og:description", post.description);
  244. setMeta("og:type", "article");
  245. setMeta("og:url", window.location.href);
  246. }
  247. return () => {
  248. const metaTags = [
  249. "og:title",
  250. "og:description",
  251. "og:type",
  252. "og:url",
  253. "og:image",
  254. ];
  255. metaTags.forEach((name) => {
  256. const element = document.querySelector(`meta[name='${name}']`);
  257. if (element) {
  258. element.remove();
  259. }
  260. });
  261. };
  262. }, [post]);
  263. useEffect(() => {
  264. // Reset title when component unmounts
  265. return () => {
  266. document.title = "GoonBlog - A Retard's Thoughts";
  267. };
  268. }, []);
  269. if (loading) {
  270. return (
  271. <div className="min-h-screen theme-bg flex items-center justify-center">
  272. <div className="text-center">
  273. <div className="animate-spin rounded-full h-12 w-12 border-b-2 theme-primary mx-auto"></div>
  274. <p className="mt-4 theme-text-secondary">Loading post...</p>
  275. </div>
  276. </div>
  277. );
  278. }
  279. if (error || !post) {
  280. return (
  281. <div className="min-h-screen theme-bg flex items-center justify-center">
  282. <div className="text-center">
  283. <h2 className="text-2xl font-bold theme-text mb-2">
  284. Post Not Found
  285. </h2>
  286. <p className="theme-text-secondary mb-4">
  287. {error || "The requested post could not be found."}
  288. </p>
  289. <Link
  290. to="/"
  291. className="theme-primary hover:theme-secondary font-medium"
  292. >
  293. ← Back to Home
  294. </Link>
  295. </div>
  296. </div>
  297. );
  298. }
  299. const conceiveFoxFromSemen = (rawMarkdown) => {
  300. let processedText = rawMarkdown;
  301. let tags = null;
  302. let imageCredit = null;
  303. let imageSrc = null;
  304. let imageAlt = null;
  305. let customQuestion = null;
  306. const tagsRegex = /tags: (.*)/;
  307. const tagsMatch = processedText.match(tagsRegex);
  308. if (tagsMatch) {
  309. tags = tagsMatch[1].split(",").map((tag) => tag.trim());
  310. processedText = processedText.replace(tagsRegex, "").trim();
  311. }
  312. const imageRegex = /!\[(.*?)\]\((.*?)\)\n_Image credit: (.*?)_/;
  313. const imageMatch = processedText.match(imageRegex);
  314. if (imageMatch) {
  315. imageAlt = imageMatch[1];
  316. imageSrc = imageMatch[2];
  317. imageCredit = imageMatch[3];
  318. processedText = processedText.replace(imageRegex, "").trim();
  319. }
  320. const questionRegex = /\?\?\? "(.*?)"/;
  321. const questionMatch = processedText.match(questionRegex);
  322. if (questionMatch) {
  323. customQuestion = questionMatch[1];
  324. processedText = processedText.replace(questionRegex, "").trim();
  325. }
  326. processedText = processedText
  327. .replace(/^title:.*$/m, "")
  328. .replace(/^desc:.*$/m, "");
  329. return {
  330. processedText,
  331. tags,
  332. imageSrc,
  333. imageAlt,
  334. imageCredit,
  335. customQuestion,
  336. };
  337. };
  338. const { processedText } = conceiveFoxFromSemen(post.content);
  339. const htmlContent = md.render(processedText);
  340. const sanitizedHtml = DOMPurify.sanitize(htmlContent);
  341. return (
  342. <div className="min-h-screen theme-bg font-sans theme-text antialiased flex flex-col">
  343. <div className="max-w-5xl mx-auto w-full flex-grow">
  344. <NavHeader />
  345. <main className="py-10 px-4 sm:px-6 lg:px-8">
  346. <div className="w-full">
  347. <div className="theme-surface theme-text border theme-border rounded-xl p-8 md:p-12 lg:p-16">
  348. <Link
  349. to="/"
  350. className="theme-text-secondary hover:theme-text transition-colors duration-200 mb-6 flex items-center"
  351. >
  352. ← Back to Home
  353. </Link>
  354. <div className="mb-8">
  355. <h1 className="text-3xl md:text-4xl font-bold theme-text mb-2 leading-tight">
  356. {post.title}
  357. </h1>
  358. <div className="text-lg italic font-light theme-text-secondary">
  359. {post.description}
  360. </div>
  361. </div>
  362. <hr className="theme-border mb-8" />
  363. <div
  364. className="markdown-content theme-text leading-relaxed text-lg"
  365. dangerouslySetInnerHTML={{
  366. __html: sanitizedHtml,
  367. }}
  368. />
  369. </div>
  370. </div>
  371. </main>
  372. </div>
  373. </div>
  374. );
  375. }
  376. function App() {
  377. return (
  378. <Router>
  379. <AuthProvider>
  380. <ThemeProvider>
  381. <Routes>
  382. <Route path="/" element={<BlogHome />} />
  383. <Route path="/posts/:slug" element={<PostView />} />
  384. <Route path="/login" element={<LoginForm />} />
  385. <Route
  386. path="/admin"
  387. element={
  388. <ProtectedRoute>
  389. <AdminDashboard />
  390. </ProtectedRoute>
  391. }
  392. />
  393. <Route
  394. path="/admin/post/new"
  395. element={
  396. <ProtectedRoute>
  397. <PostEditor />
  398. </ProtectedRoute>
  399. }
  400. />
  401. <Route
  402. path="/admin/post/:slug/edit"
  403. element={
  404. <ProtectedRoute>
  405. <PostEditor />
  406. </ProtectedRoute>
  407. }
  408. />
  409. <Route
  410. path="/admin/themes"
  411. element={
  412. <ProtectedRoute>
  413. <ThemesManager />
  414. </ProtectedRoute>
  415. }
  416. />
  417. <Route
  418. path="/admin/themes/new"
  419. element={
  420. <ProtectedRoute>
  421. <ThemeEditor />
  422. </ProtectedRoute>
  423. }
  424. />
  425. <Route
  426. path="/admin/themes/:themeId/edit"
  427. element={
  428. <ProtectedRoute>
  429. <ThemeEditor />
  430. </ProtectedRoute>
  431. }
  432. />
  433. </Routes>
  434. </ThemeProvider>
  435. </AuthProvider>
  436. </Router>
  437. );
  438. }
  439. export default App;