Skip to content

Commit 57a1c6e

Browse files
committed
enforce deterministic order
detect deletion of files
1 parent ee2f0e3 commit 57a1c6e

File tree

2 files changed

+52
-19
lines changed

2 files changed

+52
-19
lines changed

notebook/static/tree/js/directoryreadme.js

+51-18
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ define([
88
'base/js/markdown',
99
], function ($, utils, events, markdown) {
1010
"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+
1218
var DirectoryReadme = function (selector, notebook_list) {
1319
/**
1420
* Constructor
@@ -22,6 +28,12 @@ define([
2228
this.element = $(selector);
2329
this.notebook_list = notebook_list;
2430
this.drawn_readme = null;
31+
this.readme_order = [
32+
/^readme\.(md|markdown)$/i,
33+
/^about\.(md|markdown)$/i,
34+
/^readme(\.[^\.]*)?$/i,
35+
/^about(\.[^\.]*)?$/i,
36+
]
2537

2638
this.init_readme();
2739
this.bind_events();
@@ -30,17 +42,23 @@ define([
3042
DirectoryReadme.prototype.find_readme = function() {
3143
/**
3244
* 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+
*
3547
*
3648
* @return null or { name, path, last_modified... }
3749
*/
3850
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+
}
4462
}
4563
}
4664
return null;
@@ -74,7 +92,11 @@ define([
7492
var that = this;
7593
this.notebook_list.contents.get(readme.path, {type: 'file'}).then(
7694
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+
}
78100
},
79101
function() {
80102
that.clear_readme();
@@ -89,6 +111,13 @@ define([
89111
* When the notebook_list fires a draw_notebook event, fetch the readme.
90112
*/
91113
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+
});
92121
}
93122

94123
DirectoryReadme.prototype.init_readme = function() {
@@ -113,11 +142,11 @@ define([
113142
.appendTo(element);
114143
}
115144

116-
DirectoryReadme.prototype.clear_readme = function () {
145+
DirectoryReadme.prototype.clear_readme = function (drawn_readme) {
117146
/**
118147
* If no readme is found, hide.
119148
*/
120-
this.drawn_readme = null;
149+
this.drawn_readme = drawn_readme || null;
121150
this.element.hide();
122151
}
123152

@@ -139,13 +168,17 @@ define([
139168
.text(file.name);
140169

141170
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+
}
149182
};
150183

151184
return {'DirectoryReadme': DirectoryReadme};

notebook/static/tree/js/notebooklist.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1285,7 +1285,7 @@ define([
12851285
var element = $(this);
12861286
if (element.data("path") === path) {
12871287
element.remove();
1288-
events.trigger('notebook_deleted.NotebookList');
1288+
events.trigger('notebook_deleted.NotebookList', [path]);
12891289
that._selection_changed();
12901290
}
12911291
});

0 commit comments

Comments
 (0)