Skip to content

Commit fd23998

Browse files
authored
feat: use Node.js global fetch if available (#42)
1 parent de612a7 commit fd23998

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

bin/lint-markdown-links.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
LogLevel,
1212
} from '@dsanders11/vscode-markdown-languageservice';
1313
import * as minimist from 'minimist';
14-
import fetch from 'node-fetch';
1514
import { CancellationTokenSource } from 'vscode-languageserver';
1615
import { URI } from 'vscode-uri';
1716

@@ -34,6 +33,12 @@ const diagnosticOptions: DiagnosticOptions = {
3433
};
3534

3635
async function fetchExternalLink(link: string, checkRedirects = false) {
36+
// Use global fetch if available, otherwise fall back to node-fetch
37+
if (!('fetch' in global)) {
38+
const { default: fetch } = await import('node-fetch');
39+
(global as any).fetch = fetch;
40+
}
41+
3742
try {
3843
const response = await fetch(link);
3944
if (response.status !== 200) {

tests/electron-lint-markdown-links.spec.ts

+15
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ function runLintMarkdownLinks(...args: string[]) {
1111
);
1212
}
1313

14+
const nodeVersion = parseInt(process.version.match(/^v(\d+)\./)![1]);
15+
1416
describe('electron-lint-markdown-links', () => {
1517
it('should catch broken internal links', () => {
1618
const { status, stdout } = runLintMarkdownLinks(
@@ -125,4 +127,17 @@ describe('electron-lint-markdown-links', () => {
125127
expect(stdout.toString('utf-8')).toContain('Broken link');
126128
expect(status).toEqual(1);
127129
});
130+
131+
if (nodeVersion >= 18) {
132+
it('should be able to fetch GitHub label URLs', () => {
133+
const { status } = runLintMarkdownLinks(
134+
'--root',
135+
FIXTURES_DIR,
136+
'github-label-link.md',
137+
'--fetch-external-links',
138+
);
139+
140+
expect(status).toEqual(0);
141+
});
142+
}
128143
});

tests/fixtures/github-label-link.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is an [external link](https://github.com/electron/fiddle/labels/ESM)

0 commit comments

Comments
 (0)