diff --git a/Sources/Ink/Internal/Link.swift b/Sources/Ink/Internal/Link.swift index 8770093..cefe5b3 100644 --- a/Sources/Ink/Internal/Link.swift +++ b/Sources/Ink/Internal/Link.swift @@ -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) @@ -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 "\(title)" } - + func plainText() -> String { text.plainText() } diff --git a/Sources/Ink/Internal/Reader.swift b/Sources/Ink/Internal/Reader.swift index ef597d2..64f5739 100644 --- a/Sources/Ink/Internal/Reader.swift +++ b/Sources/Ink/Internal/Reader.swift @@ -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 }