Skip to content

Instantly share code, notes, and snippets.

@OndrejValenta
Created March 7, 2025 16:30
Show Gist options
  • Save OndrejValenta/6ff392ba30ed6f1ceb5ded04d6f160ca to your computer and use it in GitHub Desktop.
Save OndrejValenta/6ff392ba30ed6f1ceb5ded04d6f160ca to your computer and use it in GitHub Desktop.
Create SVG Preview html file that you can serve with live-server or similar.
# Define the folder path where SVG files are located
$folderPath = ".\"
# Get a list of all SVG files in the folder
$svgFiles = Get-ChildItem -Path $folderPath -Filter *.svg
# Create an HTML header with a grid layout
$htmlContent = @"
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SVG Gallery</title>
<style>
body {
font-family: Arial, sans-serif;
}
.svg-grid {
display: grid;
grid-template-columns: repeat(12, 1fr);
gap: 1rem;
}
.svg-cell {
text-align: center;
}
.svg-cell img {
width: 3rem;
height: 3rem;
cursor: pointer;
}
</style>
</head>
<body>
<h1>SVG Gallery</h1>
<div class="svg-grid">
"@
# Loop through the SVG files and generate HTML for each file
foreach ($svgFile in $svgFiles) {
$fileName = $svgFile.Name
$fileUrl = "./" + $svgFile.Name
$filePath = $svgFile.FullName
$htmlContent += @"
<div class="svg-cell">
<a href="$fileUrl" target="_blank">
<img src="$fileUrl" alt="$fileName"/>
</a>
<div>$fileName</div>
</div>
"@
}
# Close the grid and HTML structure
$htmlContent += @"
</div>
</body>
</html>
"@
# Define the output HTML file path
$outputFile = ".\index.html"
# Save the generated HTML content to the output file
$htmlContent | Out-File -FilePath $outputFile -Encoding UTF8
Write-Host "SVG gallery HTML file has been generated: $outputFile"
@OndrejValenta
Copy link
Author

It assumes the svg files are the current folder and creates the output as index.html so live-server can pick it up as default document

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment