Skip to content

Fix custom doc link title issue #376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 15, 2023
Merged
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
2 changes: 1 addition & 1 deletion Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 25 additions & 2 deletions Sources/SwiftDocC/Model/Rendering/RenderContentCompiler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,30 @@ struct RenderContentCompiler: MarkupVisitor {
return [RenderInlineContent.text(link.plainText)]
}

return [RenderInlineContent.reference(identifier: .init(resolved.absoluteString), isActive: true, overridingTitle: nil, overridingTitleInlineContent: nil)]
let linkTitleInlineContent = link.children.flatMap { visit($0) } as! [RenderInlineContent]
let plainTextLinkTitle = linkTitleInlineContent.plainText
let overridingTitle = plainTextLinkTitle.isEmpty ? nil : plainTextLinkTitle
let overridingTitleInlineContent = linkTitleInlineContent.isEmpty ? nil : linkTitleInlineContent

let useOverriding: Bool
if link.isAutolink { // If the link is an auto link, we don't use overriding info
useOverriding = false
} else if let overridingTitle = overridingTitle,
overridingTitle.hasPrefix(ResolvedTopicReference.urlScheme + ":"),
destination.hasPrefix(ResolvedTopicReference.urlScheme + "://"),
destination.hasSuffix(overridingTitle.dropFirst((ResolvedTopicReference.urlScheme + ":").count)) { // If the link is a transformed doc link, we don't use overriding info
useOverriding = false
} else {
useOverriding = true
}
return [
RenderInlineContent.reference(
identifier: .init(resolved.absoluteString),
isActive: true,
overridingTitle: useOverriding ? overridingTitle : nil,
overridingTitleInlineContent: useOverriding ? overridingTitleInlineContent : nil
)
]
}

mutating func resolveTopicReference(_ destination: String) -> ResolvedTopicReference? {
Expand Down Expand Up @@ -194,7 +217,7 @@ struct RenderContentCompiler: MarkupVisitor {
func resolveSymbolReference(destination: String) -> ResolvedTopicReference? {
if let cached = context.documentationCacheBasedLinkResolver.referenceFor(absoluteSymbolPath: destination, parent: identifier) {
return cached
}
}

// The symbol link may be written with a scheme and bundle identifier.
let url = ValidatedURL(parsingExact: destination)?.requiring(scheme: ResolvedTopicReference.urlScheme) ?? ValidatedURL(symbolPath: destination)
Expand Down
134 changes: 134 additions & 0 deletions Tests/SwiftDocCTests/Rendering/RenderContentCompilerTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
This source file is part of the Swift.org open source project

Copyright (c) 2023 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See https://swift.org/LICENSE.txt for license information
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

import Foundation
import Markdown
@testable import SwiftDocC
import XCTest

class RenderContentCompilerTests: XCTestCase {
func testLinkOverrideTitle() throws {
let (bundle, context) = try testBundleAndContext(named: "TestBundle")
var compiler = RenderContentCompiler(context: context, bundle: bundle, identifier: ResolvedTopicReference(bundleIdentifier: bundle.identifier, path: "/path", fragment: nil, sourceLanguage: .swift))

let source = """
[Example](http://example.com)

[Custom Title](doc:UNRESOVLED)

[Custom Title](doc:article)

[Custom Image Content ![random image](https://example.com/test.png)](doc:article2)

<doc:UNRESOVLED>

<doc:article3>
"""
let document = Document(parsing: source)
let expectedDump = """
Document
├─ Paragraph
│ └─ Link destination: "http://example.com"
│ └─ Text "Example"
├─ Paragraph
│ └─ Link destination: "doc:UNRESOVLED"
│ └─ Text "Custom Title"
├─ Paragraph
│ └─ Link destination: "doc:article"
│ └─ Text "Custom Title"
├─ Paragraph
│ └─ Link destination: "doc:article2"
│ ├─ Text "Custom Image Content "
│ └─ Image source: "https://example.com/test.png" title: ""
│ └─ Text "random image"
├─ Paragraph
│ └─ Link destination: "doc:UNRESOVLED"
│ └─ Text "doc:UNRESOVLED"
└─ Paragraph
└─ Link destination: "doc:article3"
└─ Text "doc:article3"
"""
XCTAssertEqual(document.debugDescription(), expectedDump)

let result = document.children.flatMap { compiler.visit($0) }
XCTAssertEqual(result.count, 6)

do {
guard case let .paragraph(paragraph) = result[0] as? RenderBlockContent else {
XCTFail("RenderCotent result is not the expected RenderBlockContent.paragraph(Paragraph)")
return
}
let link = RenderInlineContent.reference(
identifier: .init(forExternalLink: "http://example.com"),
isActive: true,
overridingTitle: nil,
overridingTitleInlineContent: nil
)
XCTAssertEqual(paragraph, RenderBlockContent.Paragraph(inlineContent: [link]))
}
do {
guard case let .paragraph(paragraph) = result[1] as? RenderBlockContent else {
XCTFail("RenderCotent result is not the expected RenderBlockContent.paragraph(Paragraph)")
return
}
let text = RenderInlineContent.text("Custom Title")
XCTAssertEqual(paragraph, RenderBlockContent.Paragraph(inlineContent: [text]))
}
do {
guard case let .paragraph(paragraph) = result[2] as? RenderBlockContent else {
XCTFail("RenderCotent result is not the expected RenderBlockContent.paragraph(Paragraph)")
return
}
let link = RenderInlineContent.reference(
identifier: .init("doc://org.swift.docc.example/documentation/Test-Bundle/article"),
isActive: true,
overridingTitle: "Custom Title",
overridingTitleInlineContent: [.text("Custom Title")])
XCTAssertEqual(paragraph, RenderBlockContent.Paragraph(inlineContent: [link]))
}
do {
guard case let .paragraph(paragraph) = result[3] as? RenderBlockContent else {
XCTFail("RenderCotent result is not the expected RenderBlockContent.paragraph(Paragraph)")
return
}
let link = RenderInlineContent.reference(
identifier: .init("doc://org.swift.docc.example/documentation/Test-Bundle/article2"),
isActive: true,
overridingTitle: "Custom Image Content ",
overridingTitleInlineContent: [
RenderInlineContent.text("Custom Image Content "),
RenderInlineContent.image(identifier: .init(forExternalLink: "https://example.com/test.png"), metadata: nil)
]
)
XCTAssertEqual(paragraph, RenderBlockContent.Paragraph(inlineContent: [link]))
}
do {
guard case let .paragraph(paragraph) = result[4] as? RenderBlockContent else {
XCTFail("RenderCotent result is not the expected RenderBlockContent.paragraph(Paragraph)")
return
}
let text = RenderInlineContent.text("doc:UNRESOVLED")
XCTAssertEqual(paragraph, RenderBlockContent.Paragraph(inlineContent: [text]))
}
do {
guard case let .paragraph(paragraph) = result[5] as? RenderBlockContent else {
XCTFail("RenderCotent result is not the expected RenderBlockContent.paragraph(Paragraph)")
return
}
let link = RenderInlineContent.reference(
identifier: .init("doc://org.swift.docc.example/documentation/Test-Bundle/article3"),
isActive: true,
overridingTitle: nil,
overridingTitleInlineContent: nil
)
XCTAssertEqual(paragraph, RenderBlockContent.Paragraph(inlineContent: [link]))
}
}
}