111 lines
3.0 KiB
PowerShell
111 lines
3.0 KiB
PowerShell
|
|
<#
|
|
.SYNOPSIS
|
|
将 themes/Ying/data/links.yaml 转换为 Friend-Circle-Lite 所需的 JSON 格式。
|
|
|
|
.DESCRIPTION
|
|
读取 Hugo 数据文件 links.yaml,提取 name, url, image 字段,
|
|
并生成 themes/Ying/static/json/link_lite.json 文件。
|
|
生成的格式为:
|
|
{
|
|
"friends": [
|
|
["Name", "URL", "AvatarURL"],
|
|
...
|
|
]
|
|
}
|
|
|
|
.EXAMPLE
|
|
.\scripts\update_link_lite_json.ps1
|
|
#>
|
|
|
|
$yamlPath = Join-Path $PSScriptRoot "..\themes\Ying\data\links.yaml"
|
|
$jsonPath = Join-Path $PSScriptRoot "..\themes\Ying\static\json\link_lite.json"
|
|
|
|
Write-Host "Reading $yamlPath..."
|
|
|
|
if (-not (Test-Path $yamlPath)) {
|
|
Write-Error "File not found: $yamlPath"
|
|
exit 1
|
|
}
|
|
|
|
$content = Get-Content $yamlPath -Encoding UTF8
|
|
$friends = @()
|
|
$currentFriend = $null
|
|
|
|
foreach ($line in $content) {
|
|
# Match start of a new list item: " - name: Value"
|
|
if ($line -match "^\s*-\s*name:\s*(.+)") {
|
|
# Save previous friend if exists
|
|
if ($currentFriend) {
|
|
# Only add if we have at least name and url
|
|
if ($currentFriend.name -and $currentFriend.url) {
|
|
$friends += ,$currentFriend
|
|
}
|
|
}
|
|
|
|
# Start new friend
|
|
$currentFriend = [ordered]@{
|
|
name = $matches[1].Trim()
|
|
url = ""
|
|
image = ""
|
|
rss = ""
|
|
}
|
|
}
|
|
# Match properties
|
|
elseif ($currentFriend -ne $null) {
|
|
if ($line -match "^\s*url:\s*(.+)") {
|
|
$currentFriend.url = $matches[1].Trim()
|
|
}
|
|
elseif ($line -match "^\s*image:\s*(.+)") {
|
|
$currentFriend.image = $matches[1].Trim()
|
|
}
|
|
elseif ($line -match "^\s*avatar:\s*(.+)") {
|
|
# Handle 'avatar' key just in case
|
|
$currentFriend.image = $matches[1].Trim()
|
|
}
|
|
elseif ($line -match "^\s*(rss|feed):\s*(.+)") {
|
|
$currentFriend.rss = $matches[2].Trim()
|
|
}
|
|
}
|
|
}
|
|
# Add the last one
|
|
if ($currentFriend -and $currentFriend.name -and $currentFriend.url) {
|
|
$friends += ,$currentFriend
|
|
}
|
|
|
|
Write-Host "Found $($friends.Count) friends."
|
|
|
|
# Convert to requested format: ["name", "url", "image", "rss"(optional)]
|
|
$outputList = @()
|
|
foreach ($f in $friends) {
|
|
# Use image if available, otherwise empty string or default
|
|
$img = if ($f.image) { $f.image } else { "" }
|
|
|
|
if ($f.rss) {
|
|
$outputList += ,@($f.name, $f.url, $img, $f.rss)
|
|
} else {
|
|
$outputList += ,@($f.name, $f.url, $img)
|
|
}
|
|
}
|
|
|
|
$outputData = @{
|
|
friends = $outputList
|
|
}
|
|
|
|
# Convert to JSON
|
|
# -Depth 10 ensures nested arrays are expanded
|
|
# -EscapeHandling Default ensures unicode is preserved/handled correctly (PowerShell 7)
|
|
$json = $outputData | ConvertTo-Json -Depth 10
|
|
|
|
# Ensure directory exists
|
|
$jsonDir = Split-Path $jsonPath
|
|
if (-not (Test-Path $jsonDir)) {
|
|
New-Item -ItemType Directory -Path $jsonDir | Out-Null
|
|
}
|
|
|
|
# Write to file without BOM
|
|
$utf8NoBom = New-Object System.Text.UTF8Encoding $false
|
|
[System.IO.File]::WriteAllText($jsonPath, $json, $utf8NoBom)
|
|
|
|
Write-Host "Successfully generated $jsonPath" -ForegroundColor Green
|