diff --git a/theBrain/Format-TheBrainNotesYouTubeThumbnail.ps1 b/theBrain/Format-TheBrainNotesYouTubeThumbnail.ps1 new file mode 100644 index 0000000..0be9663 --- /dev/null +++ b/theBrain/Format-TheBrainNotesYouTubeThumbnail.ps1 @@ -0,0 +1,78 @@ +<# +.SYNOPSIS + Format YouTube thumbnails in TheBrain Notes by replacing local image URLs + withto web URLs. + +.DESCRIPTION + Scan for YouTube thumbnail URLs in the Notes.md files located under the Brain + data folder, then back up and modify the Notes.md files to retrieve the + YouTube thumbnails from the YouTube API instead of using local storage. + +.PARAMETER None + +.INPUTS + None. + +.OUTPUTS + None + +.EXAMPLE + PS C:\> .\Format-TheBrainNotesYouTubeThumbnail.ps1 + +.NOTES + Version: 1.0.0 + Author: chriskyfung + License: GNU GPLv3 license +#> + +#Requires -Version 2.0 + +# Enable Verbose output +[CmdletBinding()] + +$ErrorActionPreference = "Stop" + +# 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' +$BackupFolder = Join-Path $BrainFolder 'Backup' + +$Filename = 'Notes.md' +$FilenameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($Filename) +$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 '\[\!\[(?.*)\]\(\.data\/md-images\/(?.+)\.jpg(?.*)\)\]\(https:\/\/youtu.be\/(?.+?)\)' -List + +# For each matching result +Write-Host 'Backing up and modifying Brain Notes...' +ForEach ($Match in $MatchInfo) { + $FilePath = $Match.Path | Convert-Path # FilePath of the Notes.md file + $ParentFolder = Split-Path -Path $FilePath -Parent # Path of the parent folder + $BackupLocation = $ParentFolder.Replace($BrainFolder, $BackupFolder) + # Backup the Notes.md file + $Timestamp = (Get-Item $FilePath).LastWriteTime.ToString('yyyyMMdd_HHmmss') + $BackupFilename = "$FilenameWithoutExtension-$Timestamp$FileExtension~" + $BackupPath = Join-Path $BackupLocation $BackupFilename + Copy-Item -Path $FilePath -Destination (New-Item -ItemType File -Force -Path $BackupPath) -Force + # Backup the md-image file + $LocalImageFile = $Match.Matches[0].Groups['IMAGEFILE'].Value + $LocalImagePath = Convert-Path "$ParentFolder/.data/md-images/$LocalImageFile.jpg" + $BackupImagePath = $LocalImagePath.Replace($BrainFolder, $BackupFolder) + Move-Item -Path $LocalImagePath -Destination (New-Item -ItemType File -Force -Path $BackupImagePath) -Force + Write-Verbose "Created --> '$BackupPath'" + # Amend the link of the YouTube thumbnail with UTF8 encoding + $Pattern = $Match.Matches.Value + $AltText = $Match.Matches[0].Groups['ALT'].Value + $VideoId = $Match.Matches[0].Groups['VIDEOID'].Value + $NewString = '[![{0}](https://img.youtube.com/vi/{1}/maxresdefault.jpg)](https://www.youtube.com/watch?v={1})' -f $AltText, $VideoId + (Get-Content $FilePath -Encoding UTF8).Replace($Pattern, $NewString) | Set-Content $FilePath -Encoding UTF8 + Write-Verbose "Modified --> '$FilePath'" +} + +Write-Host ('Finished: {0} file(s) found' -f $MatchInfo.Length) # Output the number of files found + +$MatchInfo | Format-Table Path | Out-Host # Output the path of the files found + +Return diff --git a/theBrain/Resize-TheBrainNotesYouTubeThumbnail.ps1 b/theBrain/Resize-TheBrainNotesYouTubeThumbnail.ps1 index 1b026ed..96c2888 100644 --- a/theBrain/Resize-TheBrainNotesYouTubeThumbnail.ps1 +++ b/theBrain/Resize-TheBrainNotesYouTubeThumbnail.ps1 @@ -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. @@ -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/ @@ -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' @@ -42,10 +52,15 @@ $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-Information 'Backing up and modifying Brain Notes...' +Write-Host 'Backing up and modifying Brain Notes...' ForEach ($Match in $MatchInfo) { $FilePath = $Match.Path | Convert-Path # FilePath of the Notes.md file $ParentFolder = Split-Path -Path $FilePath -Parent # Path of the parent folder @@ -57,12 +72,16 @@ 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'" } -Write-Host 'Finished: ' $MatchInfo.Length 'file(s) found' # Output the number of files found +Write-Host ('Finished: {0} file(s) found' -f $MatchInfo.Length) # Output the number of files found $MatchInfo | Format-Table Path | Out-Host # Output the path of the files found