Skip to content

Commit

Permalink
Change to improve messages
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Apr 10, 2024
1 parent 1625a97 commit 51ecab8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 35 deletions.
42 changes: 26 additions & 16 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ import isOnline from 'is-online'
import {lintRule} from 'unified-lint-rule'
import {visit} from 'unist-util-visit'

const remarkLintNoDeadUrls = lintRule('remark-lint:no-dead-urls', rule)

const remarkLintNoDeadUrls = lintRule(
{
origin: 'remark-lint:no-dead-urls',
url: 'https://github.com/remarkjs/remark-lint-no-dead-urls'
},
rule
)
const defaultSkipUrlPatterns = [/^(?!https?)/i]

export default remarkLintNoDeadUrls
Expand Down Expand Up @@ -53,11 +58,12 @@ async function rule(tree, file, options) {
return
}

/* c8 ignore next 8 -- difficult to test */
/* c8 ignore next 9 -- difficult to test */
if (!online) {
if (!settings.skipOffline) {
// To do: clean message.
file.message('You are not online and have not set skipOffline: true.')
file.info(
'Unexpected offline connection, expected either an online connection or `skipOffline: true`'
)
}

return
Expand Down Expand Up @@ -139,20 +145,24 @@ async function rule(tree, file, options) {

for (const node of nodes) {
for (const message of result.messages) {
// To do: enclose url in backticks.
const copy = file.message('Link to ' + url + ' is dead', {
cause: message,
place: node.position
})

copy.fatal = message.fatal
const product = file.message(
'Unexpected dead URL `' + url + '`, expected live URL',
{ancestors: [node], cause: message, place: node.position}
)
product.fatal = message.fatal
}

if (result.status === 'alive' && new URL(url).href !== result.url) {
// To do: clean message.
file.message('Link to ' + url + ' redirects to ' + result.url, {
place: node.position
})
const message = file.message(
'Unexpected redirecting URL `' +
url +
'`, expected final URL `' +
result.url +
'`',
{ancestors: [node], place: node.position}
)
message.actual = url
message.expected = [result.url]
}
}
})
Expand Down
42 changes: 23 additions & 19 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ Here is another [bad link](https://does-not-exists.com).
assert.deepEqual(
file.messages.map((d) => d.reason),
[
'Link to https://exists.com/does/not/ is dead',
'Link to https://does-not-exists.com/ is dead'
'Unexpected dead URL `https://exists.com/does/not/`, expected live URL',
'Unexpected dead URL `https://does-not-exists.com/`, expected live URL'
]
)
})
Expand Down Expand Up @@ -94,10 +94,10 @@ test('checks full URLs normally', async () => {
assert.deepEqual(
file.messages.map((d) => d.reason),
[
'Link to http://a.com/ is dead',
'Link to https://b.com/ is dead',
'Link to c:\\Documents\\c.md is dead',
'Link to file:///Users/tilde/d.js is dead'
'Unexpected dead URL `http://a.com/`, expected live URL',
'Unexpected dead URL `https://b.com/`, expected live URL',
'Unexpected dead URL `c:\\Documents\\c.md`, expected live URL',
'Unexpected dead URL `file:///Users/tilde/d.js`, expected live URL'
]
)
})
Expand Down Expand Up @@ -128,13 +128,13 @@ test('checks relative URLs w/ `from`', async () => {
assert.deepEqual(
file.messages.map((d) => d.reason),
[
'Link to https://example.com/from/a.md is dead',
'Link to https://example.com/b.md is dead',
'Link to https://example.com/from/c.md is dead',
'Link to https://example.com/d.md is dead',
'Link to https://example.com/from/folder#e is dead',
'Link to https://example.com/from/folder?f is dead',
'Link to https://g.com/ is dead'
'Unexpected dead URL `https://example.com/from/a.md`, expected live URL',
'Unexpected dead URL `https://example.com/b.md`, expected live URL',
'Unexpected dead URL `https://example.com/from/c.md`, expected live URL',
'Unexpected dead URL `https://example.com/d.md`, expected live URL',
'Unexpected dead URL `https://example.com/from/folder#e`, expected live URL',
'Unexpected dead URL `https://example.com/from/folder?f`, expected live URL',
'Unexpected dead URL `https://g.com/`, expected live URL'
]
)
})
Expand All @@ -159,7 +159,7 @@ test('checks relative URLs w/ `meta.origin`, `meta.pathname`', async () => {

assert.deepEqual(
file.messages.map((d) => d.reason),
['Link to https://example.com/from/a.md is dead']
['Unexpected dead URL `https://example.com/from/a.md`, expected live URL']
)
})

Expand All @@ -185,9 +185,9 @@ test('works with definitions and images', async () => {
assert.deepEqual(
file.messages.map((d) => d.reason),
[
'Link to https://example.com/a is dead',
'Link to https://example.com/b is dead',
'Link to https://example.com/c is dead'
'Unexpected dead URL `https://example.com/a`, expected live URL',
'Unexpected dead URL `https://example.com/b`, expected live URL',
'Unexpected dead URL `https://example.com/c`, expected live URL'
]
)
})
Expand Down Expand Up @@ -293,7 +293,9 @@ test('should support anchors', async () => {

assert.deepEqual(
file.messages.map((d) => d.reason),
['Link to https://example.com/#does-not-exist is dead']
[
'Unexpected dead URL `https://example.com/#does-not-exist`, expected live URL'
]
)
})

Expand Down Expand Up @@ -323,6 +325,8 @@ test('should support redirects', async () => {

assert.deepEqual(
file.messages.map((d) => d.reason),
['Link to https://example.com/from redirects to https://example.com/to']
[
'Unexpected redirecting URL `https://example.com/from`, expected final URL `https://example.com/to`'
]
)
})

0 comments on commit 51ecab8

Please sign in to comment.