-
Notifications
You must be signed in to change notification settings - Fork 166
Add a helper function for rendering page breadcrumbs as HTML #1378
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
d-ronnqvist
merged 2 commits into
swiftlang:main
from
d-ronnqvist:output-html-breadcrumbs
Dec 5, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /* | ||
| This source file is part of the Swift.org open source project | ||
|
|
||
| Copyright (c) 2025 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 | ||
| */ | ||
|
|
||
| #if canImport(FoundationXML) | ||
| // TODO: Consider other HTML rendering options as a future improvement (rdar://165755530) | ||
| package import FoundationXML | ||
| package import FoundationEssentials | ||
| #else | ||
| package import Foundation | ||
| #endif | ||
|
|
||
| package extension MarkdownRenderer { | ||
| /// Creates an HTML element for the breadcrumbs that lead to the renderer's current page. | ||
| func breadcrumbs(references: [URL], currentPageNames: LinkedElement.Names) -> XMLNode { | ||
| // Breadcrumbs handle symbols differently than most elements in that everything uses a default style (no "code voice") | ||
| func nameElements(for names: LinkedElement.Names) -> [XMLNode] { | ||
| switch names { | ||
| case .single(.conceptual(let name)), .single(.symbol(let name)): | ||
| return [.text(name)] | ||
|
|
||
| case .languageSpecificSymbol(let namesByLanguageID): | ||
| let names = RenderHelpers.sortedLanguageSpecificValues(namesByLanguageID) | ||
| return switch goal { | ||
| case .richness: | ||
| if names.count == 1 { | ||
| [.text(names.first!.value)] | ||
| } else { | ||
| names.map { language, name in | ||
| // Wrap the name in a span so that it can be given a language specific "class" attribute. | ||
| .element(named: "span", children: [.text(name)], attributes: ["class": "\(language.id)-only"]) | ||
| } | ||
| } | ||
| case .conciseness: | ||
| // If the goal is conciseness, only display the primary language's name | ||
| names.first.map { _, name in [.text(name)] } ?? [] | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Create links for each of the breadcrumbs | ||
| var items: [XMLNode] = references.compactMap { | ||
| linkProvider.element(for: $0).map { page in | ||
| .element(named: "li", children: [ | ||
| .element(named: "a", children: nameElements(for: page.names), attributes: ["href": self.path(to: page.path)]) | ||
| ]) | ||
| } | ||
| } | ||
|
|
||
| // Add the name of the current page. It doesn't display as a link because it would refer to the current page. | ||
| items.append( | ||
| .element(named: "li", children: nameElements(for: currentPageNames)) | ||
| ) | ||
| let list = XMLNode.element(named: "ul", children: items) | ||
|
|
||
| return switch goal { | ||
| case .conciseness: list // If the goal is conciseness, don't wrap the list in a `<nav>` HTML element with an "id". | ||
| case .richness: .element(named: "nav", children: [list], attributes: ["id": "breadcrumbs"]) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we want this behavior if there is only one language-specific name? If there is a language-specific name, even if there is only one, wouldn't we want to go through the logic in the else block that applies a "class" attribute (which I assume is used for formatting)?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good question that I don't know if I've written down the answer to anywhere.
The idea is that the language specific elements would have classes like "swift-only" or "occ-only" that can be shown or hidden through very small CSS changes. For example, consider this HTML that represents a link to a method with different names in Swift and Objective-C:
If we add this CSS to the page:
the rendered page will only display the Swift title for that link. If we flip the CSS, then the rendered page will only display the Objective-C title for that link.
If the symbol has the same title on both languages, or is only available in a single language,then it's unnecessary to have 2
<code>elements, and the HTML can be simplified to just: