vuejs / vitepress

Vite & Vue powered static site generator.
https://vitepress.dev
MIT License
12.78k stars 2.07k forks source link

Enabling External Link Validity Checking #4009

Open slusarz opened 3 months ago

slusarz commented 3 months ago

Is your feature request related to a problem? Please describe.

During build (or CI) process, external links should be checked for validity.

Currently, VP only checks localhost links. Other external links are just added as-is to the page. See https://github.com/vuejs/vitepress/blob/d837e82bc8bde63df737be2a1290a2e70c8a0bf3/src/node/markdown/plugins/link.ts#L39

Describe the solution you'd like

At a minimum, ignoreDeadLinks callback option should be extended to return ALL links seen during parsing. VP admin can then write their own logic to do link checking.

Example:

ignoreDeadLinks: [
    (url) => {
        if (external_URL) {
            // Do link checking, i.e. with 'link-check' npm package
        } elseif (localhost_URL) {
            // Handle localhost checking.
        }
    }
],

Even better would be a way to continue to allow VP to handle localhost link checking and provide an option to handle external links separately.

Even even better might be that external link checking is simply built-in to VP, as an option. (E.g., Sphinx has this ability.). Simple example:

import 'linkCheck' from 'link-check'

// ...

linkCheck(url_to_check, function (err, result) {
    if (err) {
        throw new Error('Invalid Link: ' + err)
    }
});

Describe alternatives you've considered

No response

Additional context

More than willing to provide an MR to accomplish this, but since there are so many possibilities on how to allow/enable this I would rather wait for developer input to direct towards the best solution.

Validations

brc-dd commented 3 months ago

Might be easy to just run lychee or one of the tools mentioned here - https://github.com/lycheeverse/lychee?tab=readme-ov-file#features -- most of these are CLIs and integrating directly in vitepress won't make much sense and will slow you down.

Also, ignoreDeadLinks can't receive the external links because many of these links cannot be extracted by the markdown parser - for example links added via components, html tags, frontmatter, themeConfig, etc.

slusarz commented 3 months ago

Appreciate the quick response and the pointer to the lychee project (that looks fantastic for our use-case...)

Understood now that the existing ignoreDeadLinks is not built for this ask.

More than willing to do a small MR for documentation that would adapt your explanation, if you think that would be helpful (I have to think we are not the only ones who are looking for this functionality).