Skip to content

Commit

Permalink
✨ feat: add parameter to set image width percentage in Resize-TheBrai…
Browse files Browse the repository at this point in the history
…nNotesYouTubeThumbnail.ps1

This commit introduces a new parameter to the `Resize-TheBrainNotesYouTubeThumbnail.ps1` script that allows users to set the width of the image as a percentage. This enhancement provides more flexibility to users by allowing them to specify the desired width of the image.

The changes include:
- Added a new parameter `$NewWidth` to set the width of the image as a percentage.
- Adjusted the string `$width={$CurrentWidth}p` to reflect the new width when `$ImageType` is resized.
- Set 'default' as the default value of the `$ImageType` parameter to simplify the usage of the script.

This change enhances the functionality of the script and makes it more user-friendly by providing more customization options.

Fixes #17
  • Loading branch information
chriskyfung committed Mar 22, 2024
1 parent b8048d7 commit 1fbe72e
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions theBrain/Resize-TheBrainNotesYouTubeThumbnail.ps1
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<#
.SYNOPSIS
Resize YouTube thumbnails down to 30% in theBrain Notes
Resize YouTube thumbnails in TheBrain Notes.
.DESCRIPTION
Scan all Markdown files in the user's Brain data directory, and apends `#$width=30p$`
Scan all Markdown files in the user's Brain data directory, and apends `#$width=50p$`
to the image URL of embedded YouTube thumbnails within the Markdown files, and backs
up the original notes to the Backup folder before changing the Markdown file content.
Expand All @@ -19,7 +19,7 @@
PS C:\> .\Resize-TheBrainNotesYouTubeThumbnail.ps1
.NOTES
Version: 2.0.0
Version: 2.1.0
Author: chriskyfung
License: GNU GPLv3 license
Original from: https://gist.github.com/chriskyfung/ff65df9a60a7a544ff12aa8f810d728a/
Expand All @@ -32,6 +32,16 @@

$ErrorActionPreference = "Stop"

<#
.PARAMETERS
Depending on the $ImageType parameter, it either finds the default YouTube
thumbnails or the resized ones. If the $ImageType is 'resized', it also takes
into account the $CurrentWidth parameter to find thumbnails of a specific width.
#>
$ImageType = 'default' # [ValidateSet('default', 'resized')]
$CurrentWidth = 30 # [ValidateRange(1,100)]
$NewWidth = 50 # [ValidateRange(1,100)]

# Look up the Notes.md files that locate under the Brain data folder and contain the YouTube thumbnail URLs.
$BrainFolder = . "$PSScriptRoot\Get-TheBrainDataDirectory.ps1"
$SubFolders = Get-ChildItem -Directory -Path $BrainFolder -Exclude 'Backup'
Expand All @@ -42,7 +52,12 @@ $FilenameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($Filen
$FileExtension = [System.IO.Path]::GetExtension($Filename)

Write-Host 'Scanning YouTube thumbnail URLs in Brain Notes...'
$MatchInfo = Get-ChildItem -Path $SubFolders -Filter $Filename -Recurse | Select-String '\/(hq|maxres)default.jpg\)' -List

if ($ImageType -eq 'default') {
$MatchInfo = Get-ChildItem -Path $SubFolders -Filter $Filename -Recurse | Select-String '\/(hq|maxres)default.jpg\)' -List
} else {
$MatchInfo = Get-ChildItem -Path $SubFolders -Filter $Filename -Recurse | Select-String ('\/(hq|maxres)default.jpg#\$width={0}p\$\)' -f $CurrentWidth) -List
}

# For each matching result
Write-Host 'Backing up and modifying Brain Notes...'
Expand All @@ -57,7 +72,11 @@ ForEach ($Match in $MatchInfo) {
Write-Verbose "Created --> '$BackupPath'"
# Amend the link of the YouTube thumbnail with UTF8 encoding
$Pattern = $Match.Matches.Value
$NewString = $Pattern.Replace(')', '#$width=30p$)')
if ($ImageType -eq 'default') {
$NewString = $Pattern.Replace(')', '#$width={0}p$)' -f $NewWidth)
} else {
$NewString = $Pattern.Replace('#$width=30p$)', '#$width={0}p$)' -f $NewWidth)
}
(Get-Content $FilePath -Encoding UTF8).Replace($Pattern, $NewString) | Set-Content $FilePath -Encoding UTF8
Write-Verbose "Modified --> '$FilePath'"
}
Expand Down

0 comments on commit 1fbe72e

Please sign in to comment.