@@ -2,8 +2,6 @@ import path from 'path'
2
2
import fs from 'fs'
3
3
import { globby } from 'globby'
4
4
5
- const URL_BASE = 'https://github.com/TheAlgorithms/Javascript/blob/master'
6
-
7
5
function pathPrefix ( i ) {
8
6
const res = ' ' . repeat ( i )
9
7
return res + '*'
@@ -12,6 +10,7 @@ function pathPrefix (i) {
12
10
function printPath ( oldPath , newPath , output ) {
13
11
const oldParts = oldPath . split ( path . sep )
14
12
const newParts = newPath . split ( path . sep )
13
+
15
14
for ( let i = 0 ; i < newParts . length ; ++ i ) {
16
15
const newPart = newParts [ i ]
17
16
if ( i + 1 > oldParts . length || oldParts [ i ] !== newPart ) {
@@ -20,6 +19,7 @@ function printPath (oldPath, newPath, output) {
20
19
}
21
20
}
22
21
}
22
+
23
23
return newPath
24
24
}
25
25
@@ -32,38 +32,23 @@ function pathsToMarkdown (filePaths) {
32
32
if ( a . toLowerCase ( ) > b . toLowerCase ( ) ) return 1
33
33
return 0
34
34
} )
35
+
35
36
for ( let filepath of filePaths ) {
36
- const file = filepath . split ( path . sep )
37
- let filename = ''
38
- if ( file . length === 1 ) {
39
- filepath = ''
40
- filename = file [ 0 ]
41
- } else {
42
- const total = file . length
43
- filename = file [ total - 1 ]
44
- filepath = file . splice ( 0 , total - 1 ) . join ( path . sep )
45
- }
37
+ let filename = path . basename ( filepath )
38
+ filepath = path . dirname ( path . sep )
39
+
46
40
if ( filepath !== oldPath ) {
47
41
oldPath = printPath ( oldPath , filepath , output )
48
42
}
49
- let indent = 0
50
- for ( let i = 0 ; i < filepath . length ; ++ i ) {
51
- if ( filepath [ i ] === path . sep ) {
52
- ++ indent
53
- }
54
- }
55
- if ( filepath ) {
56
- ++ indent
57
- }
43
+
44
+ let indent = filepath . replace ( new RegExp ( `/[^${ path . sep } ]/` ) ) . length + 1
58
45
59
46
// prepare the markdown-esque prefix to the file's line
60
47
const prefix = pathPrefix ( indent )
61
48
62
49
// remove extension from filename
63
- const name = filename . split ( '.' ) [ 0 ]
64
-
65
- // create URL to the actual file on github
66
- const url = encodeURI ( [ URL_BASE , filepath , filename ] . join ( '/' ) )
50
+ const name = path . basename ( filename , ".js" )
51
+ const url = path . join ( filepath , filename )
67
52
68
53
output . push ( `${ prefix } [${ name } ](${ url } )` )
69
54
}
0 commit comments