Skip to content

Commit 0cfef8b

Browse files
authored
Revert "Add link title support for commonmark (#140)"
This reverts commit e5ab909.
1 parent e5ab909 commit 0cfef8b

File tree

6 files changed

+25
-91
lines changed

6 files changed

+25
-91
lines changed

Sources/Markdown/Base/RawMarkup.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ enum RawMarkupData: Equatable {
3535
case image(source: String?, title: String?)
3636
case inlineHTML(String)
3737
case lineBreak
38-
case link(destination: String?, title: String?)
38+
case link(destination: String?)
3939
case softBreak
4040
case strong
4141
case text(String)
@@ -274,8 +274,8 @@ final class RawMarkup: ManagedBuffer<RawMarkupHeader, RawMarkup> {
274274
return .create(data: .lineBreak, parsedRange: parsedRange, children: [])
275275
}
276276

277-
static func link(destination: String?, title: String? = nil,parsedRange: SourceRange?, _ children: [RawMarkup]) -> RawMarkup {
278-
return .create(data: .link(destination: destination, title: title), parsedRange: parsedRange, children: children)
277+
static func link(destination: String?, parsedRange: SourceRange?, _ children: [RawMarkup]) -> RawMarkup {
278+
return .create(data: .link(destination: destination), parsedRange: parsedRange, children: children)
279279
}
280280

281281
static func softBreak(parsedRange: SourceRange?) -> RawMarkup {

Sources/Markdown/Inline Nodes/Inline Containers/Link.swift

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,16 @@ public struct Link: InlineMarkup, InlineContainer {
2929

3030
public extension Link {
3131
/// Create a link with a destination and zero or more child inline elements.
32-
init<Children: Sequence>(destination: String? = nil, title: String? = nil, _ children: Children) where Children.Element == RecurringInlineMarkup {
32+
init<Children: Sequence>(destination: String? = nil, _ children: Children) where Children.Element == RecurringInlineMarkup {
3333

3434
let destinationToUse: String?
3535
if let d = destination, d.isEmpty {
3636
destinationToUse = nil
3737
} else {
3838
destinationToUse = destination
3939
}
40-
let titleToUse: String?
41-
if let t = title, t.isEmpty {
42-
titleToUse = nil
43-
} else {
44-
titleToUse = title
45-
}
4640

47-
try! self.init(.link(destination: destinationToUse, title: titleToUse, parsedRange: nil, children.map { $0.raw.markup }))
41+
try! self.init(.link(destination: destinationToUse, parsedRange: nil, children.map { $0.raw.markup }))
4842
}
4943

5044
/// Create a link with a destination and zero or more child inline elements.
@@ -55,33 +49,16 @@ public extension Link {
5549
/// The link's destination.
5650
var destination: String? {
5751
get {
58-
guard case let .link(destination, _) = _data.raw.markup.data else {
52+
guard case let .link(destination) = _data.raw.markup.data else {
5953
fatalError("\(self) markup wrapped unexpected \(_data.raw)")
6054
}
6155
return destination
6256
}
6357
set {
6458
if let d = newValue, d.isEmpty {
65-
_data = _data.replacingSelf(.link(destination: nil, title: title, parsedRange: nil, _data.raw.markup.copyChildren()))
66-
} else {
67-
_data = _data.replacingSelf(.link(destination: newValue, title: title, parsedRange: nil, _data.raw.markup.copyChildren()))
68-
}
69-
}
70-
}
71-
72-
/// The link's title.
73-
var title: String? {
74-
get {
75-
guard case let .link(_, title) = _data.raw.markup.data else {
76-
fatalError("\(self) markup wrapped unexpected \(_data.raw)")
77-
}
78-
return title
79-
}
80-
set {
81-
if let t = newValue, t.isEmpty {
82-
_data = _data.replacingSelf(.link(destination: destination, title: nil, parsedRange: nil, _data.raw.markup.copyChildren()))
59+
_data = _data.replacingSelf(.link(destination: nil, parsedRange: nil, _data.raw.markup.copyChildren()))
8360
} else {
84-
_data = _data.replacingSelf(.link(destination: destination, title: newValue, parsedRange: nil, _data.raw.markup.copyChildren()))
61+
_data = _data.replacingSelf(.link(destination: newValue, parsedRange: nil, _data.raw.markup.copyChildren()))
8562
}
8663
}
8764
}

Sources/Markdown/Parser/CommonMarkConverter.swift

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2021-2023 Apple Inc. and the Swift project authors
4+
Copyright (c) 2021 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See https://swift.org/LICENSE.txt for license information
@@ -452,37 +452,21 @@ struct MarkupParser {
452452
let parsedRange = state.range(state.node)
453453
let childConversion = convertChildren(state)
454454
let destination = String(cString: cmark_node_get_url(state.node))
455-
let title = String(cString: cmark_node_get_title(state.node))
456455
precondition(childConversion.state.node == state.node)
457456
precondition(childConversion.state.event == CMARK_EVENT_EXIT)
458-
return MarkupConversion(
459-
state: childConversion.state.next(),
460-
result: .link(
461-
destination: destination.isEmpty ? nil : destination,
462-
title: title.isEmpty ? nil : title,
463-
parsedRange: parsedRange,
464-
childConversion.result
465-
)
466-
)
457+
return MarkupConversion(state: childConversion.state.next(), result: .link(destination: destination, parsedRange: parsedRange, childConversion.result))
467458
}
468459

469460
private static func convertImage(_ state: MarkupConverterState) -> MarkupConversion<RawMarkup> {
470461
precondition(state.event == CMARK_EVENT_ENTER)
471462
precondition(state.nodeType == .image)
472463
let parsedRange = state.range(state.node)
473464
let childConversion = convertChildren(state)
474-
let source = String(cString: cmark_node_get_url(state.node))
465+
let destination = String(cString: cmark_node_get_url(state.node))
475466
let title = String(cString: cmark_node_get_title(state.node))
476467
precondition(childConversion.state.node == state.node)
477468
precondition(childConversion.state.event == CMARK_EVENT_EXIT)
478-
return MarkupConversion(
479-
state: childConversion.state.next(),
480-
result: .image(
481-
source: source.isEmpty ? nil : source,
482-
title: title.isEmpty ? nil : title,
483-
parsedRange: parsedRange, childConversion.result
484-
)
485-
)
469+
return MarkupConversion(state: childConversion.state.next(), result: .image(source: destination, title: title, parsedRange: parsedRange, childConversion.result))
486470
}
487471

488472
private static func convertStrikethrough(_ state: MarkupConverterState) -> MarkupConversion<RawMarkup> {

Tests/MarkdownTests/Inline Nodes/LinkTests.swift

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -48,35 +48,4 @@ class LinkTests: XCTestCase {
4848
link.destination = "test.example.com"
4949
XCTAssertFalse(link.isAutolink)
5050
}
51-
52-
func testTitleLink() throws {
53-
let markdown = #"""
54-
[Example](example.com "The example title")
55-
[Example2](example2.com)
56-
[Example3]()
57-
"""#
58-
59-
let document = Document(parsing: markdown)
60-
XCTAssertEqual(document.childCount, 1)
61-
let paragraph = try XCTUnwrap(document.child(at: 0) as? Paragraph)
62-
XCTAssertEqual(paragraph.childCount, 5)
63-
64-
XCTAssertTrue(paragraph.child(at: 1) is SoftBreak)
65-
XCTAssertTrue(paragraph.child(at: 3) is SoftBreak)
66-
let linkWithTitle = try XCTUnwrap(paragraph.child(at: 0) as? Link)
67-
let linkWithoutTitle = try XCTUnwrap(paragraph.child(at: 2) as? Link)
68-
let linkWithoutDestination = try XCTUnwrap(paragraph.child(at: 4) as? Link)
69-
70-
XCTAssertEqual(try XCTUnwrap(linkWithTitle.child(at: 0) as? Text).string, "Example")
71-
XCTAssertEqual(linkWithTitle.destination, "example.com")
72-
XCTAssertEqual(linkWithTitle.title, "The example title")
73-
74-
XCTAssertEqual(try XCTUnwrap(linkWithoutTitle.child(at: 0) as? Text).string, "Example2")
75-
XCTAssertEqual(linkWithoutTitle.destination, "example2.com")
76-
XCTAssertEqual(linkWithoutTitle.title, nil)
77-
78-
XCTAssertEqual(try XCTUnwrap(linkWithoutDestination.child(at: 0) as? Text).string, "Example3")
79-
XCTAssertEqual(linkWithoutDestination.destination, nil)
80-
XCTAssertEqual(linkWithoutDestination.title, nil)
81-
}
8251
}

Tests/MarkdownTests/Visitors/MarkupFormatterTests.swift

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ class MarkupFormatterSimpleRoundTripTests: XCTestCase {
711711
func testRoundTripHardBreakWithImage() {
712712
let source = """
713713
This is some text.\(" ")
714-
![This is an image.](image.png)
714+
![This is an image.](image.png "")
715715
"""
716716
checkRoundTrip(for: source)
717717
checkCharacterEquivalence(for: source)
@@ -720,7 +720,7 @@ class MarkupFormatterSimpleRoundTripTests: XCTestCase {
720720
func testRoundTripSoftBreakWithImage() {
721721
let source = """
722722
This is some text.
723-
![This is an image.](image.png)
723+
![This is an image.](image.png "")
724724
"""
725725
checkRoundTrip(for: source)
726726
checkCharacterEquivalence(for: source)
@@ -1394,6 +1394,7 @@ class MarkupFormatterTableTests: XCTestCase {
13941394
"""
13951395

13961396
let document = Document(parsing: source)
1397+
13971398
let expectedDump = """
13981399
Document
13991400
└─ Table alignments: |l|c|r|
@@ -1413,7 +1414,7 @@ class MarkupFormatterTableTests: XCTestCase {
14131414
│ │ └─ Link destination: "https://apple.com"
14141415
│ │ └─ Text "Apple"
14151416
│ ├─ Cell
1416-
│ │ └─ Image source: "image.png"
1417+
│ │ └─ Image source: "image.png" title: ""
14171418
│ │ └─ Text "image"
14181419
│ └─ Cell
14191420
│ └─ Link destination: "https://swift.org"
@@ -1428,14 +1429,17 @@ class MarkupFormatterTableTests: XCTestCase {
14281429

14291430
let formatted = document.format()
14301431
let expected = """
1431-
|*A* |**B** |~C~ |
1432-
|:-------------------------|:-----------------:|------------------:|
1433-
|[Apple](https://apple.com)|![image](image.png)|<https://swift.org>|
1434-
|<br/> || |
1432+
|*A* |**B** |~C~ |
1433+
|:-------------------------|:--------------------:|------------------:|
1434+
|[Apple](https://apple.com)|![image](image.png "")|<https://swift.org>|
1435+
|<br/> || |
14351436
"""
1437+
14361438
XCTAssertEqual(expected, formatted)
1439+
print(formatted)
14371440

14381441
let reparsed = Document(parsing: formatted)
1442+
print(reparsed.debugDescription())
14391443
XCTAssertTrue(document.hasSameStructure(as: reparsed))
14401444
}
14411445

Tests/MarkdownTests/Visitors/MarkupTreeDumperTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2021-2023 Apple Inc. and the Swift project authors
4+
Copyright (c) 2021 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See https://swift.org/LICENSE.txt for license information
@@ -29,7 +29,7 @@ final class MarkupTreeDumperTests: XCTestCase {
2929
│ ├─ Link @3:39-3:50 #12 destination: "foo"
3030
│ │ └─ Text @3:40-3:44 #13 "link"
3131
│ ├─ Text @3:50-3:51 #14 " "
32-
│ ├─ Image @3:51-3:64 #15 source: "foo"
32+
│ ├─ Image @3:51-3:64 #15 source: "foo" title: ""
3333
│ │ └─ Text @3:53-3:58 #16 "image"
3434
│ └─ Text @3:64-3:65 #17 "."
3535
├─ UnorderedList @5:1-9:1 #18

0 commit comments

Comments
 (0)