Skip to content

Added a new link marker to manage in-app links #35

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Marker/Marker/Marker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ public func parsedMarkdownString(from markdownText: String,
value: linkFont,
range: NSRange(range, in: parsedString))
}
case .inAppLink(let range):
font = textStyle.strongFont
if let linkColor = textStyle.linkColor {
attributedString.addAttribute(AttributedStringKey.foregroundColor,
value: linkColor,
range: NSRange(range, in: parsedString))
}
}

if let font = font {
Expand Down
3 changes: 2 additions & 1 deletion Marker/Marker/Parser/MarkdownElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ enum MarkdownElement {
case strikethrough(range: Range<Index>)
case underline(range: Range<Index>)
case link(range: Range<Index>, urlString: String)
case inAppLink(range: Range<Index>)

/// Range of characters that the elements apply to.
var range: Range<Index> {
switch self {
case .em(let range), .strong(let range), .strikethrough(let range), .underline(let range), .link(let range, _):
case .em(let range), .strong(let range), .strikethrough(let range), .underline(let range), .link(let range, _), .inAppLink(let range):
return range
}
}
Expand Down
11 changes: 9 additions & 2 deletions Marker/Marker/Parser/MarkdownParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct MarkdownParser {
private static let linkTextClosingSymbol = Symbol(character: "]")
private static let linkURLOpeningSymbol = Symbol(character: "(")
private static let linkURLClosingSymbol = Symbol(character: ")")
private static let inAppLinkSymbol = Symbol(rawValue: "--")

// MARK: - Static functions

Expand All @@ -45,7 +46,8 @@ struct MarkdownParser {
let underscoreStrongSymbol = underscoreStrongSymbol,
let asteriskStrongSymbol = asteriskStrongSymbol,
let tildeStrikethroughSymbol = tildeStrikethroughSymbol,
let equalUnderlineSymbol = equalUnderlineSymbol else {
let equalUnderlineSymbol = equalUnderlineSymbol,
let inAppLinkSymbol = inAppLinkSymbol else {
return (string, [])
}

Expand All @@ -57,6 +59,7 @@ struct MarkdownParser {
let equalUnderlineRule = Rule(symbol: equalUnderlineSymbol)
let linkTextRule = Rule(openingSymbol: linkTextOpeningSymbol, closingSymbol: linkTextClosingSymbol)
let linkURLRule = Rule(openingSymbol: linkURLOpeningSymbol, closingSymbol: linkURLClosingSymbol)
let inAppLinkRule = Rule(symbol: inAppLinkSymbol)

let tokens = try TokenParser.parse(string,
using: [underscoreEmRule,
Expand All @@ -66,7 +69,8 @@ struct MarkdownParser {
tildeStrikethroughRule,
equalUnderlineRule,
linkTextRule,
linkURLRule])
linkURLRule,
inAppLinkRule])

guard tokens.count > 0 else {
return (string, [])
Expand Down Expand Up @@ -105,6 +109,9 @@ struct MarkdownParser {
elements.append(.link(range: range,urlString: tokens[i + 1].string))

i += 1
case .some(inAppLinkRule):
let range = strippedString.append(contentOf: token)
elements.append(.inAppLink(range: range))
default:
strippedString += token.stringWithRuleSymbols
}
Expand Down