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

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

Open
PrzemyslawKlys opened this issue Feb 21, 2021 · 1 comment · Fixed by #216
Open
Labels
bug Something isn't working enhancement New feature or request help wanted Extra attention is needed

Comments

@PrzemyslawKlys
Copy link
Member

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>'
#}
}
#>
}

@PrzemyslawKlys PrzemyslawKlys added bug Something isn't working enhancement New feature or request help wanted Extra attention is needed labels Feb 21, 2021
@PrzemyslawKlys PrzemyslawKlys pinned this issue Feb 21, 2021
alialo pushed a commit to alialo/PSWriteHTML that referenced this issue Mar 17, 2021
Removed ")" exclusion from $FindMe - any string in [text](url) format should now match, regardless of number of brackets in url.

No longer require lazy matching for $LinkURL - it should always end in ")" which can then be excluded.

Should fix EvotecIT#200
@PrzemyslawKlys
Copy link
Member Author

It seems another edge case showed up:

New-HTML {
    New-HTMLText -Text @(
        "[Attacking Read-Only Domain Controllers (RODCs) to Own Active Directory](https://adsecurity.org/?p=3592)"
    )
} -ShowHTML

grueni added a commit to grueni/PSWriteHTML that referenced this issue Oct 6, 2023
fixes link urls when multiple rounded brackets are used
fixes EvotecIT#200
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
1 participant