Skip to content

Commit

Permalink
Merge d2a1754 into dbb9bd4
Browse files Browse the repository at this point in the history
  • Loading branch information
belibug authored May 31, 2024
2 parents dbb9bd4 + d2a1754 commit 9cc7140
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
43 changes: 43 additions & 0 deletions Src/Modules/MarkdownPS/Public/New-MDAlert.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
BeforeAll {
. $PSCommandPath.Replace('.Tests.ps1', '.ps1')
$MDQuoteFile = Join-Path -Path (Split-Path $PSCommandPath -Parent) -ChildPath 'New-MDQuote.ps1'
. $MDQuoteFile
$newLine = [System.Environment]::NewLine
}
Describe -Tag @('MarkdownPS', 'Cmdlet', 'Public') 'New-MDAlert' {
It '-Lines is null' {
{ New-MDAlert -Lines $null } | Should -Throw '*because it is null.*'
}
It '-Lines is empty' {
{ New-MDAlert -Lines @() } | Should -Throw '*because it is an empty array.*'
}
It '-Style out of range' {
{ New-MDAlert -Lines 'Line 1' -Style Unknown } | Should -Throw -ErrorId 'ParameterArgumentValidationError,New-MDAlert'
}
}
Describe -Tag @('MarkdownPS', 'Cmdlet', 'Public', 'New-MDAlert') 'New-MDAlert' {
It '-Lines count is 1 & -Style not specified' {
$expected = '> [!NOTE]' + $newLine + '> Line 1' + $newLine + $newLine
New-MDAlert -Lines 'Line 1' | Should -Be $expected
New-MDAlert -Lines @('Line 1') | Should -Be $expected
'Line 1' | New-MDAlert | Should -Be $expected
@('Line 1') | New-MDAlert | Should -Be $expected
}
It '-Lines count is 2 & -Style not specified' {
$expected = '> [!NOTE]' + $newLine + '> Line 1' + $newLine + '> Line 2' + $newLine + $newLine
# New-MDAlert -Lines @('Line 1', 'Line 2') | Should -Be $expected
@('Line 1', 'Line 2') | New-MDAlert | Should -Be $expected
}
It '-Lines count is 1 & -Style provided' {
$expected = '> [!TIP]' + $newLine + '> Line 1' + $newLine + $newLine
New-MDAlert -Lines 'Line 1' -Style Tip | Should -Be $expected
New-MDAlert -Lines @('Line 1') -Style Tip | Should -Be $expected
'Line 1' | New-MDAlert -Style Tip | Should -Be $expected
@('Line 1') | New-MDAlert -Style Tip | Should -Be $expected
}
It '-Lines count is 2 & -Level provided' {
$expected = '> [!TIP]' + $newLine + '> Line 1' + $newLine + '> Line 2' + $newLine + $newLine
New-MDAlert -Lines @('Line 1', 'Line 2') -Style Tip | Should -Be $expected
@('Line 1', 'Line 2') | New-MDAlert -Style Tip | Should -Be $expected
}
}
86 changes: 86 additions & 0 deletions Src/Modules/MarkdownPS/Public/New-MDAlert.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<#
.SYNOPSIS
This commandlet output markdown alerts based on blockquote in markdown syntax
.DESCRIPTION
This cmdlet outputs a quote in markdown syntax by adding `> [!STYLE]` as the first line, followed by the input lines each prefixed with `> ` on consecutive lines.
.PARAMETER Lines
An array of lines to quote in markdown
.PARAMETER Style
The Style of choice, options are one of (Tips, Warning, Note, Important, Caution), default style is Note
.PARAMETER NoNewLine
Controls if a new line is added at the end of the output
.EXAMPLE
New-MDAlert -Lines "Line 1"
> [!NOTE]
> Line 1
.EXAMPLE
New-MDAlert -Lines @("Line 1","Line 2") -Style Important
> [!IMPORTANT]
> Line 1
>
> Line 2
.EXAMPLE
@("Line 1","Line 2") | New-MDAlert -Style Caution
>> [!CAUTION]
>> Line 1
>>
>> Line 2
.INPUTS
An array of lines
.OUTPUTS
First line > [!STYLE] followed by each input line with a '> ' in the front
.NOTES
Use the -NoNewLine parameter when you don't want the next markdown content to be separated.
.LINK
New-MDAlert
https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#alerts
#>
function New-MDAlert {
[CmdletBinding()]
[OutputType([string])]
Param (
[Parameter(
Mandatory = $true,
Position = 0,
ValueFromPipeline = $true
)]
[string[]]$Lines,
[ValidateSet('Note', 'Tip', 'Important', 'Warning', 'CAUTION')]
[string]$Style = 'Note',
[ValidateNotNullOrEmpty()]
[switch]$NoNewLine = $false
)

Begin {
$output = ''
$Admonition = '[!{0}]' -f $Style.ToUpper()
$output += New-MDQuote -Lines $Admonition -Level 1 -NoNewLine
}

Process {
$lines | ForEach-Object {
$output += New-MDQuote -Lines $_ -Level 1 -NoNewLine
}
}

End {
if (-not $NoNewLine) {
$output += [System.Environment]::NewLine
}
$output
}
}

0 comments on commit 9cc7140

Please sign in to comment.