Skip to content

Commit

Permalink
[FEATURE] Handle images without alt tags
Browse files Browse the repository at this point in the history
Issue a warning for accessibility reasons but let them output
  • Loading branch information
linawolf committed Nov 1, 2023
1 parent f6ee734 commit fa9eb9b
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public function parse(MarkupLanguageParser $parser, NodeWalker $walker, CommonMa
{
$content = [];

if ($current->firstChild() === null) {
// Handle inline nodes without content
return $this->createInlineNode($current, null);
}

while ($event = $walker->next()) {
$commonMarkNode = $event->getNode();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
use Psr\Log\LoggerInterface;

use function assert;
use function sprintf;

/** @extends AbstractInlineTextDecoratorParser<ImageInlineNode> */
final class InlineImageParser extends AbstractInlineTextDecoratorParser
{
/** @param iterable<AbstractInlineParser<InlineNode>> $inlineParsers */
public function __construct(
iterable $inlineParsers,
LoggerInterface $logger,
private readonly LoggerInterface $logger,
) {
parent::__construct($inlineParsers, $logger);
}
Expand All @@ -32,6 +33,16 @@ protected function createInlineNode(CommonMarkNode $commonMarkNode, string|null
{
assert($commonMarkNode instanceof Image);

if ($content === null) {
$this->logger->warning(
sprintf(
'Image %s does not have an alternative text. Add an alternative text like this: ![Image description](%s)',
$commonMarkNode->getUrl(),
$commonMarkNode->getUrl(),
),
);
}

return new ImageInlineNode($commonMarkNode->getUrl(), $content ?? '');
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Markdown with links - Project Title</title>
<title>Images with and without alt text - Project Title</title>

</head>
<body>
<h1>Markdown with links</h1>
<h1>Images with and without alt text</h1>

<p>This is a Markdown document with some basic formatting.</p>
<p>See also: <a href="https://www.example.com">Link Text</a></p>
<p><img src="https://example.org/example.png" alt="with alt Text"/></p>
<p><img src="https://example.org/example2.png" alt=""/></p>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
app.WARNING: Image https://example.org/example2.png does not have an alternative text. Add an alternative text like this: ![Image description](https://example.org/example2.png)
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# additional_reports
# Images with and without alt text

![](https://raw.githubusercontent.com/Apen/additional_reports/master/Resources/Public/Images/middlewares.png)
![with alt Text](https://example.org/example.png)

![](https://example.org/example2.png)

This file was deleted.

0 comments on commit fa9eb9b

Please sign in to comment.