|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + Format YouTube thumbnails in TheBrain Notes by replacing local image URLs |
| 4 | + withto web URLs. |
| 5 | +
|
| 6 | +.DESCRIPTION |
| 7 | + Scan for YouTube thumbnail URLs in the Notes.md files located under the Brain |
| 8 | + data folder, then back up and modify the Notes.md files to retrieve the |
| 9 | + YouTube thumbnails from the YouTube API instead of using local storage. |
| 10 | +
|
| 11 | +.PARAMETER None |
| 12 | +
|
| 13 | +.INPUTS |
| 14 | + None. |
| 15 | +
|
| 16 | +.OUTPUTS |
| 17 | + None |
| 18 | +
|
| 19 | +.EXAMPLE |
| 20 | + PS C:\> .\Format-TheBrainNotesYouTubeThumbnail.ps1 |
| 21 | +
|
| 22 | +.NOTES |
| 23 | + Version: 1.0.0 |
| 24 | + Author: chriskyfung |
| 25 | + License: GNU GPLv3 license |
| 26 | +#> |
| 27 | + |
| 28 | +#Requires -Version 2.0 |
| 29 | + |
| 30 | +# Enable Verbose output |
| 31 | +[CmdletBinding()] |
| 32 | + |
| 33 | +$ErrorActionPreference = "Stop" |
| 34 | + |
| 35 | +# Look up the Notes.md files that locate under the Brain data folder and contain the YouTube thumbnail URLs. |
| 36 | +$BrainFolder = . "$PSScriptRoot\Get-TheBrainDataDirectory.ps1" |
| 37 | +$SubFolders = Get-ChildItem -Directory -Path $BrainFolder -Exclude 'Backup' |
| 38 | +$BackupFolder = Join-Path $BrainFolder 'Backup' |
| 39 | + |
| 40 | +$Filename = 'Notes.md' |
| 41 | +$FilenameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($Filename) |
| 42 | +$FileExtension = [System.IO.Path]::GetExtension($Filename) |
| 43 | + |
| 44 | +Write-Host 'Scanning YouTube thumbnail URLs in Brain Notes...' |
| 45 | + |
| 46 | +$MatchInfo = Get-ChildItem -Path $SubFolders -Filter $Filename -Recurse | Select-String '\[\!\[(?<ALT>.*)\]\(\.data\/md-images\/(?<IMAGEFILE>.+)\.jpg(?<width>.*)\)\]\(https:\/\/youtu.be\/(?<VIDEOID>.+?)\)' -List |
| 47 | + |
| 48 | +# For each matching result |
| 49 | +Write-Host 'Backing up and modifying Brain Notes...' |
| 50 | +ForEach ($Match in $MatchInfo) { |
| 51 | + $FilePath = $Match.Path | Convert-Path # FilePath of the Notes.md file |
| 52 | + $ParentFolder = Split-Path -Path $FilePath -Parent # Path of the parent folder |
| 53 | + $BackupLocation = $ParentFolder.Replace($BrainFolder, $BackupFolder) |
| 54 | + # Backup the Notes.md file |
| 55 | + $Timestamp = (Get-Item $FilePath).LastWriteTime.ToString('yyyyMMdd_HHmmss') |
| 56 | + $BackupFilename = "$FilenameWithoutExtension-$Timestamp$FileExtension~" |
| 57 | + $BackupPath = Join-Path $BackupLocation $BackupFilename |
| 58 | + Copy-Item -Path $FilePath -Destination (New-Item -ItemType File -Force -Path $BackupPath) -Force |
| 59 | + # Backup the md-image file |
| 60 | + $LocalImageFile = $Match.Matches[0].Groups['IMAGEFILE'].Value |
| 61 | + $LocalImagePath = Convert-Path "$ParentFolder/.data/md-images/$LocalImageFile.jpg" |
| 62 | + $BackupImagePath = $LocalImagePath.Replace($BrainFolder, $BackupFolder) |
| 63 | + Move-Item -Path $LocalImagePath -Destination (New-Item -ItemType File -Force -Path $BackupImagePath) -Force |
| 64 | + Write-Verbose "Created --> '$BackupPath'" |
| 65 | + # Amend the link of the YouTube thumbnail with UTF8 encoding |
| 66 | + $Pattern = $Match.Matches.Value |
| 67 | + $AltText = $Match.Matches[0].Groups['ALT'].Value |
| 68 | + $VideoId = $Match.Matches[0].Groups['VIDEOID'].Value |
| 69 | + $NewString = '[](https://www.youtube.com/watch?v={1})' -f $AltText, $VideoId |
| 70 | + (Get-Content $FilePath -Encoding UTF8).Replace($Pattern, $NewString) | Set-Content $FilePath -Encoding UTF8 |
| 71 | + Write-Verbose "Modified --> '$FilePath'" |
| 72 | +} |
| 73 | + |
| 74 | +Write-Host ('Finished: {0} file(s) found' -f $MatchInfo.Length) # Output the number of files found |
| 75 | + |
| 76 | +$MatchInfo | Format-Table Path | Out-Host # Output the path of the files found |
| 77 | + |
| 78 | +Return |
0 commit comments