Skip to content

docs: Set fixed dimensions on absolute images#39296

Open
cpAdm wants to merge 1 commit intomicrosoft:mainfrom
cpAdm:docs-set-fixed-dimensions
Open

docs: Set fixed dimensions on absolute images#39296
cpAdm wants to merge 1 commit intomicrosoft:mainfrom
cpAdm:docs-set-fixed-dimensions

Conversation

@cpAdm
Copy link
Contributor

@cpAdm cpAdm commented Feb 17, 2026

By having a fixed width and height for images, room is reserved for the images, and thus preventing layout shifts.

Code snippet used to update the images (AI prompted)

import { promises as fs } from 'node:fs';
import path from 'node:path';
import process from 'node:process';
import { imageSize } from 'image-size';

const ROOT_DIR = path.resolve('./docs/src');

// Plain Markdown image syntax (no title support intentionally)
const IMAGE_REGEX = /!\[([^\]]*)]\((https?:\/\/[^\s)]+)\)/g;

async function main() {
  console.log(`Scanning: ${ROOT_DIR}`);
  await walk(ROOT_DIR);
  console.log('Done.');
}

async function walk(dir: string): Promise<void> {
  const entries = await fs.readdir(dir, { withFileTypes: true });

  for (const entry of entries) {
    const fullPath = path.join(dir, entry.name);

    if (entry.isDirectory())
      await walk(fullPath);
    else if (entry.isFile() && fullPath.endsWith('.md'))
      await processMarkdown(fullPath);
  }
}

async function processMarkdown(filePath: string) {
  let content = await fs.readFile(filePath, 'utf8');
  let modified = false;

  const matches = [...content.matchAll(IMAGE_REGEX)];

  for (const match of matches) {
    const [original, alt, url] = match;

    try {
      const dimensions = await getImageDimensions(url);

      if (!dimensions?.width || !dimensions?.height) {
        console.warn(`⚠ Could not determine size: ${url}`);
        continue;
      }

      const replacement = `<img src="${url}" alt="${escapeHtml(alt)}" width="${dimensions.width}" height="${dimensions.height}" />`;

      content = content.replace(original, replacement);
      modified = true;

      console.log(
          `✔ ${path.relative(ROOT_DIR, filePath)}${url} (${dimensions.width}x${dimensions.height})`
      );
    } catch (err) {
      console.warn(`✖ Failed: ${url}`);
    }
  }

  if (modified)
    await fs.writeFile(filePath, content, 'utf8');

}

async function getImageDimensions(url: string) {
  const response = await fetch(url);

  if (!response.ok)
    throw new Error(`HTTP ${response.status}`);


  const buffer = Buffer.from(await response.arrayBuffer());
  return imageSize(buffer);
}

function escapeHtml(str: string): string {
  return str
      .replace(/&/g, '&amp;')
      .replace(/"/g, '&quot;')
      .replace(/</g, '&lt;')
      .replace(/>/g, '&gt;');
}

main().catch(err => {
  console.error(err);
  process.exit(1);
});

Also needs: microsoft/playwright.dev#1973

Closes: microsoft/playwright.dev#1899

Copy link
Member

@Skn0tt Skn0tt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

neat! i like the solution.

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.

[Bug]: Layout shifts if page contains images

2 participants

Comments