Skip to content

Commit

Permalink
Add identifier property to DataAsset
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle-Ye committed Oct 22, 2021
1 parent 819648f commit 1bb62dd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ struct DataAssetManager {

// Store the image with given scale information and display scale.
let name = URL(fileURLWithPath: meta.reference).lastPathComponent
storage[name, default: DataAsset()]
storage[name, default: DataAsset(identifier: name)]
.register(dataURL, with: meta.traits)

if name.contains(".") {
Expand Down Expand Up @@ -173,8 +173,15 @@ public struct DataAsset: Codable {
/// The context in which you intend to use the data asset.
public var context = Context.display

/// Creates an empty asset.
public init() {}
/// The identifier of the asset.
///
/// This is typically the base name of the asset and its extension, for example "`image.png`".
public var identifier: String

/// Creates an DataAsset with the identifier.
public init(identifier: String) {
self.identifier = identifier
}

/// Registers a variant of the asset.
/// - Parameters:
Expand Down Expand Up @@ -206,6 +213,11 @@ public struct DataAsset: Codable {
return BundleData(url: variant, traitCollection: traitCollection)
}

/// Creates an empty asset.
@available(*, deprecated, message: "Please use init(identifier:) instead")
public init() {
self.identifier = ""
}
}

/// A collection of environment traits for an asset variant.
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftDocC/Model/Rendering/RenderNodeTranslator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1231,12 +1231,12 @@ public struct RenderNodeTranslator: SemanticVisitor {
// Check if media is a supported image.
if let resolvedImages = resolveAsset(ofType: .image)
{
mediaReference = RenderReferenceIdentifier(media.path)
mediaReference = RenderReferenceIdentifier(resolvedImages.identifier)

imageReferences[media.path] = ImageReference(
identifier: mediaReference,
// If no alt text has been provided and this image has been registered previously, use the registered alt text.
altText: altText ?? imageReferences[media.path]?.altText,
altText: altText ?? imageReferences[resolvedImages.identifier]?.altText,
imageAsset: resolvedImages
)
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftDocCUtilitiesTests/ConvertActionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2053,7 +2053,7 @@ private extension LinkDestinationSummary {
private extension ImageReference {
// A convenience initializer for test data.
init(name: String, altText: String?, userInterfaceStyle: UserInterfaceStyle, displayScale: DisplayScale) {
var asset = DataAsset()
var asset = DataAsset(identifier: name)
asset.register(
URL(string: "/images/\(name)")!,
with: DataTraitCollection(userInterfaceStyle: userInterfaceStyle, displayScale: displayScale)
Expand Down

0 comments on commit 1bb62dd

Please sign in to comment.