From b826e051a1cd36433250d5a6c193e988237e93bc Mon Sep 17 00:00:00 2001 From: piquel Date: Sun, 22 Nov 2026 21:50:09 +0100 Subject: [PATCH] feat: docs-images gives every image in the docs alt text --- crates/sourceorigin-standard/README.md | 1 + .../src/rules/docs_images.rs | 57 +++++++++++++++++++ crates/sourceorigin-standard/src/rules/mod.rs | 4 ++ .../src/rules/tests/docs_images.rs | 19 +++++++ .../src/rules/tests/mod.rs | 1 + 5 files changed, 82 insertions(+) create mode 100644 crates/sourceorigin-standard/src/rules/docs_images.rs create mode 100644 crates/sourceorigin-standard/src/rules/tests/docs_images.rs diff --git a/crates/sourceorigin-standard/README.md b/crates/sourceorigin-standard/README.md index 8e4b99a..9765aa5 100644 --- a/crates/sourceorigin-standard/README.md +++ b/crates/sourceorigin-standard/README.md @@ -60,6 +60,7 @@ A repository adopts the standard in four steps: - `scheduled-audit`: A workflow runs `just audit` (or `cargo deny`) on a `schedule` at least weekly: a cron whose day of the month and month are `*`. - `pr-template`: `.github/pull_request_template.md` has the headings `## What and why` and `## Testing`. - `docs-summary`: With a `book.toml`, every Markdown page under its `src` is linked from its `SUMMARY.md`, or included in a page with `{{#include …}}`. +- `docs-images`: Every image in a Markdown file, outside code blocks, has alt text: `![what it shows](…)`, or an `` with a non-empty `alt`. Each rule is a variant of [`Rule`]: its name is the one `standard.toml` and diff --git a/crates/sourceorigin-standard/src/rules/docs_images.rs b/crates/sourceorigin-standard/src/rules/docs_images.rs new file mode 100644 index 0000000..1c6fbed --- /dev/null +++ b/crates/sourceorigin-standard/src/rules/docs_images.rs @@ -0,0 +1,57 @@ +//! `docs-images`: every image in the docs says what it shows. + +use super::text_file::TextFile; +use super::{Finding, Repository}; + +impl TextFile { + /// The lines, from 1, outside fenced code blocks, without their inline + /// code spans. + pub(super) fn prose_lines(&self) -> impl Iterator { + let mut fenced = false; + self.text + .lines() + .enumerate() + .filter_map(move |(index, line)| { + let fence = + line.trim_start().starts_with("```") || line.trim_start().starts_with("~~~"); + if fence { + fenced = !fenced; + } + let prose = line.split('`').step_by(2).collect(); + (!fence && !fenced).then_some((index + 1, prose)) + }) + } + + /// The line of each image without alt text: `![](…)`, or an `` + /// without a non-empty `alt`. + fn images_without_alt(&self) -> Vec { + self.prose_lines() + .filter(|(_, line)| { + line.contains("![](") + || line.match_indices("').next().unwrap_or_default(); + !tag.contains("alt=") || tag.contains("alt=\"\"") || tag.contains("alt=''") + }) + }) + .map(|(line, _)| line) + .collect() + } +} + +impl Repository { + /// Each image in a Markdown file without alt text. + pub(super) fn docs_images(&self) -> Vec { + self.markdown + .iter() + .flat_map(|file| { + file.images_without_alt().into_iter().map(|line| Finding { + path: file.path.clone(), + line, + message: "an image without alt text: say what it shows, as `![what it \ + shows](…)` or `alt=\"what it shows\"`" + .to_owned(), + }) + }) + .collect() + } +} diff --git a/crates/sourceorigin-standard/src/rules/mod.rs b/crates/sourceorigin-standard/src/rules/mod.rs index 4736a36..14d076a 100644 --- a/crates/sourceorigin-standard/src/rules/mod.rs +++ b/crates/sourceorigin-standard/src/rules/mod.rs @@ -16,6 +16,7 @@ mod clippy_config; mod comments; mod config_comments; mod deny_config; +mod docs_images; mod docs_summary; mod fix; mod generated_lints; @@ -253,6 +254,9 @@ rules! { /// With a `book.toml`, every Markdown page under its `src` is linked /// from its `SUMMARY.md`, or included in a page with `{{#include …}}`. DocsSummary = "docs-summary" => docs_summary, + /// Every image in a Markdown file, outside code blocks, has alt text: + /// `![what it shows](…)`, or an `` with a non-empty `alt`. + DocsImages = "docs-images" => docs_images, } /// Why the rules couldn't be checked. diff --git a/crates/sourceorigin-standard/src/rules/tests/docs_images.rs b/crates/sourceorigin-standard/src/rules/tests/docs_images.rs new file mode 100644 index 0000000..c50af4e --- /dev/null +++ b/crates/sourceorigin-standard/src/rules/tests/docs_images.rs @@ -0,0 +1,19 @@ +use super::{Fixture, INHERITS}; +use crate::rules::Rule; + +#[test] +fn every_image_says_what_it_shows() { + let fixture = Fixture::new(); + fixture.add_crate("core", INHERITS).write( + "docs/guide.md", + "# Guide\n\n![The dashboard, listing two apps](dashboard.png)\n![](blank.png)\n\ + \"The\n\n\nWrite `![](x.png)` with alt text.\n\n```md\n![](example.png)\n```\n", + ); + let blank = |line: usize| { + format!( + "docs/guide.md:{line}: an image without alt text: say what it shows, as `![what it \ + shows](…)` or `alt=\"what it shows\"` (docs-images)" + ) + }; + assert_eq!(fixture.violations(Rule::DocsImages), [blank(4), blank(6)]); +} diff --git a/crates/sourceorigin-standard/src/rules/tests/mod.rs b/crates/sourceorigin-standard/src/rules/tests/mod.rs index de8c0d4..cb81f6d 100644 --- a/crates/sourceorigin-standard/src/rules/tests/mod.rs +++ b/crates/sourceorigin-standard/src/rules/tests/mod.rs @@ -12,6 +12,7 @@ mod clippy_config; mod comments; mod config_comments; mod deny_config; +mod docs_images; mod docs_summary; mod fix; mod framework;