Skip to content

Commit

Permalink
chore(build,content,server): allow mp3/mp4/ogg/ttf/webm attachments
Browse files Browse the repository at this point in the history
Allows these files to be used in live samples.
  • Loading branch information
caugner committed Nov 16, 2022
1 parent 79936fa commit 26689b5
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 20 deletions.
12 changes: 6 additions & 6 deletions build/check-images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import path from "path";

import sizeOf from "image-size";

import { Document, Image } from "../content";
import { Document, FileAttachment } from "../content";
import { FLAW_LEVELS } from "../libs/constants";
import { findMatchesInText } from "./matches-in-text";
import { DEFAULT_LOCALE } from "../libs/constants";
Expand Down Expand Up @@ -133,12 +133,12 @@ export function checkImageReferences(doc, $, options, { url, rawContent }) {
// but all our images are going to be static.
finalSrc = absoluteURL.pathname;
// We can use the `finalSrc` to look up and find the image independent
// of the correct case because `Image.findByURL` operates case
// of the correct case because `FileAttachment.findByURL` operates case
// insensitively.

// What follows uses the same algorithm as Image.findByURLWithFallback
// What follows uses the same algorithm as FileAttachment.findByURLWithFallback
// but only adds a filePath if it exists for the DEFAULT_LOCALE
const filePath = Image.findByURL(finalSrc);
const filePath = FileAttachment.findByURL(finalSrc);
let enUSFallback = false;
if (
!filePath &&
Expand All @@ -149,7 +149,7 @@ export function checkImageReferences(doc, $, options, { url, rawContent }) {
new RegExp(`^/${doc.locale}/`, "i"),
`/${DEFAULT_LOCALE}/`
);
if (Image.findByURL(enUSFinalSrc)) {
if (FileAttachment.findByURL(enUSFinalSrc)) {
// Use the en-US src instead
finalSrc = enUSFinalSrc;
// Note that this `<img src="...">` value can work if you use the
Expand Down Expand Up @@ -350,7 +350,7 @@ export function checkImageWidths(doc, $, options, { rawContent }) {
);
}
} else if (!imgSrc.includes("://") && imgSrc.startsWith("/")) {
const filePath = Image.findByURLWithFallback(imgSrc);
const filePath = FileAttachment.findByURLWithFallback(imgSrc);
if (filePath) {
const dimensions = sizeOf(filePath);
img.attr("width", `${dimensions.width}`);
Expand Down
4 changes: 2 additions & 2 deletions build/flaws/broken-links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import path from "path";
import fromMarkdown from "mdast-util-from-markdown";
import visit from "unist-util-visit";

import { Document, Redirect, Image } from "../../content";
import { Document, Redirect, FileAttachment } from "../../content";
import { findMatchesInText } from "../matches-in-text";
import {
DEFAULT_LOCALE,
Expand Down Expand Up @@ -271,7 +271,7 @@ export function getBrokenLinksFlaws(doc, $, { rawContent }, level) {
const found = Document.findByURL(hrefNormalized);
if (!found) {
// Before we give up, check if it's an image.
if (!Image.findByURLWithFallback(hrefNormalized)) {
if (!FileAttachment.findByURLWithFallback(hrefNormalized)) {
// Even if it's a redirect, it's still a flaw, but it'll be nice to
// know what it *should* be.
const resolved = Redirect.resolve(hrefNormalized);
Expand Down
12 changes: 6 additions & 6 deletions build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
MacroRedirectedLinkError,
} from "../kumascript/src/errors";

import { Document, Image, execGit } from "../content";
import { Document, FileAttachment, execGit } from "../content";
import { CONTENT_ROOT, REPOSITORY_URLS } from "../libs/env";
import * as kumascript from "../kumascript";

Expand Down Expand Up @@ -167,7 +167,7 @@ function postLocalFileLinks($, doc) {
// So we'll look-up a lot "false positives" that are not images.
// Thankfully, this lookup is fast.
const url = `${doc.mdn_url}/${href}`;
const image = Image.findByURLWithFallback(url);
const image = FileAttachment.findByURLWithFallback(url);
if (image) {
$(element).attr("href", url);
}
Expand Down Expand Up @@ -265,14 +265,14 @@ function makeTOC(doc) {
*
* @param {Document} document
*/
function getAdjacentImages(documentDirectory) {
function getAdjacentFileAttachments(documentDirectory) {
const dirents = fs.readdirSync(documentDirectory, { withFileTypes: true });
return dirents
.filter((dirent) => {
// This needs to match what we do in filecheck/checker.py
return (
!dirent.isDirectory() &&
/\.(png|jpeg|jpg|gif|svg|webp)$/i.test(dirent.name)
/\.(mp3|mp4|png|jpeg|jpg|gif|ogg|svg|ttf|webm|webp)$/i.test(dirent.name)
);
})
.map((dirent) => path.join(documentDirectory, dirent.name));
Expand Down Expand Up @@ -501,8 +501,8 @@ export async function buildDocument(
// The checkImageReferences() does 2 things. Checks image *references* and
// it returns which images it checked. But we'll need to complement any
// other images in the folder.
getAdjacentImages(path.dirname(document.fileInfo.path)).forEach((fp) =>
fileAttachments.add(fp)
getAdjacentFileAttachments(path.dirname(document.fileInfo.path)).forEach(
(fp) => fileAttachments.add(fp)
);

// Check the img tags for possible flaws and possible build-time rewrites
Expand Down
14 changes: 13 additions & 1 deletion content/image.ts → content/file-attachment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ import { DEFAULT_LOCALE } from "../libs/constants";
import { ROOTS } from "../libs/env";
import { memoize, slugToFolder } from "./utils";

function isFileAttachment(filePath: string) {
return isAudio(filePath) || isVideo(filePath) || isImage(filePath);
}

function isAudio(filePath) {
return /\.(mp3|ogg)$/i.test(filePath);
}

function isVideo(filePath) {
return /\.(mp4|webm)$/i.test(filePath);
}

function isImage(filePath) {
if (fs.statSync(filePath).isDirectory()) {
return false;
Expand Down Expand Up @@ -38,7 +50,7 @@ function urlToFilePath(url) {

const find = memoize((relativePath: string) => {
return ROOTS.map((root) => path.join(root, relativePath)).find(
(filePath) => fs.existsSync(filePath) && isImage(filePath)
(filePath) => fs.existsSync(filePath) && isFileAttachment(filePath)
);
});

Expand Down
2 changes: 1 addition & 1 deletion content/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export * as Document from "./document";
export * as Translation from "./translation";
export { getPopularities } from "./popularities";
export * as Redirect from "./redirect";
export * as Image from "./image";
export * as FileAttachment from "./file-attachment";
export { buildURL, memoize, slugToFolder, execGit, getRoot } from "./utils";
export { resolveFundamental } from "../libs/fundamental-redirects";
export { translationsOf } from "./translations";
8 changes: 4 additions & 4 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
renderContributorsTxt,
} from "../build";
import { findDocumentTranslations } from "../content/translations";
import { Document, Redirect, Image } from "../content";
import { Document, Redirect, FileAttachment } from "../content";
import { CONTENT_ROOT, CONTENT_TRANSLATED_ROOT } from "../libs/env";
import { CSP_VALUE, DEFAULT_LOCALE } from "../libs/constants";
import {
Expand Down Expand Up @@ -224,12 +224,12 @@ app.get("/*", async (req, res, ...args) => {

// TODO: Would be nice to have a list of all supported file extensions
// in a constants file.
if (/\.(png|webp|gif|jpe?g|svg)$/.test(req.path)) {
// Remember, Image.findByURLWithFallback() will return the absolute file path
if (/\.(gif|jpe?g|mp3|mp4|png|ogg|svg|ttf|webm|webp)$/.test(req.path)) {
// Remember, FileAttachment.findByURLWithFallback() will return the absolute file path
// iff it exists on disk.
// Using a "fallback" strategy here so that images embedded in live samples
// are resolved if they exist in en-US but not in <locale>
const filePath = Image.findByURLWithFallback(req.path);
const filePath = FileAttachment.findByURLWithFallback(req.path);
if (filePath) {
// The second parameter to `send()` has to be either a full absolute
// path or a path that doesn't start with `../` otherwise you'd
Expand Down

0 comments on commit 26689b5

Please sign in to comment.