You are currently reading about it within it. 😂
Challenges Faced
- Manage content efficiently to streamline operations.
- Implement MDX integration for dynamic content rendering.
- Customize code blocks in MDX to enhance readability:
- Add syntax highlighting for improved code clarity.
- Include a Copy to Clipboard feature for easy code replication.
- Display line numbers for easier navigation.
- Highlight specific lines and words to draw attention to key parts.
- Implement a feature to increment views without requiring user authentication.
Technologies Used
Solutions
Here is the explanation of how I tackled these challenges.
Manage content efficiently
To address the challenge of managing content efficiently, I opted for Contentlayer, a powerful content management solution. Here's how I approached this
- Content Structuring with Contentlayer
- Leveraged Contentlayer to define a structured content model.
- Created a
contentlayer.config.ts
file to define document types likeProject
for easy content organization.
- Efficient Content Retrieval
- Used Contentlayer feature to efficiently get and retrieve content data.
- Implemented
generateStaticParams
in Next.js for static site generation, ensuring optimal performance.
- Content Management Workflow
- Organized content files in a directory structure (e.g.,
content/projects
) for easy management. - Regularly updated content files to reflect the latest projects and achievements.
- Organized content files in a directory structure (e.g.,
MDX integration
To address the challenge of dynamic content rendering, especially for projects and blog posts, I successfully integrated MDX into my Next.js project. Here's how I approached this
-
Setting Up MDX in Next.js
-
Installed the necessary packages for MDX integration
Terminal -
Configured Next.js to recognize MDX files by updating the
next.config.mjs
file.example.js
-
-
Dynamic Page Creation with MDX
- Created individual pages for projects and blog posts using the
[slug].tsx
convention. - Dynamically get and rendered MDX content for each page using Next.js
generateStaticParams
functions.
- Created individual pages for projects and blog posts using the
Customize Code Blocks
To enhance the readability of code blocks in MDX, I implemented several features
Syntax Highlighting
I utilized the rehype-pretty-code
plugin to add syntax highlighting to code blocks. This not only improves code clarity but also provides a visually appealing presentation.
Copy to Clipboard
I utilized the react-children-utilities
to copy text inside react tree.
add copy-button to code blocks
Display Line Numbers
rehype-pretty-code
does not provide a straightforward way to display line numbers. Here's how I achieve that.
when code blocks have showLineNumbers props rehype-pretty-code
add data-line-numbers data attribute to code tag and data-line to each line.
Now i can add line numbers based on that using CSS increment feature. :not(span)
to don't apply style to inline code blocks
Highlight Lines and Words
I added the .line--highlighted
class to the highlighted line and the .word--highlighted
class to the highlighted words using rehype-pretty-code
's onVisitHighlightedLine
and onVisitHighlightedChars
methods.
I can now style lines and words easily based on class names.
:not(.word--highlighted) > .word--highlighted
This selector ensures that overlaid highlighted words do not have the style.
Increment Views
To increase view counts without requiring user authentication, I utilized Redis as a caching mechanism. Whenever a user visits a project or blog post, the view count is incremented and stored in Redis using the hashed ID and post or blog slug.
I use the ReportView component to increase the view count for specific projects or blogs. This approach allows me to easily integrate the component within server components without the need to render the entire component on the client side.
Here's how the ReportView component is being used.
Results
- Streamline content management by implementing Contentlayer to organize structured content.
- Increased the overall flexibility of the portfolio by successfully integrating MDX for dynamic content rendering.
- Code readability has been improved through the use of syntax highlighting, a 'Copy to Clipboard' button, line numbers, and the ability to highlight specific lines and words.
- Utilized Redis to provide insight into the popularity of projects and blog posts by incrementing views without requiring user authentication.
- Achieved a responsive portfolio with animations that enhance the user experience.
Feel free to reach out if you have any questions or suggestions!