|
1 | 1 | import React, {useState, useEffect, useContext} from "react"; |
| 2 | +import ReactMarkdown from "react-markdown"; |
2 | 3 | import "./Blog.scss"; |
3 | | -import BlogCard from "../../components/blogCard/BlogCard"; |
4 | | -import {blogSection} from "../../portfolio"; |
5 | 4 | import {Fade} from "react-reveal"; |
6 | 5 | import StyleContext from "../../contexts/StyleContext"; |
| 6 | +import blogManifest from "../../blogs/manifest"; |
| 7 | + |
7 | 8 | export default function Blogs() { |
8 | 9 | const {isDark} = useContext(StyleContext); |
9 | | - const [mediumBlogs, setMediumBlogs] = useState([]); |
10 | | - function setMediumBlogsFunction(array) { |
11 | | - setMediumBlogs(array); |
12 | | - } |
13 | | - //Medium API returns blogs' content in HTML format. Below function extracts blogs' text content within paragraph tags |
14 | | - function extractTextContent(html) { |
15 | | - return typeof html === "string" |
16 | | - ? html |
17 | | - .split("p>") |
18 | | - .filter(el => !el.includes(">")) |
19 | | - .map(el => el.replace("</", ".").replace("<", "")) |
20 | | - .join(" ") |
21 | | - : NaN; |
22 | | - } |
| 10 | + const [selectedPost, setSelectedPost] = useState(null); |
| 11 | + const [markdownContent, setMarkdownContent] = useState(""); |
| 12 | + const [loading, setLoading] = useState(false); |
| 13 | + |
23 | 14 | useEffect(() => { |
24 | | - if (blogSection.displayMediumBlogs === "true") { |
25 | | - const getProfileData = () => { |
26 | | - fetch("/blogs.json") |
27 | | - .then(result => { |
28 | | - if (result.ok) { |
29 | | - return result.json(); |
30 | | - } |
31 | | - }) |
32 | | - .then(response => { |
33 | | - setMediumBlogsFunction(response.items); |
34 | | - }) |
35 | | - .catch(function (error) { |
36 | | - console.error( |
37 | | - `${error} (because of this error Blogs section could not be displayed. Blogs section has reverted to default)` |
38 | | - ); |
39 | | - setMediumBlogsFunction("Error"); |
40 | | - blogSection.displayMediumBlogs = "false"; |
41 | | - }); |
42 | | - }; |
43 | | - getProfileData(); |
44 | | - } |
45 | | - }, []); |
46 | | - if (!blogSection.display) { |
47 | | - return null; |
| 15 | + if (!selectedPost) return; |
| 16 | + setLoading(true); |
| 17 | + fetch(selectedPost.file) |
| 18 | + .then(res => res.text()) |
| 19 | + .then(text => { |
| 20 | + // Strip YAML frontmatter before rendering |
| 21 | + const stripped = text.replace(/^---[\s\S]*?---\n/, ""); |
| 22 | + setMarkdownContent(stripped); |
| 23 | + setLoading(false); |
| 24 | + }) |
| 25 | + .catch(() => { |
| 26 | + setMarkdownContent("Failed to load post."); |
| 27 | + setLoading(false); |
| 28 | + }); |
| 29 | + }, [selectedPost]); |
| 30 | + |
| 31 | + if (selectedPost) { |
| 32 | + return ( |
| 33 | + <Fade bottom duration={600} distance="20px"> |
| 34 | + <div className="main" id="blogs"> |
| 35 | + <button |
| 36 | + className={isDark ? "blog-back-btn dark-mode" : "blog-back-btn"} |
| 37 | + onClick={() => setSelectedPost(null)} |
| 38 | + > |
| 39 | + ← Back |
| 40 | + </button> |
| 41 | + <div className={isDark ? "blog-post-content dark-mode" : "blog-post-content"}> |
| 42 | + <p className="blog-post-meta">{selectedPost.date}</p> |
| 43 | + <h1 className="blog-post-title">{selectedPost.title}</h1> |
| 44 | + {loading ? ( |
| 45 | + <p>Loading...</p> |
| 46 | + ) : ( |
| 47 | + <ReactMarkdown>{markdownContent}</ReactMarkdown> |
| 48 | + )} |
| 49 | + </div> |
| 50 | + </div> |
| 51 | + </Fade> |
| 52 | + ); |
48 | 53 | } |
| 54 | + |
49 | 55 | return ( |
50 | 56 | <Fade bottom duration={1000} distance="20px"> |
51 | 57 | <div className="main" id="blogs"> |
52 | 58 | <div className="blog-header"> |
53 | | - <h1 className="blog-header-text">{blogSection.title}</h1> |
54 | | - <p |
55 | | - className={ |
56 | | - isDark ? "dark-mode blog-subtitle" : "subTitle blog-subtitle" |
57 | | - } |
58 | | - > |
59 | | - {blogSection.subtitle} |
| 59 | + <h1 className="blog-header-text">Writing</h1> |
| 60 | + <p className={isDark ? "dark-mode blog-subtitle" : "subTitle blog-subtitle"}> |
| 61 | + Notes from the trenches |
60 | 62 | </p> |
61 | 63 | </div> |
62 | | - <div className="blog-main-div"> |
63 | | - <div className="blog-text-div"> |
64 | | - {blogSection.displayMediumBlogs !== "true" || |
65 | | - mediumBlogs === "Error" |
66 | | - ? blogSection.blogs.map((blog, i) => { |
67 | | - return ( |
68 | | - <BlogCard |
69 | | - key={i} |
70 | | - isDark={isDark} |
71 | | - blog={{ |
72 | | - url: blog.url, |
73 | | - image: blog.image, |
74 | | - title: blog.title, |
75 | | - description: blog.description |
76 | | - }} |
77 | | - /> |
78 | | - ); |
79 | | - }) |
80 | | - : mediumBlogs.map((blog, i) => { |
81 | | - return ( |
82 | | - <BlogCard |
83 | | - key={i} |
84 | | - isDark={isDark} |
85 | | - blog={{ |
86 | | - url: blog.link, |
87 | | - title: blog.title, |
88 | | - description: extractTextContent(blog.content) |
89 | | - }} |
90 | | - /> |
91 | | - ); |
92 | | - })} |
93 | | - </div> |
| 64 | + <div className="blog-text-div"> |
| 65 | + {blogManifest.map((post, i) => ( |
| 66 | + <div |
| 67 | + key={i} |
| 68 | + className={isDark ? "blog-card dark-mode" : "blog-card"} |
| 69 | + onClick={() => setSelectedPost(post)} |
| 70 | + > |
| 71 | + <p className="blog-card-date">{post.date}</p> |
| 72 | + <h2 className="blog-card-title">{post.title}</h2> |
| 73 | + <p className="blog-card-excerpt">{post.excerpt}</p> |
| 74 | + <span className="blog-card-read">Read →</span> |
| 75 | + </div> |
| 76 | + ))} |
94 | 77 | </div> |
95 | 78 | </div> |
96 | 79 | </Fade> |
|
0 commit comments