Skip to content

Commit

Permalink
fixup! Render alternate representations as node variants
Browse files Browse the repository at this point in the history
  • Loading branch information
anferbui committed Dec 11, 2024
1 parent 4242b33 commit ad4d279
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions Sources/SwiftDocC/Model/Rendering/RenderNodeTranslator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1852,21 +1852,30 @@ public struct RenderNodeTranslator: SemanticVisitor {
private func variants(for documentationNode: DocumentationNode) -> [RenderNode.Variant] {
let generator = PresentationURLGenerator(context: context, baseURL: bundle.baseURL)

var allVariants: [SourceLanguage: [ResolvedTopicReference]] = Dictionary(uniqueKeysWithValues: documentationNode.availableSourceLanguages.map { ($0, [identifier]) })
func mapSourceLanguageToVariants(identifier: ResolvedTopicReference, sourceLanguages: Set<SourceLanguage>) -> [(SourceLanguage, [ResolvedTopicReference])] {
sourceLanguages.map { ($0, [identifier]) }
}

var allVariants: [SourceLanguage: [ResolvedTopicReference]] = Dictionary(
mapSourceLanguageToVariants(identifier: identifier, sourceLanguages: documentationNode.availableSourceLanguages)
) { value1, value2 in value1 + value2 }

// Apply alternate representations
documentationNode.metadata?.alternateRepresentations.forEach { alternateRepresentation in
// Only counterparts which were able to be resolved to a reference should be included as an alternate representation.
// Unresolved counterparts can be ignored, as they would have been reported during link resolution.
guard case .resolved(.success(let counterpartReference)) = alternateRepresentation.reference else {
return
}
if let alternateRepresentations = documentationNode.metadata?.alternateRepresentations {
for alternateRepresentation in alternateRepresentations {
// Only alternate representations which were able to be resolved to a reference should be included as an alternate representation.
// Unresolved alternate representations can be ignored, as they would have been reported during link resolution.
guard case .resolved(.success(let alternateRepresentationReference)) = alternateRepresentation.reference else {
continue
}

// Add all of the variants of the counterpart as additional variants for the current symbol
// If the current symbol and its counterpart share source languages, the list of variants for that language will contain multiple symbol references.
// Only the first symbol reference will be respected by Swift-DocC Render.
counterpartReference.sourceLanguages.forEach {
allVariants[$0, default: []].append(counterpartReference)
// Add the language representations of the alternate symbol as additional variants for the current symbol.
// Symbols can only specify custom alternate language representations for languages that the documented symbol doesn't already have a representation for.
// If the current symbol and its custom alternate representation share language representations, the custom language representation is ignored.
allVariants.merge(mapSourceLanguageToVariants(
identifier: alternateRepresentationReference,
sourceLanguages: alternateRepresentationReference.sourceLanguages
)) { value1, value2 in value1 }
}
}

Expand Down

0 comments on commit ad4d279

Please sign in to comment.