Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions bin/lint-markdown-links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ async function fetchExternalLink(link: string, checkRedirects = false) {
}

interface Options {
allowAbsoluteLinks?: boolean;
fetchExternalLinks?: boolean;
checkRedirects?: boolean;
ignoreGlobs?: string[];
Expand All @@ -75,7 +76,12 @@ interface Options {
async function main(
workspaceRoot: string,
globs: string[],
{ fetchExternalLinks = false, checkRedirects = false, ignoreGlobs = [] }: Options,
{
allowAbsoluteLinks = false,
fetchExternalLinks = false,
checkRedirects = false,
ignoreGlobs = [],
}: Options,
) {
const workspace = new DocsWorkspace(workspaceRoot, globs, ignoreGlobs);
const parser = new MarkdownParser();
Expand All @@ -95,11 +101,17 @@ async function main(
try {
// Collect diagnostics for all documents in the workspace
for (const document of await workspace.getAllMarkdownDocuments()) {
const absoluteLinks = new Set<any>();

for (let link of await languageService.getDocumentLinks(document, cts.token)) {
if (link.target === undefined) {
link = (await languageService.resolveDocumentLink(link, cts.token)) ?? link;
}

if (!allowAbsoluteLinks && link.data && link.data.source.hrefText.startsWith('/')) {
absoluteLinks.add(link);
}

if (
link.target &&
link.target.startsWith('http') &&
Expand All @@ -114,7 +126,7 @@ async function main(
cts.token,
);

if (diagnostics.length) {
if (diagnostics.length || absoluteLinks.size) {
console.log(
'File Location:',
path.relative(URI.file(workspace.root).path, URI.parse(document.uri).path),
Expand All @@ -128,6 +140,14 @@ async function main(
);
errors = true;
}

for (const link of absoluteLinks) {
console.log(
`\tAbsolute link on line ${link.range.start.line + 1}:`,
link.data.source.hrefText,
);
errors = true;
}
}
} finally {
cts.dispose();
Expand All @@ -147,8 +167,8 @@ async function main(
function parseCommandLine() {
const showUsage = (): never => {
console.log(
'Usage: lint-roller-markdown-links [--root <dir>] <globs> [-h|--help] [--fetch-external-links] ' +
'[--check-redirects] [--ignore <globs>]',
'Usage: lint-roller-markdown-links [--root <dir>] <globs> [-h|--help] [--allow-absolute-links]' +
'[--fetch-external-links] [--check-redirects] [--ignore <globs>]',
);
process.exit(1);
};
Expand All @@ -157,6 +177,9 @@ function parseCommandLine() {
const opts = parseArgs({
allowPositionals: true,
options: {
'allow-absolute-links': {
type: 'boolean',
},
'fetch-external-links': {
type: 'boolean',
},
Expand Down Expand Up @@ -209,6 +232,7 @@ if (process.argv[1] === fileURLToPath(import.meta.url)) {
}

main(path.resolve(process.cwd(), opts.root), positionals, {
allowAbsoluteLinks: opts['allow-absolute-links'],
fetchExternalLinks: opts['fetch-external-links'],
checkRedirects: opts['check-redirects'],
ignoreGlobs: opts.ignore,
Expand Down
9 changes: 9 additions & 0 deletions tests/fixtures/absolute-internal-link.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Test

## Target section

This is the target section

## Other Section

Link to [target section](/broken-internal-link.md)
1 change: 1 addition & 0 deletions tests/fixtures/ignorepaths
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
**/absolute-internal-link.md
**/broken-{external,internal}-link.md
**/broken-cross-file-link.md
**/valid-cross-file-link.md
Expand Down
26 changes: 25 additions & 1 deletion tests/lint-roller-markdown-links.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('lint-roller-markdown-links', () => {
'--root',
FIXTURES_DIR,
'--ignore',
'**/{{broken,valid}-*-link.md,*angle-brackets.md,api-history-*.md}',
'**/{{absolute,broken,valid}-*-link.md,*angle-brackets.md,api-history-*.md}',
'*.md',
);

Expand All @@ -42,6 +42,8 @@ describe('lint-roller-markdown-links', () => {
'--root',
FIXTURES_DIR,
'--ignore',
'**/absolute-internal-link.md',
'--ignore',
'**/broken-{external,internal}-link.md',
'--ignore',
'**/{broken,valid}-cross-file-link.md',
Expand Down Expand Up @@ -153,4 +155,26 @@ describe('lint-roller-markdown-links', () => {

expect(status).toEqual(0);
});

it('should disallow absolute links by default', () => {
const { status, stdout } = runLintMarkdownLinks(
'--root',
FIXTURES_DIR,
'absolute-internal-link.md',
);

expect(stdout).toContain('Absolute link');
expect(status).toEqual(1);
});

it('should allow absolute links by with --allow-absolute-links', () => {
const { status } = runLintMarkdownLinks(
'--root',
FIXTURES_DIR,
'absolute-internal-link.md',
'--allow-absolute-links',
);

expect(status).toEqual(0);
});
});