Skip to content

Commit 7b80cf8

Browse files
committed
[Bugfix] Fix custom doc link title issue
1 parent 79d268e commit 7b80cf8

File tree

3 files changed

+148
-2
lines changed

3 files changed

+148
-2
lines changed

Sources/SwiftDocC/Model/Rendering/RenderContentCompiler.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,18 @@ struct RenderContentCompiler: MarkupVisitor {
165165
return [RenderInlineContent.text(link.plainText)]
166166
}
167167

168-
return [RenderInlineContent.reference(identifier: .init(resolved.absoluteString), isActive: true, overridingTitle: nil, overridingTitleInlineContent: nil)]
168+
let linkTitleInlineContent = link.children.flatMap { visit($0) } as! [RenderInlineContent]
169+
let plainTextLinkTitle = linkTitleInlineContent.plainText
170+
let overridingTitle = plainTextLinkTitle.isEmpty ? nil : plainTextLinkTitle
171+
let overridingTitleInlineContent = linkTitleInlineContent.isEmpty ? nil : linkTitleInlineContent
172+
return [
173+
RenderInlineContent.reference(
174+
identifier: .init(resolved.absoluteString),
175+
isActive: true,
176+
overridingTitle: link.isAutolink ? nil : overridingTitle,
177+
overridingTitleInlineContent: link.isAutolink ? nil : overridingTitleInlineContent
178+
)
179+
]
169180
}
170181

171182
mutating func resolveTopicReference(_ destination: String) -> ResolvedTopicReference? {
@@ -194,7 +205,7 @@ struct RenderContentCompiler: MarkupVisitor {
194205
func resolveSymbolReference(destination: String) -> ResolvedTopicReference? {
195206
if let cached = context.documentationCacheBasedLinkResolver.referenceFor(absoluteSymbolPath: destination, parent: identifier) {
196207
return cached
197-
}
208+
}
198209

199210
// The symbol link may be written with a scheme and bundle identifier.
200211
let url = ValidatedURL(parsingExact: destination)?.requiring(scheme: ResolvedTopicReference.urlScheme) ?? ValidatedURL(symbolPath: destination)

Sources/SwiftDocC/Semantics/MarkupReferenceResolver.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ struct MarkupReferenceResolver: MarkupRewriter {
114114
}
115115
var link = link
116116
link.destination = resolvedURL.absoluteString
117+
link.isAutolink = link.isAutolink // Save the original isAutolink status
117118
return link
118119
}
119120

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright (c) 2023 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
10+
11+
import Foundation
12+
import Markdown
13+
@testable import SwiftDocC
14+
import XCTest
15+
16+
class RenderContentCompilerTests: XCTestCase {
17+
func testLinkOverrideTitle() throws {
18+
let (bundle, context) = try testBundleAndContext(named: "TestBundle")
19+
var compiler = RenderContentCompiler(context: context, bundle: bundle, identifier: ResolvedTopicReference(bundleIdentifier: bundle.identifier, path: "/path", fragment: nil, sourceLanguage: .swift))
20+
21+
let source = """
22+
[Example](http://example.com)
23+
24+
[Custom Title](doc:UNRESOVLED)
25+
26+
[Custom Title](doc:article)
27+
28+
[Custom Image Content ![random image](https://example.com/test.png)](doc:article2)
29+
30+
<doc:UNRESOVLED>
31+
32+
<doc:article3>
33+
"""
34+
let document = Document(parsing: source)
35+
let expectedDump = """
36+
Document
37+
├─ Paragraph
38+
│ └─ Link destination: "http://example.com"
39+
│ └─ Text "Example"
40+
├─ Paragraph
41+
│ └─ Link destination: "doc:UNRESOVLED"
42+
│ └─ Text "Custom Title"
43+
├─ Paragraph
44+
│ └─ Link destination: "doc:article"
45+
│ └─ Text "Custom Title"
46+
├─ Paragraph
47+
│ └─ Link destination: "doc:article2"
48+
│ ├─ Text "Custom Image Content "
49+
│ └─ Image source: "https://example.com/test.png" title: ""
50+
│ └─ Text "random image"
51+
├─ Paragraph
52+
│ └─ Link destination: "doc:UNRESOVLED"
53+
│ └─ Text "doc:UNRESOVLED"
54+
└─ Paragraph
55+
└─ Link destination: "doc:article3"
56+
└─ Text "doc:article3"
57+
"""
58+
XCTAssertEqual(document.debugDescription(), expectedDump)
59+
60+
let result = document.children.flatMap { compiler.visit($0) }
61+
XCTAssertEqual(result.count, 6)
62+
63+
do {
64+
guard case let .paragraph(paragraph) = result[0] as? RenderBlockContent else {
65+
XCTFail("RenderCotent result is not the expected RenderBlockContent.paragraph(Paragraph)")
66+
return
67+
}
68+
let link = RenderInlineContent.reference(
69+
identifier: .init(forExternalLink: "http://example.com"),
70+
isActive: true,
71+
overridingTitle: nil,
72+
overridingTitleInlineContent: nil
73+
)
74+
XCTAssertEqual(paragraph, RenderBlockContent.Paragraph(inlineContent: [link]))
75+
}
76+
do {
77+
guard case let .paragraph(paragraph) = result[1] as? RenderBlockContent else {
78+
XCTFail("RenderCotent result is not the expected RenderBlockContent.paragraph(Paragraph)")
79+
return
80+
}
81+
let text = RenderInlineContent.text("Custom Title")
82+
XCTAssertEqual(paragraph, RenderBlockContent.Paragraph(inlineContent: [text]))
83+
}
84+
do {
85+
guard case let .paragraph(paragraph) = result[2] as? RenderBlockContent else {
86+
XCTFail("RenderCotent result is not the expected RenderBlockContent.paragraph(Paragraph)")
87+
return
88+
}
89+
let link = RenderInlineContent.reference(
90+
identifier: .init("doc://org.swift.docc.example/documentation/Test-Bundle/article"),
91+
isActive: true,
92+
overridingTitle: "Custom Title",
93+
overridingTitleInlineContent: [.text("Custom Title")])
94+
XCTAssertEqual(paragraph, RenderBlockContent.Paragraph(inlineContent: [link]))
95+
}
96+
do {
97+
guard case let .paragraph(paragraph) = result[3] as? RenderBlockContent else {
98+
XCTFail("RenderCotent result is not the expected RenderBlockContent.paragraph(Paragraph)")
99+
return
100+
}
101+
let link = RenderInlineContent.reference(
102+
identifier: .init("doc://org.swift.docc.example/documentation/Test-Bundle/article2"),
103+
isActive: true,
104+
overridingTitle: "Custom Image Content ",
105+
overridingTitleInlineContent: [
106+
RenderInlineContent.text("Custom Image Content "),
107+
RenderInlineContent.image(identifier: .init(forExternalLink: "https://example.com/test.png"), metadata: nil)
108+
]
109+
)
110+
XCTAssertEqual(paragraph, RenderBlockContent.Paragraph(inlineContent: [link]))
111+
}
112+
do {
113+
guard case let .paragraph(paragraph) = result[4] as? RenderBlockContent else {
114+
XCTFail("RenderCotent result is not the expected RenderBlockContent.paragraph(Paragraph)")
115+
return
116+
}
117+
let text = RenderInlineContent.text("doc:UNRESOVLED")
118+
XCTAssertEqual(paragraph, RenderBlockContent.Paragraph(inlineContent: [text]))
119+
}
120+
do {
121+
guard case let .paragraph(paragraph) = result[5] as? RenderBlockContent else {
122+
XCTFail("RenderCotent result is not the expected RenderBlockContent.paragraph(Paragraph)")
123+
return
124+
}
125+
let link = RenderInlineContent.reference(
126+
identifier: .init("doc://org.swift.docc.example/documentation/Test-Bundle/article3"),
127+
isActive: true,
128+
overridingTitle: nil,
129+
overridingTitleInlineContent: nil
130+
)
131+
XCTAssertEqual(paragraph, RenderBlockContent.Paragraph(inlineContent: [link]))
132+
}
133+
}
134+
}

0 commit comments

Comments
 (0)