1
1
import { checkLink } from "./checkLink" ;
2
2
import fs from 'fs' ;
3
3
4
+ const urlRegex : RegExp = / (?< ! x m l n s = [ ' " ] ) (?< ! x m l n s : .* = [ ' " ] ) (?< ! t a r g e t N a m e s p a c e = [ ' " ] ) ( \b h t t p s ? : \/ \/ (? ! .* \$ ) (? ! .* { ) (? ! .* " \s \+ ) [ - A - Z 0 - 9 + & @ # \/ % ? = ~ _ | ! : , . ; ] * [ - A - Z 0 - 9 + & @ # \/ % = ~ _ | ] ) / ig;
5
+
4
6
let QUEUE :Record < number , string [ ] > = { } ;
5
7
let PROCESS : number = 0 ;
6
8
let CURRENTPROCESS : number = 0 ;
@@ -11,14 +13,13 @@ async function runProcess(callback: any) {
11
13
if ( MAXPROCESSES <= RUNNINGPROCESSES || ! QUEUE [ PROCESS - 1 ] ) return ;
12
14
13
15
RUNNINGPROCESSES ++ ;
14
- const [ link , path ] = QUEUE [ CURRENTPROCESS ] ! ;
16
+ const [ link , path , lineNumber ] = QUEUE [ CURRENTPROCESS ] ! ;
15
17
delete QUEUE [ CURRENTPROCESS ] ;
16
18
CURRENTPROCESS ++ ;
17
19
18
20
try {
19
- if ( await checkLink ( link ! ) ) {
20
- callback ( link , path ) ;
21
- }
21
+ if ( await checkLink ( link ! ) )
22
+ callback ( link , path , lineNumber ) ;
22
23
} catch { } // Skip invalid urls
23
24
24
25
RUNNINGPROCESSES -- ;
@@ -38,10 +39,12 @@ export default async function linkInspector(arg: string, callback: any, path: st
38
39
const stats : fs . Stats = fs . lstatSync ( arg ) ;
39
40
40
41
// Skip symbolic links
41
- if ( stats . isSymbolicLink ( ) ) return ;
42
+ if ( stats . isSymbolicLink ( ) )
43
+ return ;
42
44
43
45
// Skip files over 100mb
44
- if ( 100 * 1024 * 1024 < stats . size ) return
46
+ if ( 100 * 1024 * 1024 < stats . size )
47
+ return
45
48
46
49
// Handle directory
47
50
if ( stats . isDirectory ( ) ) {
@@ -56,22 +59,27 @@ export default async function linkInspector(arg: string, callback: any, path: st
56
59
const content : string = fs . readFileSync ( arg , 'utf8' ) ;
57
60
58
61
// Skip binary files
59
- if ( ! / ^ [ \x00 - \x7F ] * $ / . test ( content ) ) return ;
62
+ if ( ! / ^ [ \x00 - \x7F ] * $ / . test ( content ) )
63
+ return ;
60
64
61
65
// Get all the links
62
- const urlRegex : RegExp = / (?< ! x m l n s = [ ' " ] ) (?< ! x m l n s : .* = [ ' " ] ) (?< ! t a r g e t N a m e s p a c e = [ ' " ] ) ( \b h t t p s ? : \/ \/ (? ! .* \$ ) (? ! .* { ) (? ! .* " \s \+ ) [ - A - Z 0 - 9 + & @ # \/ % ? = ~ _ | ! : , . ; ] * [ - A - Z 0 - 9 + & @ # \/ % = ~ _ | ] ) / ig;
63
- const links : string [ ] = content . match ( urlRegex ) || [ ] ;
66
+ const lines = content . split ( '\n' ) ;
67
+
68
+ for ( let i = 0 ; i < lines . length ; i ++ ) {
69
+ const line = lines [ i ] ;
70
+ const matches = line ! . match ( urlRegex ) || [ ] ;
64
71
65
- const directoryIndex : number = arg . indexOf ( path ) ;
66
- const pathAfterDirectory : string = arg . substring ( directoryIndex ) ;
72
+ const directoryIndex : number = arg . indexOf ( path ) ;
73
+ const pathAfterDirectory : string = arg . substring ( directoryIndex ) ;
67
74
68
- for ( const link of links ) {
69
- try { // Runs link inspector on each link
70
- new URL ( link ) ;
71
- QUEUE [ PROCESS ] = [ link , pathAfterDirectory ] ;
72
- PROCESS ++ ;
73
- runProcess ( callback ) ;
74
- } catch { }
75
+ for ( const link of matches ) {
76
+ try { // Runs link inspector on each link
77
+ new URL ( link ) ;
78
+ QUEUE [ PROCESS ] = [ link , pathAfterDirectory , ( i + 1 ) . toString ( ) ] ;
79
+ PROCESS ++ ;
80
+ runProcess ( callback ) ;
81
+ } catch { }
82
+ }
75
83
}
76
84
} catch {
77
85
console . error ( `Error: ${ arg } is not a valid link or path` ) ;
0 commit comments