Skip to content

Commit e22ac8f

Browse files
committed
References
1 parent 45f3fb1 commit e22ac8f

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

CommonMarkdown/CommonMarkdown/HTMLRenderer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public class HTMLRenderer {
9898
}
9999

100100
func renderBlocks(blocks:[Block], inTightList:Bool) -> String {
101-
return join(blockSeparator, map(blocks, { self.renderBlock($0, inTightList: inTightList) }))
101+
return join(blockSeparator, map(filter(blocks) { $0.tag != "ReferenceDef" }, { self.renderBlock($0, inTightList: inTightList) }))
102102
}
103103

104104
func renderInline(inline:Inline) -> String {

CommonMarkdown/CommonMarkdown/InlineParser.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ struct Text {
8181
func since(other:Text) -> String {
8282
return string.substringWithRange(other.position ..< position)
8383
}
84+
85+
var stringValue: String {
86+
return string.substringFromIndex(position)
87+
}
8488
}
8589

8690

@@ -89,7 +93,43 @@ class InlineParser {
8993
var refmap = [String: Link]()
9094
var labelNestLevel = 0
9195

96+
// Attempt to parse a link reference, modifying refmap.
9297
func parseReference(inout possibleReference:String) -> Bool {
98+
99+
var text = Text(string: possibleReference)
100+
101+
if let rawLabel = parseLinkLabel(&text) {
102+
103+
if text.startsWithAny(":") {
104+
text.skip(1)
105+
text.spln()
106+
107+
if let destination = parseLinkDestination(&text) {
108+
109+
let beforeTitle = text
110+
111+
text.spln()
112+
let title = parseLinkTitle(&text)
113+
114+
if title == nil {
115+
text = beforeTitle
116+
}
117+
118+
// make sure we're at line end:
119+
if text.match(regex("^ *(?:\n|$)")) != nil {
120+
let label = normalizeReference(rawLabel)
121+
122+
if refmap[label] == nil {
123+
refmap.updateValue(Link(destination: destination, title: title), forKey: label)
124+
}
125+
126+
possibleReference = text.stringValue
127+
return true
128+
}
129+
}
130+
}
131+
}
132+
93133
return false
94134
}
95135

0 commit comments

Comments
 (0)