Skip to content
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

Support SVG images defined in metadata #68

Merged
merged 1 commit into from
Jan 23, 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 src/components/extension-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const FinerDetails = styled.div`
const Logo = ({ extension }) => {
return (
<LogoImage>
<ExtensionImage extension={extension} />
<ExtensionImage extension={extension} size={80} />
</LogoImage>
)
}
Expand Down
12 changes: 8 additions & 4 deletions src/components/extension-image.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import * as React from "react"
import { GatsbyImage, getImage, StaticImage } from "gatsby-plugin-image"

const ExtensionImage = ({ extension }) => {
const ExtensionImage = ({ extension, size }) => {
const metadata = extension.metadata
const sourceControl = metadata.sourceControl

let imageData
let imageData, svgData
let altText
if (metadata && metadata.icon) {
imageData = getImage(metadata.icon)
if (!imageData) {
svgData = metadata.icon.publicURL
}
altText = "The icon of the project"
} else if (sourceControl?.projectImage) {
imageData = getImage(sourceControl.projectImage)
Expand All @@ -17,8 +20,9 @@ const ExtensionImage = ({ extension }) => {
imageData = getImage(sourceControl.ownerImage)
altText = "The icon of the organisation"
}

if (imageData) {
if (svgData) {
return <img src={svgData} alt={altText} height={size} width={size} />
} else if (imageData) {
return <GatsbyImage layout="constrained" image={imageData} alt={altText} />
} else {
return (
Expand Down
36 changes: 35 additions & 1 deletion src/components/extension-image.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render, screen } from "@testing-library/react"
import { cleanup, render, screen } from "@testing-library/react"
import React from "react"
import ExtensionImage from "./extension-image"

Expand Down Expand Up @@ -114,5 +114,39 @@ describe("the extension image", () => {

expect(image.src).toContain("yaml-logo.png")
})

describe("when the image is an svg", () => {
const extension = {
metadata: {
icon: {
childImageSharp: null,
publicURL: "some-svg.svg",
},
sourceControl: {
projectImage: {
childImageSharp: {
gatsbyImageData: "social-logo.png",
},
},
ownerImage: {
childImageSharp: {
gatsbyImageData: "owner-logo.png",
},
},
},
},
}

beforeEach(() => {
cleanup()
render(<ExtensionImage extension={extension} />)
})

it("renders the owner image", () => {
const image = screen.getByRole("img", {})

expect(image.src).toContain("some-svg.svg")
})
})
})
})
3 changes: 2 additions & 1 deletion src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ export const pageQuery = graphql`
}
icon {
childImageSharp {
gatsbyImageData(width: 220)
gatsbyImageData(width: 80)
}
publicURL
}
sourceControl {
projectImage {
Expand Down
3 changes: 2 additions & 1 deletion src/templates/extension-detail.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ const DocumentationHeading = styled.h2`
const Logo = ({ extension }) => {
return (
<LogoImage>
<ExtensionImage extension={extension} />
<ExtensionImage extension={extension} size={220} />
</LogoImage>
)
}
Expand Down Expand Up @@ -274,6 +274,7 @@ export const pageQuery = graphql`
childImageSharp {
gatsbyImageData(width: 220)
}
publicURL
}
maven {
version
Expand Down