SVG Rendering Plugin Demo
This demo shows three ways to use SVGs in your blog posts:
- Inline SVG - Write SVG code directly in markdown
- Imported SVG - Import from external files and reuse across posts
- Styled imports - Apply CSS classes and dimensions to imported SVGs
🎯 NEW: Importing SVG Constants
You can now import SVG constants from external files to save space and reuse graphics!
Import Syntax
At the top of your MDX file, import SvgWrapper component
import { SvgWrapper } from "@/react/Svg/SvgWrapper.jsx";// or use backward compatible importimport Tabs, { TabItem } from "../../react/Svg/SvgWrapper.jsx";Then import the SVG data as below:
import { HOUSE } from "@/data/svg/house.ts";import { CHECK_ICON } from "@/data/svg/icons.ts";Then use in your content:
<SvgWrapper svg={HOUSE} /><SvgWrapper svg={CHECK_ICON} className="pulse-animation" width="100px" /><SvgWrapper svg={HEART_ICON} caption="Optional caption text" />NEW: Optional Caption
Both SvgWrapper and Base64Image components now support an optional caption prop:
<SvgWrapper svg={HOUSE} caption="A simple house icon" /><Base64Image src={MY_IMAGE} caption="Beautiful landscape" />The caption appears below the image/SVG with muted, italicized text.
Example 1: Imported House
Example 2: Imported Check Icon with Caption
Success indicator icon
Example 3: Imported Heart with Animation
Example 4: Imported Warning Icon
Example 5: Imported Info Icon with Styling and Caption
Information icon with shadow and border effects
Example 6: Imported Star with Hover Effect
Example 7: Imported Circle with Pulse and Caption
Animated circle with pulse effect
Example 8: Imported Gradient Rectangle
Example 9: Imported Smiley with Bounce
🖼️ NEW: Base64 Image Component!
You can import and display base64 encoded images with optional lazy loading!
Base64 Image Import Syntax
At the top of your MDX file, import Base64Image component
import { Base64Image } from "@/react/Image/Base64Image.jsx";// or use backward compatible importimport Tabs, { TabItem } from "../../react/Image/Base64Image.jsx";Then import the Image data as below:
import { DEMO_IMAGE, SAMPLE_LANDSCAPE } from "@/data/images/base64-images.ts";Usage Options
Without lazy loading (instant display):
<Base64Image src={DEMO_IMAGE} alt="Demo" width="300px" />With lazy loading (shows spinner, requires client:load):
<Base64Image src={SAMPLE_LANDSCAPE} alt="Landscape" width="400px" lazy={true} client:load />With caption:
<Base64Image src={DEMO_IMAGE} alt="Demo" width="300px" caption="A beautiful demonstration image" />⚠️ IMPORTANT: When using lazy={true}, you MUST add client:load directive for React hydration!
Props
src- Base64 image string (required)alt- Alt textclassName- CSS classes (with-shadow, bordered, etc.)width/height- Custom dimensionslazy- Enable lazy loading (default:false)loadingDelay- Delay in ms (default: 1500, only with lazy)caption- Optional caption text shown below image
Note: Images are center-aligned by default. Use className="left" or className="right" to change alignment.
Example 11: Basic Base64 Image with Caption
Demo image showing instant display (no lazy loading)
Example 12: Base64 Image with Shadow and Caption
Beautiful landscape with shadow effect
Example 13: With Lazy Loading + Spinner + Caption (Requires client:load)
Portrait with lazy loading and border
Example 14: Left Aligned Image
Example 15: Multiple Base64 Images (No Lazy)
Example 16: Lazy Loading with Custom Delay and Caption (2 seconds)
Landscape with 2-second loading delay for demonstration
Example 17: Inline SVG (Traditional Method)
Here’s a basic SVG circle using code block syntax:
svg {class="bordered"}<svg width="120" height="120" xmlns="http://www.w3.org/2000/svg"> <circle cx="60" cy="60" r="50" fill="#ff6b6b" stroke="#c92a2a" stroke-width="3"/></svg>Example 2: Star with Custom Class
Here’s a star with custom width:
svg {width="150px"}<svg viewBox="0 0 51 48" xmlns="http://www.w3.org/2000/svg"> <path d="M25.5 0L31.5 18.5H51L35.5 30L41.5 48.5L25.5 37L9.5 48.5L15.5 30L0 18.5H19.5L25.5 0Z" fill="gold" stroke="#ff8800" stroke-width="1.5"/></svg>Example 3: Gradient Rectangle
Beautiful gradient effect:
<svg width="300" height="150" xmlns="http://www.w3.org/2000/svg"> <defs> <linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="100%"> <stop offset="0%" style="stop-color:rgb(255,95,109);stop-opacity:1" /> <stop offset="50%" style="stop-color:rgb(255,195,113);stop-opacity:1" /> <stop offset="100%" style="stop-color:rgb(168,85,247);stop-opacity:1" /> </linearGradient> </defs> <rect width="300" height="150" rx="10" fill="url(#grad1)" /> <text x="150" y="80" font-family="Arial" font-size="24" fill="white" text-anchor="middle">Gradient!</text></svg>Example 4: Responsive Icon
Using viewBox for responsiveness:
Example 5: Multiple Shapes
Complex composition:
Example 6: With Shadow
Example 7: With Border
svg {class="bordered"}
Example 8: With Background
svg {class="with-bg"}
Example 9: Centered with Shadow
svg {class="with-shadow centered"}
Example 10: Left Aligned
svg {class="left bordered"}
Example 11: Right Aligned with Background
svg {class="right with-bg"}
Example 12: Pulse Animation
svg {class="pulse-animation centered"}
Example 13: Bounce Animation
svg {class="bounce-animation"}
Example 14: Rotate Animation
svg {class="rotate-animation"}
Example 15: Wiggle Animation
svg {class="wiggle-animation bordered"}
Example 16: Fade Animation
svg {class="fade-animation"}
Example 17: Heartbeat Animation
svg {class="heartbeat-animation"}
Example 18: Shake Animation
svg {class="shake-animation with-bg"}
Example 19: Float Animation
svg {class="float-animation centered"}
Example 20: Glow Animation
svg {class="glow-animation"}
Example 21: Swing Animation
svg {class="swing-animation"}
Example 22: Zoom Animation
svg {class="zoom-animation bordered"}
Example 23: Spin Slow Animation
svg {class="spin-slow-animation"}
Example 24: Hover Grow
svg {class="hover-grow with-shadow"}
Example 25: Hover Rotate
svg {class="hover-rotate bordered"}
Example 26: Hover Shadow
svg {class="hover-shadow"}
Example 27: Small Size
svg {class="svg-small"}
Example 28: Medium Size
svg {class="svg-medium bordered"}
Example 29: Large Size
svg {class="svg-large centered"}
Example 30: Full Width Size
svg {class="svg-full with-shadow"}
Example 31: Combined Classes - Shadow + Centered + Pulse
svg {class="with-shadow centered pulse-animation"}
Example 32: Combined Classes - Border + Background + Bounce
svg {class="bordered with-bg bounce-animation"}
Example 33: With Custom Width and Height
svg {width="250px" height="120px"}
Example 34: All Combined - Width, Class, Multiple Animations
svg {class="bordered with-shadow glow-animation centered" width="180px"}
Using Directive Syntax
You can also use the directive syntax:
:::svg{class="with-shadow centered"}<svg width="120" height="120" xmlns="http://www.w3.org/2000/svg"> <rect x="10" y="10" width="100" height="100" fill="#a855f7" rx="15"/> <circle cx="60" cy="60" r="30" fill="white" opacity="0.3"/></svg>:::Example 35: Directive with Animation
:::svg{class="rotate-animation bordered"}
Example 36: Directive with Width
:::svg{width="200px" class="centered"}
Heart Icon
Old Heart Icon (For Reference)
Using Directive Syntax
You can also use the directive syntax:
CSS Custom Classes - Available Options
The plugin provides extensive CSS styling options you can combine:
Basic Utilities
with-shadow- Adds subtle drop shadowbordered- Adds 2px solid borderwith-bg- Adds light background (adapts to dark mode)centered- Center alignmentleft- Left alignmentright- Right alignment
Size Utilities
svg-small- Max width 150pxsvg-medium- Max width 300pxsvg-large- Max width 500pxsvg-full- Full width (100%)
Animations
pulse-animation- Gentle pulsing effectbounce-animation- Bouncing motionrotate-animation- 360° rotationwiggle-animation- Side-to-side wigglefade-animation- Fade in/outheartbeat-animation- Heartbeat scalingshake-animation- Horizontal shakefloat-animation- Vertical floatingglow-animation- Pulsing glow effectswing-animation- Pendulum swingzoom-animation- Zoom in/outspin-slow-animation- Slow continuous spin
Hover Effects
hover-grow- Grows on hoverhover-rotate- Rotates on hoverhover-shadow- Shadow appears on hover
Usage Examples
Single class:
```svg {class="with-shadow"}<svg>...</svg>```Multiple classes combined:
```svg {class="bordered with-bg centered pulse-animation"}<svg>...</svg>```With custom dimensions:
```svg {class="with-shadow" width="200px" height="150px"}<svg>...</svg>```Directive syntax:
:::svg{class="hover-grow with-shadow"}<svg>...</svg>:::🛡️ Security Features
The plugin automatically protects against XSS attacks by:
- Removing all
<script>tags - Removing event handlers (
onclick,onload,onmouseover, etc.) - Removing
javascript:protocol from URLs - Validating SVG structure
You can safely use SVG from untrusted sources!
Tips & Best Practices
✅ DO:
- Always include
xmlns="http://www.w3.org/2000/svg" - Use
viewBoxfor responsiveness instead of fixed dimensions - Combine classes for richer effects:
{class="bordered with-shadow pulse-animation"} - Test animations in both light and dark modes
- Use semantic IDs for gradients to avoid conflicts
❌ DON’T:
- Include JavaScript (automatically removed for security)
- Use event handlers (automatically removed)
- Forget to close SVG tags properly
- Use duplicate gradient IDs across multiple SVGs on same page
- Overuse animations - they can be distracting
Conclusion
The SVG rendering plugin works great! You can now easily embed SVG graphics directly in your markdown blog posts using either:
- Code blocks: ```svg … “’
- Directives:
:::svg ... :::
Code Block (Recommended)
```svg<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg"> <!-- SVG content here --></svg>```Directive
:::svg<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg"> <!-- SVG content here --></svg>:::Code Block with Attributes
```svg {class="custom-class" width="200px" height="150px"}<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"> <!-- SVG content --></svg>```Directive with Attributes
:::svg{class="custom-class" width="200px"}<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"> <!-- SVG content --></svg>:::Both support custom styling through class names and inline styles. The plugin also includes built-in security features to prevent XSS attacks.
Common SVG Shapes
Circle
<circle cx="50" cy="50" r="40" fill="blue" />Rectangle
<rect x="10" y="10" width="80" height="60" fill="green" />Rounded Rectangle
<rect x="10" y="10" width="80" height="60" rx="10" fill="orange" />Ellipse
<ellipse cx="50" cy="50" rx="40" ry="25" fill="purple" />Line
<line x1="10" y1="10" x2="90" y2="90" stroke="black" stroke-width="2" />Polyline
<polyline points="10,10 50,50 90,10" stroke="red" fill="none" stroke-width="2" />Polygon (Triangle)
<polygon points="50,10 90,90 10,90" fill="yellow" />Path (Complex Shapes)
<path d="M 10 10 L 90 90 L 10 90 Z" fill="cyan" />Text
<text x="50" y="50" font-family="Arial" font-size="20" fill="black" text-anchor="middle">Hello!</text>Common Attributes
| Attribute | Description | Example |
|---|---|---|
fill | Fill color | fill="red" fill="#ff0000" |
stroke | Border color | stroke="blue" |
stroke-width | Border thickness | stroke-width="2" |
opacity | Transparency | opacity="0.5" |
rx, ry | Rounded corners | rx="10" |
viewBox | Coordinate system | viewBox="0 0 100 100" |
xmlns | Required! | xmlns="http://www.w3.org/2000/svg" |
Gradients
Linear Gradient
<defs> <linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%"> <stop offset="0%" style="stop-color:rgb(255,255,0)" /> <stop offset="100%" style="stop-color:rgb(255,0,0)" /> </linearGradient></defs><rect width="200" height="100" fill="url(#grad1)" />Radial Gradient
<defs> <radialGradient id="grad2"> <stop offset="0%" style="stop-color:rgb(255,255,255)" /> <stop offset="100%" style="stop-color:rgb(0,0,255)" /> </radialGradient></defs><circle cx="50" cy="50" r="40" fill="url(#grad2)" />Animations
Attribute Animation
<circle cx="50" cy="50" r="40" fill="red"> <animate attributeName="r" values="40;50;40" dur="2s" repeatCount="indefinite" /></circle>Transform Animation
<rect x="25" y="25" width="50" height="50" fill="blue"> <animateTransform attributeName="transform" type="rotate" from="0 50 50" to="360 50 50" dur="3s" repeatCount="indefinite" /></rect>Icons (Ready to Use)
Checkmark
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"> <circle cx="50" cy="50" r="45" fill="#10b981"/> <path d="M 30 50 L 45 65 L 70 35" stroke="white" stroke-width="8" fill="none" stroke-linecap="round"/></svg>Warning
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"> <polygon points="50,10 90,85 10,85" fill="#f59e0b"/> <text x="50" y="65" font-size="50" font-weight="bold" fill="white" text-anchor="middle">!</text></svg>Info
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"> <circle cx="50" cy="50" r="45" fill="#3b82f6"/> <circle cx="50" cy="30" r="5" fill="white"/> <rect x="45" y="42" width="10" height="35" rx="5" fill="white"/></svg>Heart
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"> <path d="M50,85 C25,70 5,50 5,30 C5,15 15,5 27.5,5 C35,5 42,10 50,20 C58,10 65,5 72.5,5 C85,15 95,15 95,30 C95,50 75,70 50,85 Z" fill="#e74c3c"/></svg>Star
<svg viewBox="0 0 51 48" xmlns="http://www.w3.org/2000/svg"> <path d="M25.5 0L31.5 18.5H51L35.5 30L41.5 48.5L25.5 37L9.5 48.5L15.5 30L0 18.5H19.5L25.5 0Z" fill="gold"/></svg>CSS Custom Classes
Available wrapper classes:
.remark-svg-wrapper(default).with-shadow- adds drop shadow.centered- center aligned.left- left aligned.right- right aligned.bordered- adds border.with-bg- adds background
Usage:
```svg {class="with-shadow centered"}<svg>...</svg>```Color Reference
Common colors:
- Red:
#e74c3c,#ff0000 - Blue:
#3b82f6,#0000ff - Green:
#10b981,#00ff00 - Yellow:
#f59e0b,#ffff00 - Purple:
#a855f7,#800080 - Orange:
#f97316,#ffa500 - Pink:
#ec4899,#ffc0cb - Gray:
#6b7280,#808080
Tips & Tricks
✅ DO:
- Always include
xmlns="http://www.w3.org/2000/svg" - Use
viewBoxfor responsiveness - Use semantic class names
- Optimize SVG before embedding
- Test in different themes
❌ DON’T:
- Include JavaScript (will be removed)
- Use event handlers (removed for security)
- Embed external images
- Forget to close SVG tag
- Use absolute pixel sizes when relative units work better
🛡️ Security
The plugin automatically:
- Removes
<script>tags - Removes event handlers (
onclick,onload, etc.) - Removes
javascript:protocol - Validates SVG structure
Additional Resources
Happy SVG rendering! 🎨