@@ -12,7 +12,7 @@ import { PoweredBy } from '@/components/PoweredBy';
12
12
import { trackUsage } from '@/lib/usage' ;
13
13
import { DEFAULT_SORT } from '@/components/SortSelect' ;
14
14
import { loadOutline } from '@/lib/loadOutline' ;
15
- // import { DEFAULT_TRANSFORM } from '@/components/TransformSelect';
15
+ import { DEFAULT_TRANSFORM , getTransform } from '@/components/TransformSelect' ;
16
16
17
17
export default async function View ( {
18
18
searchParams,
@@ -44,81 +44,86 @@ export default async function View({
44
44
45
45
trackUsage ( url_str ) ;
46
46
47
- const sme :OpmlData = await loadOutline ( url_str ) ;
48
- const items :TreeItem [ ] = sme . root . children ;
47
+ const sme : OpmlData = await loadOutline ( url_str ) ;
48
+ const items : TreeItem [ ] = sme . root . children ;
49
49
50
50
if ( useRssStyle ) {
51
51
transform ( items , addRssStyle ) ;
52
52
}
53
53
54
54
if ( sort === 'name' ) {
55
- sortTreeName ( items ) ;
55
+ sortTree ( items , ( a , b ) => a . label . localeCompare ( b . label ) ) ;
56
56
} else if ( sort === 'dirfirst' ) {
57
- sortTreeDirFirst ( items ) ;
57
+ sortTree ( items , compareDirFirst ) ;
58
+ } else if ( sort === 'url' ) {
59
+ sortTree ( items , compareUrl ) ;
58
60
}
59
61
62
+ const transformer = getTransform ( getFirst ( urlParams [ 'transform' ] , DEFAULT_TRANSFORM ) ) ;
63
+ if ( transformer ) {
64
+ transform ( items , transformer ) ;
65
+ }
66
+
67
+
60
68
const title = sme . title || customTitle ;
61
69
if ( ! sme . success ) {
62
70
showDebug = true ;
63
71
}
64
72
65
73
return (
66
74
< >
67
- < Container maxWidth = { false } disableGutters = { true } sx = { { minHeight : '100vh' } } >
75
+ < Container maxWidth = { false } disableGutters = { true } sx = { { minHeight : '100vh' } } >
68
76
< NavBar debug = { showDebug } exit = { showExit } language = { showLanguage } messages = { sme . messages } mode = { showMode } title = { title } returnUrl = { returnUrl } />
69
- < Container
70
- maxWidth = "md"
71
- disableGutters = { true }
72
- sx = { { alignItems : "center" , display : "flex" , flexDirection : "column" , justifyContent : "top" , minHeight : '100vh' } }
73
- >
74
- < Box
75
- sx = { {
76
- display : 'flex' ,
77
- flexDirection : 'column' ,
78
- width : '100%' ,
79
- } }
77
+ < Container
78
+ maxWidth = "md"
79
+ disableGutters = { true }
80
+ sx = { { alignItems : "center" , display : "flex" , flexDirection : "column" , justifyContent : "top" , minHeight : '100vh' } }
80
81
>
81
- { sme . success || items . length ? < OpmlTreeView items = { items } /> : < h1 > Failed to load outline</ h1 > }
82
- </ Box >
83
- < PoweredBy />
82
+ < Box
83
+ sx = { {
84
+ display : 'flex' ,
85
+ flexDirection : 'column' ,
86
+ width : '100%' ,
87
+ } }
88
+ >
89
+ { sme . success || items . length ? < OpmlTreeView items = { items } /> : < h1 > Failed to load outline</ h1 > }
90
+ </ Box >
91
+ < PoweredBy />
92
+ </ Container >
84
93
</ Container >
85
- </ Container >
86
94
</ >
87
95
88
96
) ;
89
97
}
90
98
91
- function sortTreeName ( items : TreeItem [ ] ) {
99
+
100
+
101
+ function sortTree ( items : TreeItem [ ] , sortfn : ( a : TreeItem , b : TreeItem ) => number ) {
92
102
if ( items . length == 0 ) {
93
103
return ;
94
104
}
95
105
if ( items . length > 1 ) {
96
- items . sort ( ( a , b ) => a . label . localeCompare ( b . label ) ) ;
106
+ items . sort ( sortfn ) ;
97
107
}
98
108
99
109
for ( const item of items ) {
100
- sortTreeName ( item . children ) ;
110
+ sortTree ( item . children , sortfn ) ;
101
111
}
102
112
}
103
113
104
- function sortTreeDirFirst ( items : TreeItem [ ] ) {
105
- if ( items . length == 0 ) {
106
- return ;
107
- }
108
- if ( items . length > 1 ) {
109
- items . sort ( ( a , b ) => {
110
- if ( a . children . length > 0 && b . children . length == 0 ) {
111
- return - 1 ;
112
- } else if ( a . children . length == 0 && b . children . length > 0 ) {
113
- return 1 ;
114
- }
115
- return a . label . localeCompare ( b . label )
116
- } ) ;
114
+ function compareDirFirst ( a : TreeItem , b : TreeItem ) {
115
+ if ( a . children . length > 0 && b . children . length == 0 ) {
116
+ return - 1 ;
117
+ } else if ( a . children . length == 0 && b . children . length > 0 ) {
118
+ return 1 ;
117
119
}
120
+ return a . label . localeCompare ( b . label )
121
+ }
118
122
119
- for ( const item of items ) {
120
- sortTreeDirFirst ( item . children ) ;
121
- }
123
+ function compareUrl ( a : TreeItem , b : TreeItem ) {
124
+ const aUrl = a . htmlUrl || a . xmlUrl || a . label ;
125
+ const bUrl = b . htmlUrl || b . xmlUrl || b . label ;
126
+ return aUrl . localeCompare ( bUrl ) ;
122
127
}
123
128
124
129
function addRssStyle ( item : TreeItem ) {
@@ -127,7 +132,7 @@ function addRssStyle(item: TreeItem) {
127
132
}
128
133
}
129
134
130
- function transform ( items : TreeItem [ ] , transformer : ( item :TreeItem ) => void ) {
135
+ function transform ( items : TreeItem [ ] , transformer : ( item : TreeItem ) => void ) {
131
136
for ( const item of items ) {
132
137
transformer ( item ) ;
133
138
if ( item . children . length > 0 ) {
0 commit comments