@@ -8,7 +8,13 @@ define([
8
8
'base/js/markdown' ,
9
9
] , function ( $ , utils , events , markdown ) {
10
10
"use strict" ;
11
-
11
+
12
+ function endsWith ( haystack , needle ) {
13
+ if ( haystack . endsWith ) return haystack . endsWith ( needle ) ;
14
+ return haystack . substring (
15
+ haystack . length - needle . length , haystack . length ) === needle ;
16
+ }
17
+
12
18
var DirectoryReadme = function ( selector , notebook_list ) {
13
19
/**
14
20
* Constructor
@@ -22,6 +28,12 @@ define([
22
28
this . element = $ ( selector ) ;
23
29
this . notebook_list = notebook_list ;
24
30
this . drawn_readme = null ;
31
+ this . readme_order = [
32
+ / ^ r e a d m e \. ( m d | m a r k d o w n ) $ / i,
33
+ / ^ a b o u t \. ( m d | m a r k d o w n ) $ / i,
34
+ / ^ r e a d m e ( \. [ ^ \. ] * ) ? $ / i,
35
+ / ^ a b o u t ( \. [ ^ \. ] * ) ? $ / i,
36
+ ]
25
37
26
38
this . init_readme ( ) ;
27
39
this . bind_events ( ) ;
@@ -30,17 +42,23 @@ define([
30
42
DirectoryReadme . prototype . find_readme = function ( ) {
31
43
/**
32
44
* Find a readme in the current directory. Look for files with
33
- * a name like 'readme.md' (case insensitive) or similar and
34
- * mimetype 'text/markdown'.
45
+ * a name matching a pattern in this.readme_order.
46
+ *
35
47
*
36
48
* @return null or { name, path, last_modified... }
37
49
*/
38
50
var files_in_directory = this . notebook_list . model_list . content ;
39
- for ( var i = 0 ; i < files_in_directory . length ; i ++ ) {
40
- var file = files_in_directory [ i ] ;
41
- if ( file . type === "file"
42
- && file . name . toLowerCase ( ) . split ( "." ) [ 0 ] === "readme" ) {
43
- return file ;
51
+
52
+
53
+ for ( var j = 0 ; j < this . readme_order . length ; ++ j ) {
54
+ var readme_name = this . readme_order [ j ] ;
55
+ for ( var i = 0 ; i < files_in_directory . length ; ++ i ) {
56
+ var file = files_in_directory [ i ] ;
57
+ if ( file . type === "file"
58
+ && file . name . match ( readme_name )
59
+ ) {
60
+ return file ;
61
+ }
44
62
}
45
63
}
46
64
return null ;
@@ -74,7 +92,11 @@ define([
74
92
var that = this ;
75
93
this . notebook_list . contents . get ( readme . path , { type : 'file' } ) . then (
76
94
function ( file ) {
77
- that . draw_readme ( file ) ;
95
+ if ( file . format !== "text" ) {
96
+ that . clear_readme ( file ) ;
97
+ } else {
98
+ that . draw_readme ( file ) ;
99
+ }
78
100
} ,
79
101
function ( ) {
80
102
that . clear_readme ( ) ;
@@ -89,6 +111,13 @@ define([
89
111
* When the notebook_list fires a draw_notebook event, fetch the readme.
90
112
*/
91
113
events . on ( "draw_notebook_list.NotebookList" , $ . proxy ( this . fetch_readme , this ) ) ;
114
+
115
+ var that = this ;
116
+ events . on ( "notebook_deleted.NotebookList" , function ( event , path ) {
117
+ if ( that . drawn_readme . path === path ) {
118
+ that . clear_readme ( ) ;
119
+ }
120
+ } ) ;
92
121
}
93
122
94
123
DirectoryReadme . prototype . init_readme = function ( ) {
@@ -113,11 +142,11 @@ define([
113
142
. appendTo ( element ) ;
114
143
}
115
144
116
- DirectoryReadme . prototype . clear_readme = function ( ) {
145
+ DirectoryReadme . prototype . clear_readme = function ( drawn_readme ) {
117
146
/**
118
147
* If no readme is found, hide.
119
148
*/
120
- this . drawn_readme = null ;
149
+ this . drawn_readme = drawn_readme || null ;
121
150
this . element . hide ( ) ;
122
151
}
123
152
@@ -139,13 +168,17 @@ define([
139
168
. text ( file . name ) ;
140
169
141
170
var page = this . page ;
142
- markdown . render ( file . content , {
143
- with_math : true ,
144
- sanitize : true
145
- } , function ( err , html ) {
146
- page . html ( html ) ;
147
- utils . typeset ( page ) ;
148
- } ) ;
171
+ if ( endsWith ( file . name . toLowerCase ( ) , ".md" ) || endsWith ( file . name . toLowerCase ( ) , ".markdown" ) ) {
172
+ markdown . render ( file . content , {
173
+ with_math : true ,
174
+ sanitize : true
175
+ } , function ( err , html ) {
176
+ page . html ( html ) ;
177
+ utils . typeset ( page ) ;
178
+ } ) ;
179
+ } else {
180
+ page . html ( $ ( "<pre>" ) . text ( file . content . replace ( / \r \n / g, '\n' ) ) ) ;
181
+ }
149
182
} ;
150
183
151
184
return { 'DirectoryReadme' : DirectoryReadme } ;
0 commit comments