Skip to content

Commit

Permalink
Allow inline parantheses and parse links without explicit schemes, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gaebel committed Apr 22, 2021
1 parent 5621501 commit 2a579e6
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions MarkdownKit/Sources/Common/Elements/Link/MarkdownLink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import Foundation
open class MarkdownLink: MarkdownLinkElement {

fileprivate static let regex = "\\[[^\\]]+\\]\\(\\S+(?=\\))\\)"

// This regex is eager if does not count even trailing Parentheses.
fileprivate static let onlyLinkRegex = "\\(\\S+(?=\\))\\)"
fileprivate static let onlyLinkRegex = "\\]\\(\\S+(?=\\))\\)"

open var font: MarkdownFont?
open var color: MarkdownColor?

Expand All @@ -33,11 +33,13 @@ open class MarkdownLink: MarkdownLinkElement {

open func formatText(_ attributedString: NSMutableAttributedString, range: NSRange,
link: String) {
guard let encodedLink = link.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlHostAllowed)
let fullLink = link.starts(with: "http") ? link : "https://\(link)"

guard let encodedLink = fullLink.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlHostAllowed)
else {
return
}
guard let url = URL(string: link) ?? URL(string: encodedLink) else { return }
guard let url = URL(string: fullLink) ?? URL(string: encodedLink) else { return }
attributedString.addAttribute(NSAttributedString.Key.link, value: url, range: range)
}

Expand Down

0 comments on commit 2a579e6

Please sign in to comment.