Skip to content
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
33 changes: 22 additions & 11 deletions Sources/Ink/Internal/Link.swift
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
/**
* Ink
* Copyright (c) John Sundell 2019
* MIT license, see LICENSE file for details
*/
* Ink
* Copyright (c) John Sundell 2019
* MIT license, see LICENSE file for details
*/

internal struct Link: Fragment {
var modifierTarget: Modifier.Target { .links }

var target: Target
var text: FormattedText

static func read(using reader: inout Reader) throws -> Link {
try reader.read("[")
var double=false
if reader.currentCharacter == "[" {
// double [[link]] style
reader.advanceIndex()
double=true
}
let text = FormattedText.read(using: &reader, terminators: ["]"])
try reader.read("]")

if double{
try reader.read("]")
}

guard !reader.didReachEnd else { throw Reader.Error() }

if reader.currentCharacter == "(" {
if double{
return Link(target: .url(URL("markdown://"+text.plainText())), text: text)
}
else if reader.currentCharacter == "(" {
reader.advanceIndex()
let url = try reader.read(until: ")")
return Link(target: .url(url), text: text)
Expand All @@ -27,14 +38,14 @@ internal struct Link: Fragment {
return Link(target: .reference(reference), text: text)
}
}

func html(usingURLs urls: NamedURLCollection,
modifiers: ModifierCollection) -> String {
let url = target.url(from: urls)
let title = text.html(usingURLs: urls, modifiers: modifiers)
return "<a href=\"\(url)\">\(title)</a>"
}

func plainText() -> String {
text.plainText()
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Ink/Internal/Reader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension Reader {

var didReachEnd: Bool { currentIndex == endIndex }
var previousCharacter: Character? { lookBehindAtPreviousCharacter() }
var currentCharacter: Character { string[currentIndex] }
var currentCharacter: Character { if currentIndex>=endIndex{return Character.init("ƒ")} else {return string[currentIndex]} }
var nextCharacter: Character? { lookAheadAtNextCharacter() }
var endIndex: String.Index { string.endIndex }

Expand Down