$text
# .SYNOPSIS Build script for static website generator .DESCRIPTION This script processes Markdown content files and generates a static HTML website. It dynamically detects content sections, processes blog posts, and generates navigation menus. The output is a fully functional static website in the dist/ folder. Features: - Dynamic section detection from content/ folder - Markdown to HTML conversion - Blog post processing with front matter - Automatic image handling - Responsive navigation generation .REQUIREMENTS - PowerShell 7+ (for better UTF-8 handling) - Markdown files in content/ folder - Images in images/ folder .NOTES Output: Generated files are placed in dist/ directory #> # ============================================================================ # CONFIGURATION # ============================================================================ # Get the directory where this script is located $projectRoot = $PSScriptRoot # Define source and output directories $sourceDir = Join-Path $projectRoot "content" # Markdown content source $templateFile = Join-Path $projectRoot "template.html" # Temporary template file $imagesDir = Join-Path $projectRoot "images" # Source images directory # Output directory - can be overridden with OUTPUT_DIR environment variable $outputDir = if ($env:OUTPUT_DIR) { Join-Path $projectRoot $env:OUTPUT_DIR } else { Join-Path $projectRoot "dist" # Default output directory } # Get current year for copyright $year = (Get-Date).Year # Site configuration - customize these values $siteTitle = "My Website" $siteHeader = "Welcome to My Website" $footerText = "My Website" # Temporary output files (will be moved to dist/ at the end) $tempOutputFile = Join-Path $projectRoot "index.html" $tempBlogOutputFile = Join-Path $projectRoot "blog.html" # ============================================================================ # HELPER FUNCTIONS # ============================================================================ <# .SYNOPSIS Finds the background image file in the images directory .DESCRIPTION Searches for any file matching "background.*" pattern and returns the relative path for use in CSS background-image property. .PARAMETER imagesDir Path to the images directory .OUTPUTS String - Relative path to background image (e.g., "images/background.jpg") Empty string if no background image found #> function Get-BackgroundImagePath { param ( [string]$imagesDir ) $backgroundImages = Get-ChildItem (Join-Path $imagesDir "background.*") -ErrorAction SilentlyContinue if ($backgroundImages.Count -gt 0) { return "images/" + $backgroundImages[0].Name } return "" # Return empty string if no background image found } # Create output directory if it doesn't exist if (-not (Test-Path $outputDir)) { New-Item -ItemType Directory -Path $outputDir | Out-Null } # Copy static files to dist first Copy-Item "styles.css" -Destination $outputDir -Force if (Test-Path "images") { Copy-Item "images" -Destination $outputDir -Recurse -Force } # Set output files after copying $outputFile = Join-Path $outputDir "index.html" $blogOutputFile = Join-Path $outputDir "blog.html" # Get background image path $backgroundPath = Get-BackgroundImagePath -imagesDir $imagesDir $backgroundStyle = if ($backgroundPath) { "background-image: url('$backgroundPath');" } else { "" # No background image } <# .SYNOPSIS Generates navigation menu HTML from section IDs .DESCRIPTION Creates an unordered list of navigation links for all detected sections, plus a Blog link. Section names are capitalized for display. .PARAMETER sections Array of section IDs (e.g., @("about", "services", "contact")) .OUTPUTS String - HTML navigation menu items #> function Get-NavigationHtml { param ( [array]$sections ) $navItems = @() foreach ($section in $sections) { # Capitalize first letter of section name $sectionName = $section.Substring(0,1).ToUpper() + $section.Substring(1) $navItems += "
$text
$2'
# Convert inline code (`code`)
$html = $html -replace '`([^`]+)`', '$1'
# Convert headings (##, ###, etc.)
$html = $html -replace '(?m)^####\s+(.+)$', '$para
" } } } return $processedParagraphs -join "`n" } <# .SYNOPSIS Converts a blog post Markdown file to HTML .DESCRIPTION Processes blog posts with front matter (YAML-like metadata between --- markers): - Extracts title, date, author, tags, image - Converts markdown content to HTML - Generates complete blog post article HTML .PARAMETER file Path to blog post Markdown file .PARAMETER imagesDir Path to images directory for blog post images .OUTPUTS String - Complete HTML article markup .NOTES Front matter format: --- title: Post Title date: YYYY-MM-DD author: Author Name tags: [tag1, tag2, tag3] image: imageName --- #> function Convert-BlogPostToHtml { param ( [string]$file, [string]$imagesDir ) Write-Host "Converting file: $file" $content = Get-Content $file -Raw Write-Host "Content length: $($content.Length)" # Updated regex pattern to match exactly three dashes if ($content -match "(?ms)^---[\r\n](.*?)[\r\n]---[\r\n](.*)$") { Write-Host "Found front matter" $frontMatter = $matches[1] $content = $matches[2].Trim() Write-Host "Front matter: $frontMatter" Write-Host "Content after front matter: $content" # Parse front matter into hashtable $metadata = @{} foreach ($line in ($frontMatter -split "`n")) { if ($line -match "^(\w+):\s*(.*)$") { $key = $matches[1] $value = $matches[2].Trim() if ($key -eq "tags") { $value = $value.Trim("[]").Split(",").ForEach({ $_.Trim() }) } $metadata[$key] = $value Write-Host "Parsed metadata: $key = $value" } } # Find blog post image $imagePath = Get-BlogPostImage -imageName $metadata.image -postFileName (Split-Path $file -Leaf) -imagesDir $imagesDir $imageHtml = if ($imagePath) { "