Public/ConvertTo-MarkdownLink.ps1
Function ConvertTo-MarkdownLink { <# .SYNOPSIS Converts file paths to Markdown link items. .DESCRIPTION Converts file paths to Markdown links by inserting the proper syntax at the beginning and end of the string. .PARAMETER Path The path of the file to transform into a link. .PARAMETER Label The text to be displayed instead of the link destination. If not specified, the full path will be used. If the destination is a picture file, the filename will be used. .PARAMETER Picture Manually specifies an embedded image file link should be generated. Common image types will automatically be detected. .PARAMETER AsLink Overrides automatic image detection and generates a text link instead of an embedded image link. .PARAMETER RelativePath Converts a full file path to a relative path based on current directory. .EXAMPLE ConvertTo-MarkdownLink -Path "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" -Label "Google Logo" .EXAMPLE ConvertTo-MarkdownLink -Path "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" -Label "Google Logo" -AsLink .EXAMPLE Set-Location C:\Path\To\Project\Docs Get-ChildItem *.jpg | ConvertTo-MarkdownLink -RelativePath .EXAMPLE Set-Location C:\Path\To\Project\Docs Get-ChildItem "1.docx" | ConvertTo-MarkdownLink -RelativePath -Label "Project Documentation" #> [CmdletBinding(DefaultParameterSetName="Picture")] PARAM ( [Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] [ValidateNotNullOrEmpty()] [Alias("FullName")] [String[]]$Path, [Parameter(Mandatory=$false, Position=1, ValueFromPipeline=$false)] [ValidateNotNullOrEmpty()] [String]$Label, [Parameter(Mandatory=$false, ParameterSetName="Picture")] [Switch]$Picture, [Parameter(Mandatory=$false, ParameterSetName="AsLink")] [Switch]$AsLink, [Parameter(Mandatory=$false)] [Switch]$RelativePath ) #region BEGIN Block BEGIN { # Locally scope ErrorActionPreference for predictable behavior of Try/Catch blocks inside the function $ErrorActionPreference = 'Stop' # Create output variable $Results = [System.Collections.ArrayList]::new() # Create list of automatically recognized picture filetypes $AutomaticPictureTypes = ".jpg", ".jpeg", ".gif", ".bmp", ".png", ".tiff" # Construct a regex query to check for supported file types $AutomaticPictureTypes = ($AutomaticPictureTypes -join "$|")+"$" } #endregion BEGIN Block #region PROCESS Block PROCESS { FOREACH ($Entry in $Path) { # Declare variables $LabelStart = '[' # Resolve relative path if $RelativePath switch is specified IF ($RelativePath) { $Entry = Get-Item $Entry | Resolve-Path -Relative } # Sanitize path input $Entry = $Entry.Trimstart(' ').TrimEnd(' ').Replace('\','/').Replace(" ","%20") # Set $Label if not specified by the user IF (!$Label) { $Label = $Entry } # Set $Label to filename if file is a picture and label has not been specified by the user IF (!$Label -eq $Entry -and $Picture) { $Label = $Entry.Split('/')[-1].Replace("%20"," ") } # Flip $Picture switch automatically if a recognized file type is found IF ($Entry -match $AutomaticPictureTypes) { $Picture = $true } # Change insertion syntax based on whether $Picture switch is specified IF ($Picture -and !$AsLink) { $LabelStart = $LabelStart.Insert(0,'!') } # Insert Markdown syntax into $Entry $Entry = "$LabelStart$Label]($Entry)" # Add to results $Results.Add($Entry) | Out-Null } } #endregion PROCESS Block #region END Block END { Return $Results } #endregion END Block } |