# Set the path to your posts directory $postsPath = "content/post" # Get all markdown files in the posts directory $files = Get-ChildItem -Path $postsPath -Filter "*.md" # Initialize max ID $maxId = 0 # Loop through each file to find the maximum pid foreach ($file in $files) { $content = Get-Content $file.FullName -Raw -Encoding UTF8 # Check if the file has a pid field if ($content -match "(?m)^pid:\s*(\d+)") { $currentId = [int]$matches[1] if ($currentId -gt $maxId) { $maxId = $currentId } } } # Calculate the next ID $nextId = $maxId + 1 Write-Host "Current max PID is $maxId. Next PID will be $nextId." -ForegroundColor Green # Prompt user for the new post filename (slug) $slug = Read-Host "Enter the filename for the new post (e.g., my-new-post, without .md extension)" if ([string]::IsNullOrWhiteSpace($slug)) { Write-Error "Filename cannot be empty." exit } # Ensure the filename ends with .md (optional, hugo new handles it usually but let's be safe) if (-not $slug.EndsWith(".md")) { $filename = "$slug.md" } else { $filename = $slug } # Create the new post using Hugo Write-Host "Creating new post: content/post/$filename" hugo new "content/post/$filename" # Path to the new file $newFilePath = Join-Path $postsPath $filename # Check if file was created successfully if (Test-Path $newFilePath) { # Read the new file content $newFileContent = Get-Content $newFilePath -Raw -Encoding UTF8 # Add the pid field to the front matter # Assuming front matter ends with --- or similar. # We'll just insert it after the date or title, or at the end of the front matter. # Simple replacement to add pid after date if ($newFileContent -match "(?m)^date:.*$") { $newFileContent = $newFileContent -replace "(?m)(^date:.*$)", "`$1`npid: $nextId" } else { # Fallback: add to the end of the first YAML block if date not found if ($newFileContent -match "^---[\s\S]*?---") { $newFileContent = $newFileContent -replace "(?m)(^---[\s\S]*?)(\r?\n---)", "`$1`npid: $nextId`$2" } } # Save the updated content back to the file Set-Content -Path $newFilePath -Value $newFileContent -Encoding UTF8 Write-Host "Successfully added 'pid: $nextId' to $newFilePath" -ForegroundColor Cyan } else { Write-Error "Failed to create the new post file." }