Overview
Utility functions are defined insrc/utils/index.ts and provide reusable helper functionality across the application.
Social Sharing
createShareUrls
Generates social media share URLs for various platforms. Source:src/utils/index.ts:1-37
Function Signature
Parameters
The URL to be shared. Should be a fully qualified URL including protocol.Examples:
"https://aviv.sh/blog/my-post""https://aviv.sh/projects"
The title or headline for the shared content.Examples:
"My Latest Blog Post""Check out my new project"
A description or summary of the content being shared.Examples:
"Learn how to build fast websites with Astro""My experience contributing to Node.js core"
Return Value
Returns an array of share button configurations, each containing:Icon identifier for the platform using astro-icon format.Values:
"simple-icons:x"- X (Twitter)"simple-icons:linkedin"- LinkedIn"simple-icons:facebook"- Facebook"simple-icons:reddit"- Reddit"simple-icons:gmail"- Email
The platform-specific share URL with properly encoded parameters.
Human-readable label for accessibility and display.Values:
"Share on X""Share on LinkedIn""Share on Facebook""Share on Reddit""Share via Email"
Platforms Supported
The function generates share URLs for the following platforms:X (Twitter)
X (Twitter)
API Format:
https://twitter.com/share?url={url}&text={title}Parameters:url- The page URL (URL-encoded)text- The title/message (URL-encoded)
LinkedIn
API Format:
https://www.linkedin.com/shareArticle?url={url}&text={title}Parameters:url- The page URL (URL-encoded)text- The title/message (URL-encoded)
Facebook
API Format:
https://www.facebook.com/sharer/sharer.php?u={url}Parameters:u- The page URL (URL-encoded)
Reddit
API Format:
https://www.reddit.com/submit?u={url}&t={title}Parameters:u- The page URL (URL-encoded)t- The title (URL-encoded)
Email
API Format:
mailto:?subject={title}&text={description}%0A{url}Parameters:subject- The email subject (URL-encoded)text- Email body with description and URL (URL-encoded)
Usage Example
Usage in Components
Typically used in theShareButtons.astro component:
Implementation Details
URL Encoding
URL Encoding
The function uses JavaScript’s This ensures special characters are properly handled in share URLs.
encodeURIComponent() to properly encode all parameters:Email Formatting
Email Formatting
The email share includes both the description and URL in the body:The
%0A represents a newline character between the description and URL.Related Components
The utilities are used by the following components:- ShareButtons.astro (
src/components/common/ShareButtons.astro) - Renders social share buttons usingcreateShareUrls() - SocialLinks.astro (
src/components/common/SocialLinks.astro) - Displays profile social links (uses data fromsrc/data/index.ts)
Best Practices
URL Validation
Always pass fully qualified URLs (including
https://) to createShareUrls() to ensure proper sharing across platforms.Title Length
Keep titles concise (under 100 characters) for optimal display on social media platforms, especially Twitter/X.
Description Length
Descriptions should be 150-200 characters for best results across platforms.
Future Enhancements
Potential additions to the utilities module:- Date formatting helpers
- String truncation utilities
- Slug generation functions
- Reading time calculation
- SEO metadata generation
