-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from alt3/prettier
- various features and optimizations - massive integration tests - finalizes the refactoring
- Loading branch information
Showing
27 changed files
with
420 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
function InsertFinalNewline() { | ||
<# | ||
.SYNOPSIS | ||
Adds a traling newline to the end of the file. | ||
#> | ||
param( | ||
[Parameter(Mandatory = $True)][System.IO.FileSystemInfo]$MarkdownFile | ||
) | ||
|
||
$content = ReadFile -MarkdownFile $MarkdownFile | ||
|
||
# replace file | ||
WriteFile -MarkdownFile $MarkdownFile -Content ($content + "`n") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
function InsertPowershellMonikers() { | ||
<# | ||
.SYNOPSIS | ||
Adds the `powershell` moniker to all code blocks without a language moniker. | ||
.NOTES | ||
We need to do this because PlatyPS does (yet) not add the moniker itself | ||
=> https://github.com/PowerShell/platyPS/issues/475 | ||
.LINK | ||
https://regex101.com/r/Jpo9AL/1 | ||
#> | ||
param( | ||
[Parameter(Mandatory = $True)][System.IO.FileSystemInfo]$MarkdownFile | ||
) | ||
|
||
$content = ReadFile -MarkdownFile $MarkdownFile | ||
|
||
$regex = '(```)\n((?:(?!```)[\s\S])+)(```)\n' | ||
|
||
$content = [regex]::replace($content, $regex, '```powershell' + "`n" + '$2```' + "`n") | ||
|
||
# replace file | ||
WriteFile -MarkdownFile $MarkdownFile -Content $content | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
function ReadFile() { | ||
<# | ||
.SYNOPSIS | ||
Retrieves raw markdown from file. | ||
#> | ||
param( | ||
[Parameter(Mandatory = $True)][System.IO.FileSystemInfo]$MarkdownFile | ||
) | ||
|
||
(Get-Content -Path $MarkdownFile.FullName -Raw).TrimEnd() | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
function ReplaceHeader1() { | ||
<# | ||
.SYNOPSIS | ||
Removes the markdown H1 element OR preprends it with an extra newline if the -KeepHeader1 switch is used. | ||
.LINK | ||
https://regex101.com/r/hnVQvQ/1 | ||
#> | ||
param( | ||
[Parameter(Mandatory = $True)][System.IO.FileSystemInfo]$MarkdownFile, | ||
[switch]$KeepHeader1 | ||
) | ||
|
||
$content = ReadFile -MarkdownFile $MarkdownFile | ||
|
||
$regex = '(---)\n(# .+)' | ||
|
||
if ($KeepHeader1) { | ||
$content = $content -replace $regex, ("---`n`n" + '$2') # prepend newline (for first match only) | ||
} else { | ||
$content = $content -replace $regex, '---' # remove line (for first match only) | ||
} | ||
|
||
# replace file | ||
WriteFile -MarkdownFile $MarkdownFile -Content $content | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
function SeparateHeaders() { | ||
<# | ||
.SYNOPSIS | ||
Adds an empty line after markdown headers IF they are directly followed by an adjacent non-empty lines. | ||
.NOTES | ||
This ensures the markdown format will match with e.g. Prettier which in turn will | ||
prevent getting format-change suggestions when running e.g. > Visual Studio Code | ||
> CTRL+SHIFT+P > Format Document. | ||
.LINK | ||
https://regex101.com/r/Gsd3PX/1 | ||
#> | ||
param( | ||
[Parameter(Mandatory = $True)][System.IO.FileSystemInfo]$MarkdownFile | ||
) | ||
|
||
Write-Verbose "Inserting empty line beneath non-separated headers." | ||
|
||
$content = ReadFile -MarkdownFile $MarkdownFile | ||
|
||
$regex = [regex]::new('(?m)([#+].+)\n(.+)') | ||
|
||
$content = $content -replace $regex, ('$1' + "`n`n" + '$2') | ||
|
||
# replace file | ||
WriteFile -MarkdownFile $MarkdownFile -Content $content | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
function SetLfLineEndings() { | ||
<# | ||
.SYNOPSIS | ||
Replaces all CRLF line endings with LF so we can consitently use/expect `n when regexing etc. | ||
#> | ||
param( | ||
[Parameter(Mandatory = $True)][System.IO.FileSystemInfo]$MarkdownFile | ||
) | ||
|
||
$content = ReadFile -MarkdownFile $MarkdownFile | ||
|
||
$content = ($content -replace "`r`n", "`n") + "`n" | ||
|
||
WriteFile -MarkdownFile $MarkdownFile -Content $content | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
function UnescapeSpecialChars() { | ||
<# | ||
.SYNOPSIS | ||
Replaces platyPS escaped special chars with the un-escaped version. | ||
#> | ||
param( | ||
[Parameter(Mandatory = $True)][System.IO.FileSystemInfo]$MarkdownFile | ||
) | ||
|
||
$content = ReadFile -MarkdownFile $MarkdownFile | ||
|
||
# regular | ||
$content = [regex]::replace($content, '\\`', '`') # backticks: ` | ||
$content = [regex]::replace($content, '\\\[', '[') # square opening brackets: [ | ||
$content = [regex]::replace($content, '\\\]', ']') # square closing brackets: ] | ||
|
||
# specific cases | ||
$content = [regex]::replace($content, '\\\\\\>', '\>') # as used in eg: PS C:\> | ||
|
||
# replace file | ||
WriteFile -MarkdownFile $MarkdownFile -Content $content | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
function WriteFile() { | ||
<# | ||
.SYNOPSIS | ||
Writes content to a UTF-8 file without BOM using LF as newlines. | ||
#> | ||
param( | ||
[Parameter(Mandatory = $True)][System.IO.FileSystemInfo]$MarkdownFile, | ||
[Parameter(Mandatory = $True)][string]$Content | ||
) | ||
|
||
# replace file (UTF-8 without BOM) | ||
$fileEncoding = New-Object System.Text.UTF8Encoding $False | ||
[System.IO.File]::WriteAllText($MarkdownFile.FullName, $Content, $fileEncoding) | ||
} |
Oops, something went wrong.