Skip to content

Commit 1fbe72e

Browse files
committed
✨ feat: add parameter to set image width percentage in Resize-TheBrainNotesYouTubeThumbnail.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
1 parent b8048d7 commit 1fbe72e

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

theBrain/Resize-TheBrainNotesYouTubeThumbnail.ps1

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<#
22
.SYNOPSIS
3-
Resize YouTube thumbnails down to 30% in theBrain Notes
3+
Resize YouTube thumbnails in TheBrain Notes.
44
55
.DESCRIPTION
6-
Scan all Markdown files in the user's Brain data directory, and apends `#$width=30p$`
6+
Scan all Markdown files in the user's Brain data directory, and apends `#$width=50p$`
77
to the image URL of embedded YouTube thumbnails within the Markdown files, and backs
88
up the original notes to the Backup folder before changing the Markdown file content.
99
@@ -19,7 +19,7 @@
1919
PS C:\> .\Resize-TheBrainNotesYouTubeThumbnail.ps1
2020
2121
.NOTES
22-
Version: 2.0.0
22+
Version: 2.1.0
2323
Author: chriskyfung
2424
License: GNU GPLv3 license
2525
Original from: https://gist.github.com/chriskyfung/ff65df9a60a7a544ff12aa8f810d728a/
@@ -32,6 +32,16 @@
3232

3333
$ErrorActionPreference = "Stop"
3434

35+
<#
36+
.PARAMETERS
37+
Depending on the $ImageType parameter, it either finds the default YouTube
38+
thumbnails or the resized ones. If the $ImageType is 'resized', it also takes
39+
into account the $CurrentWidth parameter to find thumbnails of a specific width.
40+
#>
41+
$ImageType = 'default' # [ValidateSet('default', 'resized')]
42+
$CurrentWidth = 30 # [ValidateRange(1,100)]
43+
$NewWidth = 50 # [ValidateRange(1,100)]
44+
3545
# Look up the Notes.md files that locate under the Brain data folder and contain the YouTube thumbnail URLs.
3646
$BrainFolder = . "$PSScriptRoot\Get-TheBrainDataDirectory.ps1"
3747
$SubFolders = Get-ChildItem -Directory -Path $BrainFolder -Exclude 'Backup'
@@ -42,7 +52,12 @@ $FilenameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($Filen
4252
$FileExtension = [System.IO.Path]::GetExtension($Filename)
4353

4454
Write-Host 'Scanning YouTube thumbnail URLs in Brain Notes...'
45-
$MatchInfo = Get-ChildItem -Path $SubFolders -Filter $Filename -Recurse | Select-String '\/(hq|maxres)default.jpg\)' -List
55+
56+
if ($ImageType -eq 'default') {
57+
$MatchInfo = Get-ChildItem -Path $SubFolders -Filter $Filename -Recurse | Select-String '\/(hq|maxres)default.jpg\)' -List
58+
} else {
59+
$MatchInfo = Get-ChildItem -Path $SubFolders -Filter $Filename -Recurse | Select-String ('\/(hq|maxres)default.jpg#\$width={0}p\$\)' -f $CurrentWidth) -List
60+
}
4661

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

0 commit comments

Comments
 (0)