Skip to content

URL building based on the markdown approach works for most cases, but not all #200

@PrzemyslawKlys

Description

@PrzemyslawKlys

URL building based on the markdown approach works for most cases. But it doesn't work for cases where the URL itself has brackets in it. The second text won't convert properly.

New-HTML {
    New-HTMLText -Text @(
        "This is a string with [SomeURL](https://evotec.xyz) and this isn't. "
        "This is more complicated [URL](https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2012-r2-and-2012/dn535495(v=ws.11)) with double () and it doesn't work properly "
        "More URLs in one line  [SomeURL](https://evotec.xyz) and and [SomeURL](https://evotec.xyz)."
    )
} -ShowHTML

Code for it is located at:

New-HTMLSpanStyle @newSpanTextSplat {
$FindMe = [regex]::Matches($Text[$i], "\[[^\]]+\]\([^)]+\)")
if ($FindMe) {
foreach ($find in $FindMe) {
$LinkName = ([regex]::Match($Find.value, "[^\[]+(?=\])")).Value
$LinkURL = ([regex]::Match($Find.value, "(?<=\().+?(?=\))")).Value
$Link = New-HTMLAnchor -HrefLink $LinkURL -Text $LinkName
$Text[$i] = $Text[$i].Replace($find.value, $Link)
}
$Text[$i]
} else {
# Default
$Text[$i]
}
# Replaces code above -> JustinWGrote made it -> complains go to him
#$markdownRegex = '\[(?<title>[^\]]+)\]\((?<uri>https?.+?)\)[\s\r\n$]'
#$Text[$i] -replace $markdownRegex, '<a href="$2">$1</a>'
<#
if ($Text[$i] -match "\[[^\]]+\]\([^)]+\)") {
# Covers markdown LINK "[somestring](https://evotec.xyz)"
$RegexBrackets1 = [regex] '\[[^\]]+\]\([^)]+\)'
$RegexBrackets1 = [regex] "\[([^\[]*)\]" # catch 'sometstring'
$RegexBrackets2 = [regex] "\(([^\[]*)\)" # catch link
$RegexBrackets3 = [regex] "\[([^\[]*)\)" # catch both somestring and link
$Text1 = $RegexBrackets1.match($Text[$i]).Groups[1].value
#$Text2 = $RegexBrackets2.match($Text[$i]).Groups[1].value
$Text3 = $RegexBrackets3.match($Text[$i]).Groups[0].value
if ($Text1 -ne '' -and $Text2 -ne '') {
$Link = New-HTMLAnchor -HrefLink $Text2 -Text $Text1
$Text[$i].Replace($Text3, $Link)
}
} else {
# Default
$Text[$i]
# if ($NewLine[$i]) {
# '<br>'
#}
}
#>
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingenhancementNew feature or requesthelp wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions