Skip to content

Commit

Permalink
Image flaw checking should be different for translated content
Browse files Browse the repository at this point in the history
Part of mdn#3174
  • Loading branch information
peterbe committed Mar 9, 2021
1 parent b6b9538 commit 268fc90
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
26 changes: 24 additions & 2 deletions build/check-images.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,34 @@ function checkImageReferences(doc, $, options, { url, rawContent }) {
// We can use the `finalSrc` to look up and find the image independent
// of the correct case because `Image.findByURL` operates case
// insensitively.
const filePath = Image.findByURL(finalSrc);
let filePath = Image.findByURL(finalSrc);
let enUSFallback = false;
if (
!filePath &&
doc.locale !== "en-US" &&
!finalSrc.startsWith("/en-us/")
) {
const enFinalSrc = finalSrc.replace(
`/${doc.locale.toLowerCase()}/`,
"/en-us/"
);
if (Image.findByURL(enFinalSrc)) {
// Use the en-US src instead
finalSrc = enFinalSrc;
// Note that this `<img src="...">` value can work if you use the
// en-US equivalent URL instead.
enUSFallback = true;
}
}
if (filePath) {
filePaths.add(filePath);
}

if (checkImages) {
if (!filePath) {
if (enUSFallback) {
// If it worked by switching to the en-US src, don't do anything more.
// Do nothing! I.e. don't try to perfect the spelling.
} else if (!filePath) {
// E.g. <img src="doesnotexist.png"
addImageFlaw(img, src, {
explanation: "File not present on disk",
Expand Down Expand Up @@ -178,6 +199,7 @@ function checkImageReferences(doc, $, options, { url, rawContent }) {
}
}
}
console.log("SETTING src=", finalSrc);
img.attr("src", finalSrc);
}
if (
Expand Down
1 change: 1 addition & 0 deletions build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ async function buildDocument(document, documentOptions = {}) {

// Check and scrutinize any local image references
const fileAttachments = checkImageReferences(doc, $, options, document);
console.log("fileAttachments:", fileAttachments);

// Check the img tags for possible flaws and possible build-time rewrites
checkImageWidths(doc, $, options, document);
Expand Down
5 changes: 5 additions & 0 deletions testing/translated-content/files/fr/web/foo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@
---

<p>Foë</p>

<figure>
<img src="screenshot.png" alt="Capture d'écran des couleurs" />
<figcaption>Une image parfaitement normale</figcaption>
</figure>

0 comments on commit 268fc90

Please sign in to comment.