-
Notifications
You must be signed in to change notification settings - Fork 0
/
MarkdownToHtml.Shortcut.psm1
72 lines (66 loc) · 2.66 KB
/
MarkdownToHtml.Shortcut.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#Requires -Version 6.1
using namespace System.IO
If (-not $IsWindows) {
Throw 'Windows platform required. The script can only be executed in a Windows OS.'
}
Function Set-MarkdownToHtmlShortcut {
<#
.SYNOPSIS
Install the context menu shortcut to convert Markdown files to HTML files.
.DESCRIPTION
This function creates a context menu shortcut to convert Markdown files to HTML files by setting up the Windows Registry.
.PARAMETER NoIcon
Specifies that the shortcut icon should not be configured.
#>
[CmdletBinding()]
Param ([switch] $NoIcon)
# Set the extension of the file with base name Convert-MarkdownToHtml and return full path.
Function Private:Set-ConvertMd2HtmlExtension([string] $Extension) {
Return "$PSScriptRoot\Convert-MarkdownToHtml$Extension"
}
# The arguments to Set-Item and New-Item cmdlets.
$Arguments = @{
# The registry key of the command executed by the shortcut.
Path = 'HKCU:\SOFTWARE\Classes\SystemFileAssociations\.md\shell\cv2html\Command'
# %1 is the path to the selected mardown file to convert.
# The script to hide the PowerShell console window is executed in GUI mode (WScript).
# I keep using WScript.EXE because I cannot use the link directly in the registry.
Value = 'C:\Windows\System32\wscript.exe //E:jscript "{0}" /MarkdownPath:"%1" /RunLink' -f (Set-ConvertMd2HtmlExtension '.js')
}
# Overwrite the key value if it already exists.
# Otherwise, create it.
If (Test-Path $Arguments.Path -PathType Container) {
$CommandKey = Set-Item @Arguments -PassThru
} Else {
$CommandKey = New-Item @Arguments -Force
}
# Set the text on the menu and the icon using the parent of the command key: cv2html.
Set-Item -Path $CommandKey.PSParentPath -Value 'Convert to &HTML' -Force
$Arguments = @{
Path = $CommandKey.PSParentPath
Name = 'Icon'
Force = $True
}
If ($NoIcon) {
Remove-ItemProperty @Arguments -ErrorAction SilentlyContinue
Return
}
Set-ItemProperty @Arguments -Value (Set-ConvertMd2HtmlExtension '.ico')
}
Function Remove-MarkdownToHtmlShortcut {
<#
.SYNOPSIS
Remove the context menu shortcut to convert Markdown files to HTML files.
.DESCRIPTION
This function removes the context menu shortcut to convert Markdown files to HTML files by setting up the Windows Registry.
#>
[CmdletBinding()]
Param ()
# Remove the registry key of the shortcut verb.
Remove-Item 'HKCU:\SOFTWARE\Classes\SystemFileAssociations\.md\shell\cv2html' -Recurse
}
# Export the function Convert-MarkdownToHtml.
Get-Item -LiteralPath "$PSScriptRoot\Convert-MarkdownToHtml.ps1" |
ForEach-Object {
New-Item -Path "Function:\$($_.BaseName)" -Value (Get-Content $_.FullName -Raw)
}