|
3 | 3 | Resize YouTube thumbnails down to 30% in theBrain Notes
|
4 | 4 |
|
5 | 5 | .DESCRIPTION
|
6 |
| - Append `#$width=30p$` to the image address within the Markdown files |
| 6 | + Scan all Markdown files in the user's Brain data directory, and apends `#$width=30p$` |
| 7 | + to the image URL of embedded YouTube thumbnails within the Markdown files, and backs |
| 8 | + up the original notes to the Backup folder before changing the Markdown file content. |
7 | 9 |
|
8 | 10 | .PARAMETER None
|
9 | 11 |
|
|
17 | 19 | PS C:\> .\Resize-TheBrainNotesYouTubeThumbnail.ps1
|
18 | 20 |
|
19 | 21 | .NOTES
|
20 |
| - Version: 1.1.5 |
| 22 | + Version: 2.0.0 |
21 | 23 | Author: chriskyfung
|
22 | 24 | License: GNU GPLv3 license
|
23 | 25 | Original from: https://gist.github.com/chriskyfung/ff65df9a60a7a544ff12aa8f810d728a/
|
|
28 | 30 | # Enable Verbose output
|
29 | 31 | [CmdletBinding()]
|
30 | 32 |
|
| 33 | +$ErrorActionPreference = "Stop" |
| 34 | + |
31 | 35 | # Look up the Notes.md files that locate under the Brain data folder and contain the YouTube thumbnail URLs.
|
32 | 36 | $BrainFolder = . "$PSScriptRoot\Get-TheBrainDataDirectory.ps1"
|
33 |
| -$MatchInfo = Get-ChildItem -Path $BrainFolder -Filter 'Notes.md' -Recurse | Select-String '\/(hq|maxres)default.jpg\)' -List |
| 37 | +$BackupFolder = Join-Path $BrainFolder 'Backup' |
| 38 | + |
| 39 | +$Filename = 'Notes.md' |
| 40 | +$FilenameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($Filename) |
| 41 | +$FileExtension = [System.IO.Path]::GetExtension($Filename) |
| 42 | + |
| 43 | +Write-Host 'Scanning YouTube thumbnail URLs in Brain Notes...' |
| 44 | +$MatchInfo = Get-ChildItem -Path $BrainFolder -Exclude $BackupFolder -Filter $Filename -Recurse | Select-String '\/(hq|maxres)default.jpg\)' -List |
34 | 45 |
|
35 | 46 | # For each matching result
|
| 47 | +Write-Information 'Backing up and modifying Brain Notes...' |
36 | 48 | ForEach ($Match in $MatchInfo) {
|
37 | 49 | $FilePath = $Match.Path | Convert-Path # FilePath of the Notes.md file
|
38 |
| - $ParentFolder = Split-Path -Path $FilePath # Path of the parent folder |
39 |
| - # Check if any backup files exist in the parent folder |
40 |
| - $NewIndex = 1 |
41 |
| - if (Test-Path -Path ( -join ($FilePath, '.bak*')) -PathType leaf) { |
42 |
| - # If true then determine the index of the last backup file |
43 |
| - $LastIndex = (Get-ChildItem -Path $ParentFolder -Filter '*.bak*' | Select-Object Extension -Unique | |
44 |
| - Sort-Object -Property Extension | Select-Object -Last 1)[0] -replace ('\D*', '') |
45 |
| - $NewIndex = [int]$LastIndex |
46 |
| - # If there are more than three backup files |
47 |
| - if ($NewIndex -ge 3) { |
48 |
| - # Remove the first backup file and re-index the rest of the backup files |
49 |
| - Remove-Item ( -join ($FilePath, '.bak1')) |
50 |
| - Rename-Item ( -join ($FilePath, '.bak2')) -NewName ( -join ($FilePath, '.bak1')) |
51 |
| - Rename-Item ( -join ($FilePath, '.bak3')) -NewName ( -join ($FilePath, '.bak2')) |
52 |
| - } |
53 |
| - else { |
54 |
| - # Else increment the index for the new backup file |
55 |
| - $NewIndex++ |
56 |
| - } |
57 |
| - } |
58 |
| - $NewName = -join ($FilePath, '.bak', $NewIndex) # FilePath of the new backup file |
59 |
| - Copy-Item $FilePath -Destination $NewName # Backup the Notes.md file |
60 |
| - Write-Verbose "Created -->' $NewName" |
| 50 | + $ParentFolder = Split-Path -Path $FilePath -Parent # Path of the parent folder |
| 51 | + $Timestamp = (Get-Item $FilePath).LastWriteTime.ToString('yyyyMMdd_HHmmss') |
| 52 | + $BackupFilename = "$FilenameWithoutExtension-$Timestamp$FileExtension~" |
| 53 | + $BackupPath = Join-Path $ParentFolder.Replace($BrainFolder, $BackupFolder) $BackupFilename |
| 54 | + # Backup the Notes.md file |
| 55 | + Copy-Item -Path $FilePath -Destination (New-Item -ItemType File -Force -Path $BackupPath) -Force |
| 56 | + Write-Verbose "Created --> '$BackupPath'" |
61 | 57 | # Amend the link of the YouTube thumbnail with UTF8 encoding
|
62 | 58 | $Pattern = $Match.Matches.Value
|
63 | 59 | $NewString = $Pattern.Replace(')', '#$width=30p$)')
|
64 | 60 | (Get-Content $FilePath -Encoding UTF8).Replace($Pattern, $NewString) | Set-Content $FilePath -Encoding UTF8
|
65 |
| - Write-Verbose "Modified -->' $FilePath" |
| 61 | + Write-Verbose "Modified --> '$FilePath'" |
66 | 62 | }
|
67 | 63 |
|
68 |
| -Write-Host $MatchInfo.Length 'file(s) found' # Output the number of files found |
| 64 | +Write-Host 'Finished: ' $MatchInfo.Length 'file(s) found' # Output the number of files found |
69 | 65 |
|
70 | 66 | $MatchInfo | Format-Table Path | Out-Host # Output the path of the files found
|
71 | 67 |
|
|
0 commit comments