Skip to content

Commit bc52f5e

Browse files
authored
Merge pull request #3384 from Blargian/custom_rule_images
add custom rule for disallowing markdown image syntax
2 parents 60244d0 + b17e22a commit bc52f5e

File tree

8 files changed

+102
-35
lines changed

8 files changed

+102
-35
lines changed

docusaurus.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ const config = {
9898
editCurrentVersion: true,
9999
breadcrumbs: true,
100100
editUrl: ({ docPath }) => {
101-
if (docPath === 'index.md') return false
102-
101+
if (docPath === 'index.md')
102+
return false
103103
if (
104104
docPath.includes('development') ||
105105
docPath.includes('engines') ||

scripts/.markdownlint-cli2.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ config:
88
MD040: false # Fenced code blocks should have a language specified
99
links-url-type: false # Disallow relative links to a .md or .mdx file
1010
custom-anchor-headings: true # Headings must have a custom anchor which is unique per page eg. # A Heading {#a-heading}
11-
11+
no-markdown-image-tags: false
1212
# Keep this item last due to length
1313
proper-names: # MD044
1414
code_blocks: false
@@ -27,7 +27,7 @@ ignores:
2727
- "docs/cloud/manage/api"
2828
customRules:
2929
# add custom rules here
30-
- "./markdownlint/rules/links_url_type.js"
31-
- "./markdownlint/rules/headings_have_custom_anchors.js"
30+
- "./markdownlint/custom_rules/links_url_type.js"
31+
- "./markdownlint/custom_rules/headings_have_custom_anchors.js"
3232

3333

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Custom Rules
2+
3+
Add custom rules to this directory and call them in `scripts/.markdownlint-cli2.yaml` here:
4+
5+
```yaml
6+
customRules:
7+
# add custom rules here
8+
```
9+
10+
## links_url_type
11+
12+
This custom rule is used to check that we have consistent types of linking.
13+
We want to use URL type links (by slug) as relative links don't work with
14+
translations.
15+
16+
Examples of valid links:
17+
18+
```markdown
19+
[valid link](/docs/interfaces/formats)
20+
```
21+
22+
Examples of invalid links:
23+
24+
```markdown
25+
[invalid link](../../docs/interfaces/formats.md)
26+
```
27+
28+
```markdown
29+
[invalid link](../../docs/interfaces)
30+
```
31+
32+
## headings_have_custom_anchors
33+
34+
This custom rule is used to check that every heading has a custom anchor tag.
35+
This is necessary for our LLM translation system to ensure that linking works.
36+
37+
Examples of valid headings:
38+
39+
```markdown
40+
## An H2 heading {#an-h2-heading}
41+
42+
### An H3 heading {#an-h3-heading}
43+
```
44+
45+
Examples of invalid headings:
46+
47+
```markdown
48+
## An H2 heading
49+
50+
### An H3 heading
51+
```
52+
53+
## no_markdown_image_tags
54+
55+
Our documentation uses an opinionated guideline on images. We disallow pure
56+
markdown image declarations using the `![]()` syntax and prefer to use a
57+
specific image component instead. This rule checks that markdown images are not
58+
used.
59+
60+
Example of a valid usage:
61+
62+
```markdown
63+
import image from '@site/static/images/image.png'
64+
65+
<img src={image} alt="Description"/>
66+
```
67+
68+
Example of an invalid usage:
69+
70+
```markdown
71+
![An image](/path/to/an/image.png)
72+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const { forEachLine, getLineMetadata, isBlankLine } = require(`markdownlint-rule-helpers`);
2+
const {filterTokens} = require('./utility_functions.js');
3+
4+
// A custom rule to disallow markdown image tags like ![text](url)
5+
module.exports = {
6+
names: ['no-markdown-image-tags'],
7+
tags: ["images"],
8+
description: 'Disallows markdown image tags. Prefer img tag instead',
9+
function: (params, onError) => {
10+
filterTokens(params, "inline", (token) => {
11+
const children = token.children ?? [];
12+
for (const child of children) {
13+
const { type, lineNumber, content } = child;
14+
15+
if (type === "image") {
16+
onError({
17+
lineNumber,
18+
detail: `Markdown image tag found: ![${content}](${child.attrs.find(attr => attr[0] === 'src')[1]})`,
19+
});
20+
}
21+
}
22+
});
23+
},
24+
};
25+

scripts/markdownlint/rules/README.md

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)