|
| 1 | +/* |
| 2 | + This source file is part of the Swift.org open source project |
| 3 | + |
| 4 | + Copyright (c) 2025 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 | +#if canImport(FoundationXML) |
| 12 | +// TODO: Consider other HTML rendering options as a future improvement (rdar://165755530) |
| 13 | +package import FoundationXML |
| 14 | +package import FoundationEssentials |
| 15 | +#else |
| 16 | +package import Foundation |
| 17 | +#endif |
| 18 | + |
| 19 | +package extension MarkdownRenderer { |
| 20 | + /// Creates an HTML element for the breadcrumbs leading up to the renderer's current page. |
| 21 | + func breadcrumbs(references: [URL], currentPageNames: LinkedElement.Names) -> XMLNode { |
| 22 | + // Breadcrumbs handle symbols differently than most elements in that everything uses a default style (no "code voice") |
| 23 | + func nameElements(for names: LinkedElement.Names) -> [XMLNode] { |
| 24 | + switch names { |
| 25 | + case .single(.conceptual(let name)), .single(.symbol(let name)): |
| 26 | + return [.text(name)] |
| 27 | + |
| 28 | + case .languageSpecificSymbol(let namesByLanguageID): |
| 29 | + let names = RenderHelpers.sortedLanguageSpecificValues(namesByLanguageID) |
| 30 | + return switch goal { |
| 31 | + case .richness: |
| 32 | + if names.count == 1 { |
| 33 | + [.text(names.first!.value)] |
| 34 | + } else { |
| 35 | + names.map { language, name in |
| 36 | + // Wrap the name in a span so that it can be given a language specific "class" attribute. |
| 37 | + .element(named: "span", children: [.text(name)], attributes: ["class": "\(language.id)-only"]) |
| 38 | + } |
| 39 | + } |
| 40 | + case .conciseness: |
| 41 | + // If the goal is conciseness, only display the primary language's name |
| 42 | + names.first.map { _, name in [.text(name)] } ?? [] |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + // Create links for each of the breadcrumbs |
| 48 | + var items: [XMLNode] = references.compactMap { |
| 49 | + linkProvider.element(for: $0).map { page in |
| 50 | + .element(named: "li", children: [ |
| 51 | + .element(named: "a", children: nameElements(for: page.names), attributes: ["href": self.path(to: page.path)]) |
| 52 | + ]) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // Add the name of the current page. It doesn't display as a link because it would refer to the current page. |
| 57 | + items.append( |
| 58 | + .element(named: "li", children: nameElements(for: currentPageNames)) |
| 59 | + ) |
| 60 | + let list = XMLNode.element(named: "ul", children: items) |
| 61 | + |
| 62 | + return switch goal { |
| 63 | + case .conciseness: list // If the goal is conciseness, don't wrap the list in a `<nav>` HTML element with an "id". |
| 64 | + case .richness: .element(named: "nav", children: [list], attributes: ["id": "breadcrumbs"]) |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments