Skip to content

Commit

Permalink
Smarter Jekyll or not determination.
Browse files Browse the repository at this point in the history
Only files scoped within _posts and ending with a markdown file extension are considered Jekyll files.
  • Loading branch information
michael committed Jun 26, 2012
1 parent 30c7854 commit af4a3c1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 20 deletions.
20 changes: 10 additions & 10 deletions _includes/model.js
Expand Up @@ -30,13 +30,6 @@ function getRepo(user, repo) {
return currentRepo.instance;
}

// Helpers
// -------

function isMarkdown(filename) {
var regex = new RegExp("^(\\w|-)*\.(md|mkdn?|mdown|markdown)$");
return regex.test(filename);
}

// Authentication
// -------
Expand Down Expand Up @@ -172,7 +165,7 @@ function loadSite(user, repo, branch, path, cb) {
}

if (file.type === "tree") return null; // Skip directories
if (isMarkdown(file.path)) return semantify(file, "markdown");
if (_.markdown(file.path)) return semantify(file, "markdown");
if (!app.state.jekyll) return semantify(file, "file");
return null;
});
Expand All @@ -194,7 +187,7 @@ function saveFile(user, repo, branch, path, file, metadata, content, message, cb

var repo = getRepo(user, repo);
function serialize() {
if (app.state.jekyll && isMarkdown(file)) {
if (app.state.jekyll && _.markdown(file)) {
return ["---", metadata, "---"].join('\n')+'\n\n'+content;
} else {
return content;
Expand Down Expand Up @@ -293,6 +286,13 @@ function loadPost(user, repo, branch, path, file, cb) {

// Extract metadata
var post = parse(data);
cb(err, _.extend(post, {"markdown": isMarkdown(file), "jekyll": app.state.jekyll, "repo": repo, "path": path, "file": file, "persisted": true}));
cb(err, _.extend(post, {
"markdown": _.markdown(file),
"jekyll": _.jekyll(path, file),
"repo": repo,
"path": path,
"file": file,
"persisted": true
}));
});
}
15 changes: 15 additions & 0 deletions _includes/util.js
Expand Up @@ -33,6 +33,21 @@ _.validFilename = function(filename) {
};


// Check if a given file is a Jekyll post
// -------

_.jekyll = function(path, file) {
return !!(path.match('_posts') && _.markdown(file));
};

// Determines whether a given file is a markdown file or not
// -------

_.markdown = function(file) {
var regex = new RegExp("^(\\w|-)*\.(md|mkdn?|mdown|markdown)$");
return !!(regex.test(file));
};

// Check for a valid post file name
// -------

Expand Down
10 changes: 4 additions & 6 deletions _includes/views/application.js
Expand Up @@ -84,7 +84,7 @@ views.Application = Backbone.View.extend({
this.header.render();
if (err) return this.notify('error', 'The requested resource could not be found.');
data.preview = preview;

this.replaceMainView("post", new views.Post({ model: data, id: 'post' }).render());
var that = this;
}, this));
Expand All @@ -93,16 +93,14 @@ views.Application = Backbone.View.extend({
},

newPost: function (user, repo, branch, path) {
app.state.jekyll = true;

loadSite(user, repo, branch, path, _.bind(function (err, data) {
emptyPost(user, repo, branch, path, _.bind(function(err, data) {
data.jekyll = _.jekyll(path, data.file);
data.preview = false;
data.jekyll = true;
data.markdown = true;
data.markdown = _.markdown(data.file);
this.replaceMainView("post", new views.Post({ model: data, id: 'post' }).render());
this.mainView._makeDirty();
app.state.file = this.mainView.model.file;
app.state.file = data.file;
this.header.render();
}, this));
}, this));
Expand Down
7 changes: 3 additions & 4 deletions _includes/views/post.js
Expand Up @@ -192,7 +192,7 @@ views.Post = Backbone.View.extend({
}

function save() {
if (!app.state.jekyll || that.updateMetaData()) {
if (!that.model.jekyll || that.updateMetaData()) {

saveFile(app.state.user, app.state.repo, app.state.branch, that.model.path, file, that.model.raw_metadata, that.editor.getValue(), message, function(err) {
if (err) {
Expand Down Expand Up @@ -241,7 +241,7 @@ views.Post = Backbone.View.extend({
initEditor: function() {
var that = this;
setTimeout(function() {
if (app.state.jekyll) {
if (that.model.jekyll) {
that.metadataEditor = CodeMirror($('#raw_metadata')[0], {
mode: 'yaml',
value: that.model.raw_metadata,
Expand All @@ -263,7 +263,6 @@ views.Post = Backbone.View.extend({
onChange: _.bind(that._makeDirty, that)
});


}, 100);
},

Expand All @@ -274,7 +273,7 @@ views.Post = Backbone.View.extend({

render: function() {
var that = this;
$(this.el).html(templates.post(_.extend(this.model, { mode: this.mode, jekyll: app.state.jekyll })));
$(this.el).html(templates.post(_.extend(this.model, { mode: this.mode })));
if (this.model.metadata.published) $(this.el).addClass('published');
this.initEditor();
return this;
Expand Down

0 comments on commit af4a3c1

Please sign in to comment.