diff --git a/src/data/blogPostsData.ts b/src/data/blogPostsData.ts index 98e3683..ee437ef 100644 --- a/src/data/blogPostsData.ts +++ b/src/data/blogPostsData.ts @@ -19,7 +19,7 @@ export const blogPostsData: BlogPostProps[] = [ The realm of IoT demands reliable, efficient communication protocols that can withstand the chaotic nature of real-world environments. Through careful implementation and optimization of the STM32's communication peripherals, we can craft resilient connections that bridge the gap between digital and physical domains. - ## The Three Elemental Protocols: I2C, SPI, and UART + # The Three Elemental Protocols: I2C, SPI, and UART Before we forge ahead into advanced territory, let us briefly recall the elemental forces at our disposal. Each protocol serves as a channel through which our digital entities communicate, each with its unique strengths and limitations: * **I2C (Inter-Integrated Circuit)** - A two-wire interface requiring minimal physical connections, ideal for communication with multiple peripheral devices over short distances. diff --git a/src/pages/BlogPost.tsx b/src/pages/BlogPost.tsx index f5cc3cd..5be2b7c 100644 --- a/src/pages/BlogPost.tsx +++ b/src/pages/BlogPost.tsx @@ -5,6 +5,13 @@ import { NotFound } from './NotFound'; import style from '@styles/BlogPost.module.scss'; +import { get } from 'http'; + +interface TableOfContentsItem { + title: string; + id: number; + children?: TableOfContentsItem[]; +} function dedent(str: string): string { const lines = str.split('\n'); @@ -27,10 +34,13 @@ export const BlogPost = () => { const { slug } = useParams<{ slug: string }>(); const [post, setPost] = useState(blogPostsData.find(post => post.slug === slug)); const [relatedPosts, setRelatedPosts] = useState(blogPostsData.slice(0, 3)); + const [toc, setToc] = useState([]); useEffect(() => { const currentPost = blogPostsData.find(post => post.slug === slug); - + let id = 0; + const toc: TableOfContentsItem[] = []; + if (currentPost) { setPost(currentPost); @@ -45,6 +55,85 @@ export const BlogPost = () => { setRelatedPosts(related); + let newContent = ''; + if (currentPost.content) { + const content = dedent(currentPost.content); + const lines = content.split('\n'); + for (let i = 0; i < lines.length; i++) { + if (/^#\s/.test(lines[i])) { + newContent += `

${lines[i].replace(/^#+\s*/, '')}

\n`; + toc.push({ title: lines[i].replace(/^#+\s*/, ''), id }); + id++; + } + else if (/^##\s/.test(lines[i])) { + newContent += `

${lines[i].replace(/^#+\s*/, '')}

\n`; + if (toc.length > 0) { + const parent = toc[toc.length - 1]; + if (!parent.children) { + parent.children = []; + } + parent.children.push({ title: lines[i].replace(/^#+\s*/, ''), id }); + } + id++; + } + else if (/^###\s/.test(lines[i])) { + newContent += `

${lines[i].replace(/^#+\s*/, '')}

\n`; + if (toc.length > 0) { + const h2Parent = toc[toc.length - 1]; + if (h2Parent.children && h2Parent.children.length > 0) { + const h3Parent = h2Parent.children[h2Parent.children.length - 1]; + if (!h3Parent.children) { + h3Parent.children = []; + } + h3Parent.children.push({ title: lines[i].replace(/^#+\s*/, ''), id }); + } + } + id++; + } + else if (/^\*\s/.test(lines[i])) { + newContent += '\n'; + } + else if (/^\-\s/.test(lines[i])) { + newContent += '
    \n'; + + for (; i < lines.length; i++) { + if (/^\-\s/.test(lines[i])) { + newContent += `
  1. ${lines[i].replace(/^\-\s/, '')}
  2. \n`; + } + else { + break; + } + } + newContent += '
\n'; + } + else { + newContent += `

${lines[i]}

\n`; + } + } + } + + newContent = newContent.replace(/\*\*(.*?)\*\*/g, '$1'); + newContent = newContent.replace(/__(.*?)__/g, '$1'); + newContent = newContent.replace(/\*(.*?)\*/g, '$1'); + newContent = newContent.replace(/_(.*?)_/g, '$1'); + newContent = newContent.replace(/`(.*?)`/g, '$1'); + newContent = newContent.replace(/!\[(.*?)\]\((.*?)\)/g, '$1'); + newContent = newContent.replace(/\[(.*?)\]\((.*?)\)/g, '$1'); + + const contentElement = document.getElementById('postContent'); + contentElement!.innerHTML = newContent; + setToc(toc); + window.scrollTo(0, 0); } }, [slug]); @@ -86,7 +175,8 @@ export const BlogPost = () => { -
+
+
{/* Post Tags */}