Skip to content

Conversation

@muescha
Copy link
Contributor

@muescha muescha commented Jan 5, 2021

Description

changed:

  • changed in readme filename to my-awesome-image.png
  • changed in readme the generated alt tag to match the file name
  • fixed in docs the generated alt tag to match the file name (in line 67 - not visible in diff)

because the default alt description is generated from file name (defaultAlt):

// Generate default alt tag
const srcSplit = getImageInfo(node.url).url.split(`/`)
const fileName = srcSplit[srcSplit.length - 1]
const fileNameNoExt = fileName.replace(/\.[^/.]+$/, ``)
const defaultAlt = fileNameNoExt.replace(/[^A-Z0-9]/gi, ` `)
const isEmptyAlt = node.alt === EMPTY_ALT
const alt = isEmptyAlt
? ``
: _.escape(
overWrites.alt ? overWrites.alt : node.alt ? node.alt : defaultAlt
)

Documentation

Related Issues

@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Jan 5, 2021
@muescha
Copy link
Contributor Author

muescha commented Jan 5, 2021

should and info added about an empty []?

By default, the text `my awesome image` will be used as 
-the alt attribute of the generated `img` tag.
+the alt attribute of the generated `img` tag if it is empty: `![](./my-awesome-image.png)`

there are 3 possible situations

![Alt text here](./my-awesome-image.png)    => alt="Alt text here"
![](./my-awesome-image.png)                 => alt="my awesome image"
![GATSBY_EMPTY_ALT](./my-awesome-image.png) => alt=""

@LekoArts LekoArts removed the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Jan 7, 2021
@LekoArts
Copy link
Contributor

LekoArts commented Jan 7, 2021

Hi!

The changes you made are incorrect. You can check this with the tests:

describe(`image alt attribute`, () => {
it(`should be generated correctly`, async () => {
const imagePath = `images/my-image.jpeg`
const content = `![testing-if-alt-is-correct](./${imagePath} "some title")`
const nodes = await plugin(createPluginOptions(content, imagePath))
expect(nodes.length).toBe(1)
const node = nodes.pop()
const $ = cheerio.load(node.value)
expect($(`img`).attr(`alt`)).toEqual(`testing-if-alt-is-correct`)
})
it(`should use escaped filename as fallback`, async () => {
const imagePath = `images/my-image.jpeg`
const content = `![](./${imagePath} "some title")`
const nodes = await plugin(createPluginOptions(content, imagePath))
expect(nodes.length).toBe(1)
const node = nodes.pop()
const $ = cheerio.load(node.value)
expect($(`img`).attr(`alt`)).toEqual(`my image`)
})
it(`should be able to consider EMPTY_ALT`, async () => {
const imagePath = `images/my-image.jpeg`
const content = `![GATSBY_EMPTY_ALT](./${imagePath} "some title")`
const nodes = await plugin(createPluginOptions(content, imagePath))
expect(nodes.length).toBe(1)
const node = nodes.pop()
const $ = cheerio.load(node.value)
expect($(`img`).attr(`alt`)).toEqual(``)
})
})

This here means:

const alt = isEmptyAlt
? ``
: _.escape(
overWrites.alt ? overWrites.alt : node.alt ? node.alt : defaultAlt
)

  • If the override for empty alt is given, return ''
  • If overWrites.alt is given, use that
  • If overWrites.alt is not given, check if node.alt (coming from the [desc here]) exists and use it
  • Otherwise use defaultAlt

@LekoArts LekoArts closed this Jan 7, 2021
@muescha muescha deleted the patch-6 branch January 7, 2021 13:52
@muescha
Copy link
Contributor Author

muescha commented Jan 7, 2021

yes - you are right

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants