Skip to content

Commit

Permalink
downloading external images for translated-content (#3207)
Browse files Browse the repository at this point in the history
* downloading external images for translated-content

Fixes #3174

* eslint
  • Loading branch information
peterbe authored Mar 12, 2021
1 parent 9710df3 commit 0b000dc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
28 changes: 22 additions & 6 deletions build/check-images.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const sizeOf = require("image-size");
const { Document, Image } = require("../content");
const { FLAW_LEVELS } = require("./constants");
const { findMatchesInText } = require("./matches-in-text");
const { DEFAULT_LOCALE } = require("../libs/constants");

/**
* Mutate the `$` instance for image reference and if appropriate,
Expand Down Expand Up @@ -119,9 +120,24 @@ function checkImageReferences(doc, $, options, { url, rawContent }) {
// it now, we still want the full relative URL.
img.attr("src", absoluteURL.pathname);
} else {
let suggestion = null;
// If this document is *not* en-US, perhaps the external image has already
// been downloaded by the en-US equivalent. If so, make that the suggestion.
if (doc.locale !== DEFAULT_LOCALE) {
const filePath = Image.findByURL(
path.join(
doc.mdn_url.replace(`/${doc.locale}/`, `/${DEFAULT_LOCALE}/`),
path.basename(src)
)
);
if (filePath) {
suggestion = path.basename(filePath);
}
}
addImageFlaw(img, src, {
explanation: "External image URL",
externalImage: true,
suggestion,
});
}
}
Expand All @@ -137,16 +153,16 @@ function checkImageReferences(doc, $, options, { url, rawContent }) {
let enUSFallback = false;
if (
!filePath &&
doc.locale !== "en-US" &&
!finalSrc.startsWith("/en-us/")
doc.locale !== DEFAULT_LOCALE &&
!finalSrc.startsWith(`/${DEFAULT_LOCALE.toLowerCase()}/`)
) {
const enFinalSrc = finalSrc.replace(
const enUSFinalSrc = finalSrc.replace(
`/${doc.locale.toLowerCase()}/`,
"/en-us/"
`/${DEFAULT_LOCALE.toLowerCase()}/`
);
if (Image.findByURL(enFinalSrc)) {
if (Image.findByURL(enUSFinalSrc)) {
// Use the en-US src instead
finalSrc = enFinalSrc;
finalSrc = enUSFinalSrc;
// Note that this `<img src="...">` value can work if you use the
// en-US equivalent URL instead.
enUSFallback = true;
Expand Down
6 changes: 3 additions & 3 deletions build/flaws.js
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,9 @@ async function fixFixableFlaws(doc, options, document) {
// HTML. It's only proper HTML when the kumascript macros have been
// expanded.
let newSrc;
if (flaw.externalImage) {
if (flaw.suggestion) {
newSrc = flaw.suggestion;
} else {
// Sanity check that it's an external image
const url = new URL(forceExternalURL(flaw.src));
if (url.protocol !== "https:") {
Expand Down Expand Up @@ -739,8 +741,6 @@ async function fixFixableFlaws(doc, options, document) {
throw error;
}
}
} else {
newSrc = flaw.suggestion;
}
newRawHTML = replaceMatchesInText(flaw.src, newRawHTML, newSrc, {
inAttribute: "src",
Expand Down

0 comments on commit 0b000dc

Please sign in to comment.