Skip to content

tranquilsoftware/beautiful-react-markdown-renderer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🎨 Beautiful React Markdown Renderer

A powerful, feature-rich React component specifically designed to create beautiful blog posts in markdown format with a strong focus on SEO. Transform your markdown content into stunning, search-engine optimised (SEO) articles with comprehensive metadata extraction and beautiful styling.

✨ Features

  • πŸ“ YAML Frontmatter Support: Extract SEO metadata directly from markdown files
  • πŸ–ΌοΈ Rich Media Components: Custom markdown syntax for images, videos, galleries, and iframes
  • πŸ“Š Mermaid Diagrams: Beautiful flowcharts and diagrams with SVG rendering
  • πŸ”’ LaTeX Math Support: Inline and block math equations with KaTeX
  • 🎯 Table of Contents: Auto-generated navigation with smooth scrolling
  • πŸŒ™ Dark/Light Theme: Built-in theme switching with system preference detection
  • πŸ“± Responsive Design: Mobile-first design with Tailwind CSS
  • ⚑ Performance Optimised: Efficient rendering with React hooks and context
  • 🎨 Customisable: Easy to extend with custom components and styling
  • 🎨 Tailwind CSS v4: Modern styling with Tailwind CSS v4

πŸš€ Quick Start

Installation

# Clone the repository
git clone <your-repo-url>
cd beautiful-react-markdown-renderer

# Install dependencies
npm install

# Install math and diagram support
npm install remark-math rehype-katex katex

# Start development server
npm run dev

Basic Usage

import { MarkdownRenderer } from './src/components/markdown/MarkdownRenderer';

const markdownContent = `
---
title: "My Blog Post"
description: "A comprehensive guide to markdown rendering"
author: "John Doe"
publishedDate: "2024-01-15"
tags: ["markdown", "react", "blog"]
---

# Welcome to My Blog Post

This is a **beautiful** markdown renderer with custom components!

## Math Support

Inline math: $E = mc^2$

Block math:
$$
\\sum_{i=1}^{n} i = \\frac{n(n+1)}{2}
$$

## Mermaid Diagrams

\`\`\`mermaid
graph TD
    A[Start] --> B[Process]
    B --> C[Decision]
    C --> D[End]
\`\`\`

<img src="https://example.com/image.jpg" alt="Example image" />

<youtube id="dQw4w9WgXcQ" title="Example Video" />
`;

function App() {
  return (
    <MarkdownRenderer 
      content={markdownContent} 
      showToc={true}
      enableMath={true}
    />
  );
}

πŸ“ YAML Frontmatter

Add metadata to your markdown files using YAML frontmatter:

---
title: "Understanding Biblical Archaeology"
description: "A comprehensive overview of archaeological evidence supporting biblical narratives"
author: "Dr. Sarah Johnson"
publishedDate: "2024-01-15"
modifiedDate: "2024-01-20"
readTime: "8 min read"
category: "Archaeology"
tags: ["archaeology", "bible", "evidence", "history"]
image: "https://example.com/archaeology-image.jpg"
featured: true
draft: false
---

# Your content starts here...

Available Frontmatter Fields

Field Type Description
title String Post title for SEO and display
description String Meta description (under 160 chars)
author String Post author name
publishedDate String Publication date (YYYY-MM-DD)
modifiedDate String Last modification date (YYYY-MM-DD)
readTime String Estimated reading time
category String Post category
tags Array Array of tags for filtering
image String Featured image URL
featured Boolean Whether to feature this post
draft Boolean Whether this is a draft

πŸ”’ Math Rendering

Inline Math

Use single dollar signs for inline equations:

Einstein's famous equation: $E = mc^2$

The quadratic formula: $x = \frac{-b \pm \sqrt{b^2-4ac}}{2a}$

Block Math

Use double dollar signs for display equations:

$$
\sum_{i=1}^{n} i = \frac{n(n+1)}{2}
$$

$$
\int_{-\infty}^{\infty} e^{-x^2} dx = \sqrt{\pi}
$$

Features:

  • Powered by KaTeX for fast, beautiful rendering
  • Supports full LaTeX math syntax
  • Dark mode compatible
  • Responsive and mobile-friendly
  • Custom styled containers with gradient backgrounds

πŸ“Š Mermaid Diagrams

Create beautiful flowcharts and diagrams using Mermaid syntax:

\`\`\`mermaid
graph TD
    A[Start] --> B[Process Data]
    B --> C{Decision}
    C -->|Yes| D[Execute]
    C -->|No| E[Skip]
    D --> F[End]
    E --> F
\`\`\`

Supported Formats:

  • Flowcharts (graph TD, graph LR)
  • More diagram types render with fallback styling

Features:

  • Custom SVG rendering with beautiful gradients
  • Interactive hover effects
  • Dark mode support
  • Curved connectors with arrows
  • Node labels and connection labels
  • Responsive layout

🎨 Custom Markdown Components

1. Images with Captions

<img src="https://placehold.co/600x400/png" alt="Tranquil Software" />

Features:

  • Responsive width (w-full)
  • Automatic height (h-auto)
  • Rounded corners (rounded-lg)
  • Caption display below image
  • Theme-aware styling

2. YouTube Videos

<youtube id="dQw4w9WgXcQ" title="Rick Astley - Never Gonna Give You Up" />

Features:

  • Responsive 16:9 aspect ratio
  • Full YouTube embed with controls
  • Rounded corners container
  • Fullscreen support enabled

3. Iframe Embeds (Google Maps, etc.)

<iframe src="https://www.google.com/maps/embed?KEYS" title="Tranquil Software" />

Features:

  • Fixed height (h-96)
  • Full width responsive
  • Rounded corners
  • Fullscreen support

4. Image Galleries

<imagegallery 
  title="My Photo Collection" 
  images='[{"src": "https://placehold.co/600x400/png", "alt": "Tranquil Software"}]' 
/>

Advanced Gallery Options

<imagegallery 
  title="Archaeological Findings" 
  images='[
    {"src": "https://placehold.co/600x400/png", "alt": "Tranquil Software"},
    {"src": "https://placehold.co/600x400/png", "alt": "Tranquil Software"}
  ]' 
  fullWidth={true}
  centerThumbnails={true}
  thumbnailOptions='{"width": 300, "height": 200, "borderRadius": 8, "gap": 16}'
/>

Gallery Props

Prop Type Default Description
title String "" Gallery title
images Array [] Array of image objects with src and alt
fullWidth Boolean false Whether gallery should take full width
centerThumbnails Boolean false Whether to center thumbnails
thumbnailOptions Object {...} Thumbnail customization options

Thumbnail Options

Option Type Default Description
width Number 200 Thumbnail width
height Number 150 Thumbnail height
borderRadius Number 8 Corner radius
gap Number 12 Spacing between thumbnails

πŸ—οΈ Component Architecture

Core Components

MarkdownRenderer (Main entry point)
β”œβ”€β”€ MarkdownProvider (Context provider)
β”œβ”€β”€ MarkdownContentWrapper (Layout + TOC)
β”‚   β”œβ”€β”€ TableOfContents (Navigation)
β”‚   └── MarkdownContent (ReactMarkdown wrapper)
β”‚       β”œβ”€β”€ Headings (h1-h6 with TOC registration)
β”‚       β”œβ”€β”€ Text (paragraphs, links, emphasis)
β”‚       β”œβ”€β”€ Lists (ul, ol, li)
β”‚       β”œβ”€β”€ CodeBlock (syntax highlighting)
β”‚       β”œβ”€β”€ Table (responsive tables)
β”‚       β”œβ”€β”€ Media (custom components)
β”‚       β”œβ”€β”€ MermaidDiagram (flowchart rendering)
β”‚       └── Math (LaTeX equation rendering)
└── ThemeContext (Dark/Light mode)

Supporting Components

  • ThemeContext.tsx: Manages dark/light theme state
  • useToc.ts: Hook for table of contents functionality
  • markdownParser.ts: Parses YAML frontmatter and extracts metadata
  • SEO.tsx: Handles meta tags and SEO optimization
  • MermaidDiagram.tsx: Custom SVG flowchart renderer
  • Math.tsx: LaTeX math equation renderer with KaTeX

πŸ”„ Data Flow

Markdown File (.md)
    ↓
markdownParser.ts (extracts metadata + content)
    ↓
[slug].tsx (loads post data + metadata)
    ↓
BlogPost.tsx (wraps with ReadMeter)
    ↓
MarkdownRenderer.tsx (renders markdown)
    ↓
Remark Plugins (remarkGfm, remarkMath)
    ↓
Rehype Plugins (rehypeRaw, rehypeHighlight, rehypeKatex)
    ↓
Individual Components (Headings, Text, Lists, Math, Mermaid, etc.)
    ↓
Final Rendered HTML + CSS + Theme

🎨 Styling System

Color System

// colors.ts
export const textColors = {
  primary: 'text-gray-900 dark:text-gray-100',
  secondary: 'text-gray-600 dark:text-gray-400',
  muted: 'text-gray-500 dark:text-gray-500',
  // ... more colors
};

export const bgColors = {
  primary: 'bg-white dark:bg-gray-900',
  secondary: 'bg-gray-50 dark:bg-gray-800',
  // ... more colors
};

CSS Structure

src/
β”œβ”€β”€ components/
β”‚   β”œβ”€β”€ markdown/
β”‚   β”‚   β”œβ”€β”€ MarkdownRenderer.tsx
β”‚   β”‚   β”œβ”€β”€ components/
β”‚   β”‚   β”‚   β”œβ”€β”€ Headings.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ Text.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ Lists.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ CodeBlock.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ Table.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ Media.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ MermaidDiagram.tsx
β”‚   β”‚   β”‚   └── Math.tsx
β”‚   β”‚   β”œβ”€β”€ context/
β”‚   β”‚   β”‚   └── ThemeContext.tsx
β”‚   β”‚   β”œβ”€β”€ hooks/
β”‚   β”‚   β”‚   └── useToc.ts
β”‚   β”‚   └── markdownParser.ts
β”‚   └── blog/
β”‚       β”œβ”€β”€ BlogPage.tsx
β”‚       β”œβ”€β”€ BlogPost.tsx
β”‚       └── [slug].tsx
β”œβ”€β”€ lib/
β”‚   β”œβ”€β”€ utils.ts
β”‚   └── colors.ts
└── styles/
    └── markdown.css

πŸ› οΈ Development

Installation

To enable math support in your markdown, install the following dependencies:

npm install remark-math rehype-katex katex
npm install --save-dev @types/katex

Project Structure

beautiful-react-markdown-renderer/
β”œβ”€β”€ public/
β”‚   β”œβ”€β”€ assets/
β”‚   β”œβ”€β”€ favicon/
β”‚   └── posts/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ components/
β”‚   β”œβ”€β”€ data/
β”‚   β”œβ”€β”€ lib/
β”‚   └── pages/
β”œβ”€β”€ index.html
β”œβ”€β”€ package.json
β”œβ”€β”€ tailwind.config.js
β”œβ”€β”€ tsconfig.json
└── vite.config.ts

πŸ“š API Reference

MarkdownRenderer Props

Prop Type Default Description
content String "" Markdown content to render
showToc Boolean false Whether to show table of contents
className String "" Additional CSS classes
enableMath Boolean true Enable LaTeX math rendering

useToc Hook

const { registerHeading, activeHeading } = useToc();

parseMarkdownWithFrontmatter

const { metadata, content } = parseMarkdownWithFrontmatter(markdown);

πŸ”Œ Plugins & Dependencies

Remark Plugins (Markdown Processing)

  • remarkGfm: GitHub Flavored Markdown (tables, strikethrough, etc.)
  • remarkMath: Parses LaTeX math syntax ($...$ and $$...$$)

Rehype Plugins (HTML Processing)

  • rehypeRaw: Allows raw HTML in markdown
  • rehypeHighlight: Syntax highlighting for code blocks
  • rehypeKatex: Renders LaTeX math equations with KaTeX

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

Free forever. Tranquil Software certified. ❀️

πŸ™ Acknowledgments


Made with ❀️ for beautiful markdown rendering by Tranquil Software

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published