Skip to content

Commit 8a449ad

Browse files
authored
feat: add root_path config option (#14)
Fixes #12
1 parent 9bb5ffe commit 8a449ad

File tree

6 files changed

+83
-19
lines changed

6 files changed

+83
-19
lines changed

.markdownlint-cli2.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ const config = {
44
config: {
55
extends: "markdownlint/style/prettier",
66
default: true,
7-
"relative-links": true,
7+
"relative-links": {
8+
root_path: ".",
9+
},
810
"no-inline-html": false,
911
},
1012
globs: ["**/*.md"],

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Thanks a lot for your interest in contributing to **markdownlint-rule-relative-l
44

55
## Code of Conduct
66

7-
**markdownlint-rule-relative-links** adopted the [Contributor Covenant](https://www.contributor-covenant.org/) as its Code of Conduct, and we expect project participants to adhere to it. Please read [the full text](./CODE_OF_CONDUCT.md) so that you can understand what actions will and will not be tolerated.
7+
**markdownlint-rule-relative-links** adopted the [Contributor Covenant](https://www.contributor-covenant.org/) as its Code of Conduct, and we expect project participants to adhere to it. Please read [the full text](/CODE_OF_CONDUCT.md) so that you can understand what actions will and will not be tolerated.
88

99
## Open Development
1010

README.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,13 @@ awesome.md:3 relative-links Relative links should be valid ["./invalid.txt" shou
5151
- Support images (e.g: `![Image](./image.png)`).
5252
- Support links fragments similar to the [built-in `markdownlint` rule - MD051](https://github.com/DavidAnson/markdownlint/blob/main/doc/md051.md) (e.g: `[Link](./awesome.md#heading)`).
5353
- Ignore external links and absolute paths as it only checks relative links (e.g: `https://example.com/` or `/absolute/path.png`).
54+
- If necessary, absolute paths can be validated too, with [`root_path` configuration option](#absolute-paths).
5455

5556
### Limitations
5657

5758
- Only images and links defined using markdown syntax are validated, html syntax is ignored (e.g: `<a href="./link.txt" />` or `<img src="./image.png" />`).
5859

59-
Contributions are welcome to improve the rule, and to alleviate these limitations. See [CONTRIBUTING.md](./CONTRIBUTING.md) for more information.
60+
Contributions are welcome to improve the rule, and to alleviate these limitations. See [CONTRIBUTING.md](/CONTRIBUTING.md) for more information.
6061

6162
### Related links
6263

@@ -108,6 +109,25 @@ export default config
108109
}
109110
```
110111

112+
### Absolute paths
113+
114+
GitHub (and, likely, other similar platforms) resolves absolute paths in Markdown links relative to the repository root.
115+
116+
To validate such links, add `root_path` option to the configuration:
117+
118+
```js
119+
config: {
120+
default: true,
121+
"relative-links": {
122+
root_path: ".",
123+
},
124+
},
125+
```
126+
127+
After this change, all absolute paths will be converted to relative paths, and will be resolved relative to the specified directory.
128+
129+
For example, if you run markdownlint from a subdirectory (if `package.json` is located in a subdirectory), you should set `root_path` to `".."`.
130+
111131
## Usage
112132

113133
```sh
@@ -118,8 +138,8 @@ node --run lint:markdown
118138

119139
Anyone can help to improve the project, submit a Feature Request, a bug report or even correct a simple spelling mistake.
120140

121-
The steps to contribute can be found in the [CONTRIBUTING.md](./CONTRIBUTING.md) file.
141+
The steps to contribute can be found in the [CONTRIBUTING.md](/CONTRIBUTING.md) file.
122142

123143
## 📄 License
124144

125-
[MIT](./LICENSE)
145+
[MIT](/LICENSE)

src/index.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,25 @@ const relativeLinksRule = {
5151
}
5252
}
5353

54-
if (hrefSrc == null) {
54+
if (hrefSrc == null || hrefSrc.startsWith("#")) {
5555
continue
5656
}
5757

58-
const url = new URL(hrefSrc, pathToFileURL(params.name))
59-
const isRelative =
60-
url.protocol === "file:" &&
61-
!hrefSrc.startsWith("/") &&
62-
!hrefSrc.startsWith("#")
58+
let url
6359

64-
if (!isRelative) {
60+
if (hrefSrc.startsWith("/")) {
61+
const rootPath = params.config["root_path"]
62+
63+
if (!rootPath) {
64+
continue
65+
}
66+
67+
url = new URL(`.${hrefSrc}`, pathToFileURL(`${rootPath}/`))
68+
} else {
69+
url = new URL(hrefSrc, pathToFileURL(params.name))
70+
}
71+
72+
if (url.protocol !== "file:") {
6573
continue
6674
}
6775

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Valid
2+
3+
![Absolute Path](/test/fixtures/image.png)

test/index.test.js

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,22 @@ import * as markdownlint from "markdownlint/promise"
55

66
import relativeLinksRule, { markdownIt } from "../src/index.js"
77

8+
const defaultConfig = {
9+
"relative-links": true,
10+
}
11+
812
/**
913
*
1014
* @param {string} fixtureFile
15+
* @param {Object} config
1116
* @returns
1217
*/
13-
const validateMarkdownLint = async (fixtureFile) => {
18+
const validateMarkdownLint = async (fixtureFile, config = defaultConfig) => {
1419
const lintResults = await markdownlint.lint({
1520
files: [fixtureFile],
1621
config: {
1722
default: false,
18-
"relative-links": true,
23+
...config,
1924
},
2025
customRules: [relativeLinksRule],
2126
markdownItFactory: () => {
@@ -146,11 +151,27 @@ test("ensure the rule validates correctly", async (t) => {
146151
fixturePath: "test/fixtures/invalid/non-existing-image.md",
147152
errors: ['"./image.png" should exist in the file system'],
148153
},
154+
{
155+
name: "should be invalid with incorrect absolute paths",
156+
fixturePath: "test/fixtures/config-dependent/absolute-paths.md",
157+
errors: ['"/test/fixtures/image.png" should exist in the file system'],
158+
config: {
159+
"relative-links": {
160+
root_path: "test",
161+
},
162+
},
163+
},
149164
]
150165

151-
for (const { name, fixturePath, errors } of testCases) {
166+
for (const {
167+
name,
168+
fixturePath,
169+
errors,
170+
config = defaultConfig,
171+
} of testCases) {
152172
await t.test(name, async () => {
153-
const lintResults = (await validateMarkdownLint(fixturePath)) ?? []
173+
const lintResults =
174+
(await validateMarkdownLint(fixturePath, config)) ?? []
154175
const errorsDetails = lintResults.map((result) => {
155176
assert.deepEqual(result.ruleNames, relativeLinksRule.names)
156177
assert.deepEqual(
@@ -219,7 +240,7 @@ test("ensure the rule validates correctly", async (t) => {
219240
fixturePath: "test/fixtures/valid/existing-image.md",
220241
},
221242
{
222-
name: "should ignore absolute paths",
243+
name: "should ignore absolute paths if root_path is not set",
223244
fixturePath: "test/fixtures/valid/ignore-absolute-paths.md",
224245
},
225246
{
@@ -231,11 +252,21 @@ test("ensure the rule validates correctly", async (t) => {
231252
fixturePath:
232253
"test/fixtures/valid/ignore-fragment-checking-in-own-file.md",
233254
},
255+
{
256+
name: "should be valid with correct absolute paths if root_path is set",
257+
fixturePath: "test/fixtures/config-dependent/absolute-paths.md",
258+
config: {
259+
"relative-links": {
260+
root_path: ".",
261+
},
262+
},
263+
},
234264
]
235265

236-
for (const { name, fixturePath } of testCases) {
266+
for (const { name, fixturePath, config = defaultConfig } of testCases) {
237267
await t.test(name, async () => {
238-
const lintResults = (await validateMarkdownLint(fixturePath)) ?? []
268+
const lintResults =
269+
(await validateMarkdownLint(fixturePath, config)) ?? []
239270
const errorsDetails = lintResults.map((result) => {
240271
return result.errorDetail
241272
})

0 commit comments

Comments
 (0)