-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ps1
73 lines (65 loc) · 2.07 KB
/
main.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
param (
[string]$TemplatePath,
[psobject[]]$ReplaceHash,
[string]$ReplaceHashFilePath,
[string]$OutputPath,
[string]$CSSPath,
[string]$NoOptimize
)
if (-not (Test-Path -Path $OutputPath)) {
$null = New-Item -Path $OutputPath -ItemType Directory -Force
}
$nonopt = [bool]($NoOptimize)
$template = Get-Content -Path $TemplatePath
if ($ReplaceHashFilePath) {
$ReplaceHash = Invoke-Expression (Get-Content -Path $ReplaceHashFilePath -Raw)
}
foreach ($hash in $ReplaceHash) {
foreach ($item in $hash) {
$content = $template
foreach ($key in $item.Keys) {
if ($key -eq "FileName") {
continue
}
$content = $content.Replace($key, $item[$key])
}
if ($item["FileName"]) {
$basename = $item["FileName"]
} else {
$count++
$basename = "image$count"
}
# Set all the name variables
$ext = $TemplatePath.Split('.') | Select-Object -Last 1
$filename = Join-Path -Path $OutputPath -ChildPath "$basename.$ext"
$outfile = Join-Path -Path $OutputPath -ChildPath "$basename.html"
$image = Join-Path -Path $OutputPath -ChildPath "$basename.png"
$content | Set-Content -Path $filename
Write-Output "Filename $filename"
Write-Output "Outfile $outfile"
Write-Output "Image $image"
Write-Output "CSS Path $CSSPath"
Write-Output "Opt in? $nonopt"
# Perform conversion
if ($ext -eq "md") {
$type = "markdown"
} else {
$type = "html"
}
if ($CSSPath) {
$csspaths = @()
foreach ($item in $CSSPath.Split(",")) {
$item = $item.Trim()
$csspaths += "--css=$item"
}
pandoc -f $type -t html $filename -o $outfile --self-contained $csspaths
} else {
pandoc -f $type -t html $filename -o $outfile --self-contained
}
npx playwright screenshot --viewport-size=1200,630 $outfile $image
if (-not $nonopt) {
Write-Output "Optimizing $image"
optipng -o7 $image *> /dev/null
}
}
}