-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
196 additions
and
1,287 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import Link from "next/link"; | ||
|
||
interface MenuItemProps { | ||
offset: number; | ||
title: string; | ||
href: string; | ||
highlighted?: boolean; | ||
} | ||
|
||
export function MenuItem({ title, href, highlighted, offset }: MenuItemProps) { | ||
console.log("highlighted", highlighted); | ||
|
||
return ( | ||
<Link | ||
href={href} | ||
scroll={true} | ||
className={`text-${highlighted ? "violet-600" : "black"} text-lg`} | ||
style={{ paddingLeft: `${offset * 8}px` }} | ||
> | ||
{title} | ||
</Link> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
"use client"; | ||
import { kebabCase } from "@/lib/textIntoKebabNotation"; | ||
import { useState, useEffect } from "react"; | ||
import { MenuItem } from "./MenuItem"; | ||
|
||
type TMenuItem = { | ||
name: string; | ||
id: string; | ||
// 0 || 1 || 2 | ||
offset: number; | ||
startY: number; | ||
endY: number; | ||
}; | ||
|
||
interface PostSearchProps { | ||
mdContent: string; | ||
} | ||
|
||
export const PreviewMenu = ({ mdContent }: PostSearchProps) => { | ||
const menuItems = useCreateMenuItemsFromMdContent(mdContent); | ||
const scrollY = useGetCurrentScrollY(); | ||
|
||
if (menuItems.length === 0) return null; | ||
|
||
return ( | ||
<aside className="sticky top-28 flex h-min w-96 flex-col gap-2 bg-gray-50 px-4 py-3 shadow-md"> | ||
{menuItems?.map(({ id, startY, endY, offset, name }) => ( | ||
<MenuItem | ||
offset={offset} | ||
key={id} | ||
title={name} | ||
href={`#${id}`} | ||
highlighted={isHighlighted(startY, endY, scrollY)} | ||
/> | ||
))} | ||
</aside> | ||
); | ||
}; | ||
|
||
function useCreateMenuItemsFromMdContent(mdContent: string): TMenuItem[] { | ||
const [menuItems, setMenuItems] = useState<TMenuItem[]>([]); | ||
|
||
const regex = /^(#{1,3}) (.*)$/gm; | ||
|
||
let match; | ||
|
||
useEffect(() => { | ||
const _menuItems: TMenuItem[] = []; | ||
|
||
while ((match = regex.exec(mdContent)) !== null) { | ||
const offset = match[1]!.length - 1; | ||
const name = match[2]!; | ||
const id = kebabCase(name); | ||
|
||
const headerItem = document.querySelector(`#${id}`)!; | ||
|
||
const clientRect = headerItem?.getBoundingClientRect(); | ||
_menuItems.push({ | ||
startY: clientRect.top, | ||
endY: 0, | ||
offset, | ||
name, | ||
id: id!, | ||
}); | ||
} | ||
|
||
if (_menuItems.length === 0) return; | ||
|
||
// @ts-expect-error: types suck | ||
_menuItems[0].startY = 0; | ||
|
||
// @ts-expect-error: types suck | ||
_menuItems[_menuItems.length - 1].endY = Infinity; | ||
|
||
for (let i = 0; i < _menuItems.length - 1; i++) { | ||
// @ts-expect-error: types suck | ||
_menuItems[i].endY = _menuItems[i + 1].startY; | ||
} | ||
|
||
setMenuItems(_menuItems); | ||
}, [mdContent]); | ||
|
||
return menuItems; | ||
} | ||
|
||
function useGetCurrentScrollY() { | ||
const scrollOffset = 0; | ||
const [currentPosition, setCurrentPosition] = useState<number>(0); | ||
|
||
useEffect(() => { | ||
const handleScroll = () => { | ||
setCurrentPosition(window.scrollY + scrollOffset); | ||
}; | ||
|
||
window.addEventListener("scroll", handleScroll); | ||
|
||
return () => { | ||
window.removeEventListener("scroll", handleScroll); | ||
}; | ||
}, []); | ||
|
||
return currentPosition; | ||
} | ||
|
||
function isHighlighted(startY: number, endY: number, scrollY: number) { | ||
return scrollY > startY && scrollY < endY; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { api } from "@/trpc/server"; | ||
import { notFound } from "next/navigation"; | ||
import { MdPreview } from "@/components/mdPreview"; | ||
import { PreviewMenu } from "@/app/posts/[postId]/_components/PreviewMenu"; | ||
|
||
export default async function Page(props: { params: { postId: string } }) { | ||
const postId = props.params.postId; | ||
|
||
const { post } = await api.post.getPostById.query({ postId: Number(postId) }); | ||
|
||
if (!post) return notFound(); | ||
|
||
return ( | ||
<main className="flex flex-col items-center py-20"> | ||
<div className="max-lg:w-[95%] lg:w-[1200px]"> | ||
<section className="relative flex w-full gap-10"> | ||
<MdPreview | ||
className="w-[70%]" | ||
content={post.content!} | ||
title={post.title!} | ||
/> | ||
<PreviewMenu mdContent={post.content!} /> | ||
</section> | ||
|
||
<section>Comments gonna be here</section> | ||
</div> | ||
</main> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ | |
|
||
@layer base { | ||
:root { | ||
scroll-behavior: smooth; | ||
|
||
--background: 0 0% 100%; | ||
--foreground: 222.2 84% 4.9%; | ||
|
||
|