@@ -10,59 +10,67 @@ function alfredMatcher(str) {
10
10
return [ camelCaseSeparated , str ] . join ( " " ) + " " ;
11
11
}
12
12
13
+ /** @param {string } url @return {string } */
14
+ function httpRequest ( url ) {
15
+ const queryURL = $ . NSURL . URLWithString ( url ) ;
16
+ const data = $ . NSData . dataWithContentsOfURL ( queryURL ) ;
17
+ return $ . NSString . alloc . initWithDataEncoding ( data , $ . NSUTF8StringEncoding ) . js ;
18
+ }
19
+
13
20
//──────────────────────────────────────────────────────────────────────────────
14
21
15
22
/** @type {AlfredRun } */
16
23
// biome-ignore lint/correctness/noUnusedVariables: Alfred run
17
24
function run ( ) {
18
25
const docsUrl = "https://api.github.com/repos/biomejs/website/git/trees/main?recursive=1" ;
19
26
const baseUrl = "https://biomejs.dev" ;
20
- const docPathRegex = / ^ s r c \/ c o n t e n t \/ d o c s \/ ( .* ) \. m d x ? $ / i;
21
27
22
- const workArray = JSON . parse ( app . doShellScript ( `curl -sL " ${ docsUrl } "` ) ) . tree . map (
23
- ( /** @type { { path: string; } } */ entry ) => {
24
- const path = entry . path ;
28
+ // `\w{3,}` excludes translations of the docs, which are in folders like
29
+ // `/ja/` or `/zh-CN/` https://github.com/biomejs/website/tree/main/src/content/docs
30
+ const docPathRegex = / ^ s r c \/ c o n t e n t \/ d o c s \/ ( \w { 3 , } . * ) \. m d x ? $ / ;
25
31
26
- // GUARD
27
- const translatedDocs =
28
- path . includes ( "/zh-cn/" ) || path . includes ( "/ja/" ) || path . includes ( "/pt-br/" ) ;
29
- const isDocsSite = docPathRegex . test ( path ) && ! path . endsWith ( "404.md" ) ;
30
- if ( translatedDocs || ! isDocsSite ) return { } ;
32
+ const response = JSON . parse ( httpRequest ( docsUrl ) ) ;
33
+ if ( ! response ) return JSON . stringify ( { items : [ { title : "Could not load." , valid : false } ] } ) ;
31
34
32
- const subsite = path . replace ( docPathRegex , "$1" ) ;
33
- const parts = subsite . split ( "/" ) ;
34
- let title = parts . pop ( ) || "??" ;
35
- let category = parts . join ( "/" ) ;
36
- let url = `${ baseUrl } /${ subsite } ` ;
35
+ const workArray = response . tree . map ( ( /** @type {{ path: string; } } */ entry ) => {
36
+ const path = entry . path ;
37
+ const [ _ , subsite ] = path . match ( docPathRegex ) || [ ] ;
37
38
38
- if ( subsite . endsWith ( "index" ) ) {
39
- title = category ;
40
- category = "" ;
41
- url = `${ baseUrl } /${ subsite . slice ( 0 , - 5 ) } ` ;
42
- }
39
+ if ( ! subsite ) return { } ;
40
+ if ( path . endsWith ( "404.md" ) ) return { } ;
43
41
44
- if ( category . endsWith ( "rules" ) ) {
45
- // camelCase to conform to rule-casing the user expects
46
- title = title . replace ( / - \w / g, ( match ) => match . slice ( - 1 ) . toUpperCase ( ) ) ;
47
- } else {
48
- // capitalize
49
- title = title . replaceAll ( "-" , " " ) ;
50
- title = title . charAt ( 0 ) . toUpperCase ( ) + title . slice ( 1 ) ; // capitalize
51
- }
42
+ const parts = subsite . split ( "/" ) ;
43
+ let title = parts . pop ( ) || "??" ;
44
+ let category = parts . join ( "/" ) ;
45
+ let url = `${ baseUrl } /${ subsite } ` ;
52
46
53
- return {
54
- title : title ,
55
- subtitle : category ,
56
- match : alfredMatcher ( title ) ,
57
- arg : url ,
58
- mods : {
59
- cmd : { arg : title } , // copy entry
60
- } ,
61
- quicklookurl : url ,
62
- uid : title ,
63
- } ;
64
- } ,
65
- ) ;
47
+ if ( subsite . endsWith ( "index" ) ) {
48
+ title = category ;
49
+ category = "" ;
50
+ url = `${ baseUrl } /${ subsite . slice ( 0 , - 5 ) } ` ;
51
+ }
52
+
53
+ if ( category . endsWith ( "rules" ) ) {
54
+ // camelCase to conform to rule-casing the user expects
55
+ title = title . replace ( / - \w / g, ( match ) => match . slice ( - 1 ) . toUpperCase ( ) ) ;
56
+ } else {
57
+ // capitalize
58
+ title = title . replaceAll ( "-" , " " ) ;
59
+ title = title . charAt ( 0 ) . toUpperCase ( ) + title . slice ( 1 ) ; // capitalize
60
+ }
61
+
62
+ return {
63
+ title : title ,
64
+ subtitle : category ,
65
+ match : alfredMatcher ( title ) ,
66
+ arg : url ,
67
+ mods : {
68
+ cmd : { arg : title } , // copy entry
69
+ } ,
70
+ quicklookurl : url ,
71
+ uid : title ,
72
+ } ;
73
+ } ) ;
66
74
67
75
return JSON . stringify ( {
68
76
items : workArray ,
0 commit comments