Skip to content

Commit 23e4ff0

Browse files
feat: create no-empty-images (#357)
Co-authored-by: Nitin Kumar <snitin315@gmail.com>
1 parent f00d2af commit 23e4ff0

4 files changed

Lines changed: 176 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export default defineConfig([
7676
| [`fenced-code-language`](./docs/rules/fenced-code-language.md) | Require languages for fenced code blocks | yes |
7777
| [`heading-increment`](./docs/rules/heading-increment.md) | Enforce heading levels increment by one | yes |
7878
| [`no-duplicate-headings`](./docs/rules/no-duplicate-headings.md) | Disallow duplicate headings in the same document | no |
79+
| [`no-empty-images`](./docs/rules/no-empty-images.md) | Disallow empty images | yes |
7980
| [`no-empty-links`](./docs/rules/no-empty-links.md) | Disallow empty links | yes |
8081
| [`no-html`](./docs/rules/no-html.md) | Disallow HTML tags | no |
8182
| [`no-invalid-label-refs`](./docs/rules/no-invalid-label-refs.md) | Disallow invalid label references | yes |

docs/rules/no-empty-images.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# no-empty-images
2+
3+
Disallow empty images.
4+
5+
## Background
6+
7+
In Markdown, it’s not always easy to spot when you’ve forgotten to provide a destination for an image. This is especially common when you’re writing something in Markdown and intend to insert an image, leaving the destination to fill in later, only to forget to go back and complete it. This often results in broken image links.
8+
9+
## Rule Details
10+
11+
This rule warns when it finds images that either don't have a URL specified or have only an empty fragment (`"#"`).
12+
13+
Examples of incorrect code:
14+
15+
```markdown
16+
![]()
17+
18+
![ESLint Logo]()
19+
20+
![](#)
21+
22+
![Image](#)
23+
```
24+
25+
Exmaples of correct code:
26+
27+
```markdown
28+
![](https://eslint.org/image.png)
29+
30+
![ESLint Logo](https://eslint.org/image.png)
31+
```
32+
33+
## When Not to Use It
34+
35+
If you aren't concerned with empty images, you can safely disable this rule.
36+
37+
## Prior Art
38+
39+
* [remark-lint-no-empty-url](https://github.com/remarkjs/remark-lint/tree/main/packages/remark-lint-no-empty-url)

src/rules/no-empty-images.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @fileoverview Rule to prevent empty images in Markdown.
3+
* @author 루밀LuMir(lumirlumir)
4+
*/
5+
6+
//-----------------------------------------------------------------------------
7+
// Type Definitions
8+
//-----------------------------------------------------------------------------
9+
10+
/**
11+
* @typedef {import("../types.ts").MarkdownRuleDefinition<{ RuleOptions: []; }>}
12+
* NoEmptyImagesRuleDefinition
13+
*/
14+
15+
//-----------------------------------------------------------------------------
16+
// Rule Definition
17+
//-----------------------------------------------------------------------------
18+
19+
/** @type {NoEmptyImagesRuleDefinition} */
20+
export default {
21+
meta: {
22+
type: "problem",
23+
24+
docs: {
25+
recommended: true,
26+
description: "Disallow empty images",
27+
url: "https://github.com/eslint/markdown/blob/main/docs/rules/no-empty-images.md",
28+
},
29+
30+
messages: {
31+
emptyImage: "Unexpected empty image found.",
32+
},
33+
},
34+
35+
create(context) {
36+
return {
37+
image(node) {
38+
if (!node.url || node.url === "#") {
39+
context.report({
40+
loc: node.position,
41+
messageId: "emptyImage",
42+
});
43+
}
44+
},
45+
};
46+
},
47+
};
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* @fileoverview Tests for no-empty-images rule.
3+
* @author 루밀LuMir(lumirlumir)
4+
*/
5+
6+
//------------------------------------------------------------------------------
7+
// Imports
8+
//------------------------------------------------------------------------------
9+
10+
import rule from "../../src/rules/no-empty-images.js";
11+
import markdown from "../../src/index.js";
12+
import { RuleTester } from "eslint";
13+
14+
//------------------------------------------------------------------------------
15+
// Tests
16+
//------------------------------------------------------------------------------
17+
18+
const ruleTester = new RuleTester({
19+
plugins: {
20+
markdown,
21+
},
22+
language: "markdown/commonmark",
23+
});
24+
25+
ruleTester.run("no-empty-images", rule, {
26+
valid: ["![foo](bar)", "![foo](#bar)", "![foo](http://bar.com/image.png)"],
27+
invalid: [
28+
{
29+
code: "![]()",
30+
errors: [
31+
{
32+
messageId: "emptyImage",
33+
line: 1,
34+
column: 1,
35+
endLine: 1,
36+
endColumn: 6,
37+
},
38+
],
39+
},
40+
{
41+
code: "![](#)",
42+
errors: [
43+
{
44+
messageId: "emptyImage",
45+
line: 1,
46+
column: 1,
47+
endLine: 1,
48+
endColumn: 7,
49+
},
50+
],
51+
},
52+
{
53+
code: "![foo]()",
54+
errors: [
55+
{
56+
messageId: "emptyImage",
57+
line: 1,
58+
column: 1,
59+
endLine: 1,
60+
endColumn: 9,
61+
},
62+
],
63+
},
64+
{
65+
code: "![foo](#)",
66+
errors: [
67+
{
68+
messageId: "emptyImage",
69+
line: 1,
70+
column: 1,
71+
endLine: 1,
72+
endColumn: 10,
73+
},
74+
],
75+
},
76+
{
77+
code: "![foo]( )",
78+
errors: [
79+
{
80+
messageId: "emptyImage",
81+
line: 1,
82+
column: 1,
83+
endLine: 1,
84+
endColumn: 10,
85+
},
86+
],
87+
},
88+
],
89+
});

0 commit comments

Comments
 (0)