Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

-Text strings ignore '_' underscore character. #30

Open
TechDufus opened this issue Dec 1, 2020 · 7 comments · May be fixed by #31
Open

-Text strings ignore '_' underscore character. #30

TechDufus opened this issue Dec 1, 2020 · 7 comments · May be fixed by #31
Labels
enhancement New feature or request pr welcome Would be great if someone did PR

Comments

@TechDufus
Copy link

When including the underscore character in any -Text value, the underscores are replaced with null space.

Example:

$PoolName = "App_Pool_01"
$SectionParams = @{
    ActivityTitle = "**Application Pool Recycle**"
    ActivitySubtitle = "@$env:USERNAME - $(Get-Date)"
    ActivityText = "Recycled Pool: $PoolName"
    SectionInput = [scriptblock] {
        New-TeamsButton -Name 'View Logs' -Type OpenUri -Link 'https://some.link.com'
    }
    Text = "An app pool recycle of $PoolName was initiated by $env:USERNAME"
}
Send-TeamsMessage {
    New-TeamsSection @SectionParams
} -Uri $Uri -Color DarkSeaGreen -MessageSummary 'Pool Recycle'

You can see in the following screenshot that it was removed the underscores from the app pool names.

image

@PrzemyslawKlys
Copy link
Member

Actually, they don't ignore it, but in my opinion, they try to convert it to markdown.

Teams support basic markdown. _test_ should give you italics. And that's what I see on the screen, that Pool01 is converted to italics. Is it possible the name is App_Pool_01? If so, you would need to escape it.

@PrzemyslawKlys
Copy link
Member

I can see in issue jenkinsci/office-365-connector-plugin#82 someone proposed using ticks

facts:
- name: "With back ticks"
  value: "`{{ this_is_it }}`"

Try it out.

@PrzemyslawKlys
Copy link
Member

New-AdaptiveCard -Uri $Env:TEAMSPESTERID -VerticalContentAlignment center {
    New-AdaptiveColumnSet {
        New-AdaptiveColumn {
            New-AdaptiveTextBlock -Size 'Medium' -Text 'Test Card _Title_ 1' -Color Warning
            New-AdaptiveTextBlock -Size 'Medium' -Text "Test Card \_Title_ 1" -Color Warning # This works
            New-AdaptiveTextBlock -Size 'Medium' -Text "Test Card \_Title\_ 1" -Color Warning # This works
            $Text = "Test Card _Title_ 1" -replace '_','\_'
            New-AdaptiveTextBlock -Size 'Medium' -Text $Text -Color Warning # This works
        }
    }
} -Verbose

So potentially we could add a switch called -EscapeMarkdown which would find all possible options that markdown triggers on and have it automatically fix it for a user. For now, my suggestion is to simply use the replacement proposed.

@PrzemyslawKlys PrzemyslawKlys added enhancement New feature or request pr welcome Would be great if someone did PR labels Dec 1, 2020
@TechDufus
Copy link
Author

Good call.. I wrote this function to fix this...

        #Region Invoke-EscapeCharacters
        
        Function Invoke-EscapeCharacters() {
            [CmdletBinding()]
            Param(
                [Parameter()]
                [Char] $Character,

                [Parameter(Mandatory)]
                [System.String] $InputString
            )

            Process {
                [System.String] $Character = $Character.ToString()
                $InputString.Replace($Character, "\$Character")
            }
        }
        #EndRegion Invoke-EscapeCharacters

My code looks something like this for now...

$PoolName = "App_Pool_01"
$SectionParams = @{
    ActivityTitle = "**Application Pool Recycle**"
    ActivitySubtitle = "@$env:USERNAME - $(Get-Date)"
    ActivityText = (Invoke-EscapeCharacters -Character _ -InputString "Recycled Pool: $PoolName")
    SectionInput = [scriptblock] {
        New-TeamsButton -Name 'View Logs' -Type OpenUri -Link 'https://some.link.com'
    }
    Text = "An app pool recycle of $(Invoke-EscapeCharacters -Character _ -InputString $PoolName) was initiated by $env:USERNAME"
}
Send-TeamsMessage {
    New-TeamsSection @SectionParams
} -Uri $Uri -Color DarkSeaGreen -MessageSummary 'Pool Recycle'

@PrzemyslawKlys
Copy link
Member

You don't need to do this inside Text. You can do it for the whole text. That is my point. As long as you don't plan using markdown you can escape all _ it can find. Same for * and other markdown chars. This way you don't have to "think" about it.

@TechDufus
Copy link
Author

How exactly would you do this for the whole Text at once? o.O

@PrzemyslawKlys
Copy link
Member

PrzemyslawKlys commented Dec 1, 2020

Using your function and example:

Function Invoke-EscapeCharacters() {
    [CmdletBinding()]
    Param(
        [Parameter()]
        [Char] $Character,

        [Parameter(Mandatory)]
        [System.String] $InputString
    )

    Process {
        [System.String] $Character = $Character.ToString()
        $InputString.Replace($Character, "\$Character")
    }
}
#EndRegion Invoke-EscapeCharacters

$PoolName = "App_Pool_01"
$SectionParams = @{
    ActivityTitle    = "**Application Pool Recycle**"
    ActivitySubtitle = "@$env:USERNAME - $(Get-Date)"
    ActivityText     = (Invoke-EscapeCharacters -Character _ -InputString "Recycled Pool: $PoolName")
    SectionInput     = [scriptblock] {
        New-TeamsButton -Name 'View Logs' -Type OpenUri -Link 'https://some.link.com'
    }
    Text             = Invoke-EscapeCharacters -Character _ -InputString "An app pool recycle of $PoolName was initiated by $env:USERNAME"
}
Send-TeamsMessage {
    New-TeamsSection @SectionParams
} -Uri $Uri -Color DarkSeaGreen -MessageSummary 'Pool Recycle'

image

In other words it would be possible to easily extend all text functions to turn off Markdown. In theory it would be possible to "disable" markdown on Send-TeamsMessage level as well - I guess.

I hope someone will try to PR this. Otherwise if I will have a need for it I'll fix it myself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request pr welcome Would be great if someone did PR
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants