02-22-2025
Next.js is a web app development framework built for the frontend UI framework called React. Leveraging the newest app router, I created this blogging system.
This post is not a tutorial but rather an overview of how I created a blogging system using Next.js.
In my Next.js application, I am using Static Site Generation (SSG), which dynamically generates websites based on predefined conditions. A Static website is a website that isn't dynamic, which means that the website is a simple document like a blog post. Making payments or pressing a button and something happening makes a website dynamic.
Here's how it works:
posts that contains Markdown files, with each file representing an individual blog post.In the new app router by Next.js, the app directory contains subdirectories that represent different pages of your app. In my app, one of these subdirectories is named posts (for blog posts, different from the posts directory containing Markdown files). This posts subdirectory is where static sites (blog posts) are generated programmatically.
The script has three main functions:
getSortedPostsData()Reads the Markdown files directory, processes each Markdown file to extract metadata (information like blog Title and Date), and sorts the metadata based on the date it was created.
getAllPostIds()Reads the Markdown files directory and gives a structure containing the ids of all blog files (file name without extension).
getPostData()Reads a specific Markdown file, processes its content to convert it to HTML, and gives the content based on the id.
These functions are used to create blog posts dynamically. Specifically, the getAllPostIds() and getPostData() are used by the static website generator to generate individual blog posts. I write my blogs as Markdown files, and each file is then read, and its content is converted into HTML using remark and remark-rehype.
You can find the source code to this blogging system here.
Given an array of strings strs, group all anagrams together into sublists. You may return the output in any order.
An anagram is a string that contains the exact same characters as another string, but the order of the characters can be different.
So, how I like to start solving problems is like this:
First, make the problem clear by asking: What is this problem asking?
So, What?
We are given an array of strings and need to group all anagrams within that array into subarrays.
Once the problem is clear, we move to the next step: How do we solve it?
To solve this problem, we need a structured approach. This is where pseudocode comes in handy.
A hash map allows us to store values in key-value pairs, which is perfect for grouping anagrams:
hashMap = {
key: [],
key: [],
key: []
}
Each key will be a sorted version of a word, and the corresponding value will be an array of words that match that sorted key.
A key observation:
If two words are anagrams, their sorted versions will be the same.
"eat" → "aet"
"tea" → "aet"
"tan" → "ant"
Using this, we can group words by their sorted version.
To sort a string in JavaScript, we use:
sortedStr = str.split('').sort().join('');
split(''): Converts the string into an array of characters.sort(): Sorts the array alphabetically.join(''): Joins the sorted characters back into a string.This gives us a unique key for each group of anagrams.
function groupedAnagrams(strs) {
let hashMap = {};
for (let str of strs) {
let sortedString = str.split('').sort().join('');
if (!hashMap[sortedString]) {
hashMap[sortedString] = [];
}
hashMap[sortedString].push(str);
}
return Object.values(hashMap); // returns an array of values of the object
}
strs, sorting each string.Object.values(hashMap), which extracts the grouped anagram arrays.split(), sort(), and join() to manipulate strings.This method ensures optimal efficiency and a clean solution for grouping anagrams.