diff --git a/src/backends/backend.js b/src/backends/backend.js index 049f1262..312b93ec 100644 --- a/src/backends/backend.js +++ b/src/backends/backend.js @@ -73,19 +73,49 @@ class Backend { }; } - persistEntry(collection, entryDraft, MediaFiles) { - const entryData = entryDraft.getIn(['entry', 'data']).toObject(); - const entryObj = { - path: entryDraft.getIn(['entry', 'path']), - slug: entryDraft.getIn(['entry', 'slug']), - raw: this.entryToRaw(collection, entryData) - }; + slugFormatter(template, entry) { + var date = new Date(); + return template.replace(/\{\{([^\}]+)\}\}/g, function(_, name) { + switch (name) { + case 'year': + return date.getFullYear(); + case 'month': + return ('0' + (date.getMonth() + 1)).slice(-2); + case 'day': + return ('0' + date.getDate()).slice(-2); + case 'slug': + return entry.getIn(['data', 'title']).trim().toLowerCase().replace(/[^a-z0-9\.\-\_]+/gi, '-'); + default: + return entry.getIn(['data', name]); + } + }); + } - const commitMessage = (entryDraft.getIn(['entry', 'newRecord']) ? 'Created ' : 'Updated ') + + persistEntry(collection, entryDraft, MediaFiles) { + const newEntry = entryDraft.getIn(['entry', 'newRecord']) || false; + const entryData = entryDraft.getIn(['entry', 'data']).toObject(); + let entryObj; + + if (newEntry) { + const slug = this.slugFormatter(collection.get('slug'), entryDraft.get('entry')); + entryObj = { + path: `${collection.get('folder')}/${slug}.md`, + slug: slug, + raw: this.entryToRaw(collection, entryData) + }; + } else { + entryObj = { + path: entryDraft.getIn(['entry', 'path']), + slug: entryDraft.getIn(['entry', 'slug']), + raw: this.entryToRaw(collection, entryData) + }; + } + + const commitMessage = (newEntry ? 'Created ' : 'Updated ') + collection.get('label') + ' “' + entryDraft.getIn(['entry', 'data', 'title']) + '”'; - return this.implementation.persistEntry(collection, entryObj, MediaFiles, { commitMessage }); + return this.implementation.persistEntry(collection, entryObj, MediaFiles, { commitMessage }, newEntry); } entryToRaw(collection, entry) { diff --git a/src/backends/test-repo/implementation.js b/src/backends/test-repo/implementation.js index 806791e2..3d534029 100644 --- a/src/backends/test-repo/implementation.js +++ b/src/backends/test-repo/implementation.js @@ -45,10 +45,14 @@ export default class TestRepo { )); } - persistEntry(collection, entry, mediaFiles = []) { + persistEntry(collection, entry, mediaFiles = [], newEntry = false) { const folder = entry.path.substring(0, entry.path.lastIndexOf('/')); const fileName = entry.path.substring(entry.path.lastIndexOf('/') + 1); - window.repoFiles[folder][fileName]['content'] = entry.raw; + if (newEntry) { + window.repoFiles[folder][fileName] = { content: entry.raw }; + } else { + window.repoFiles[folder][fileName]['content'] = entry.raw; + } mediaFiles.forEach(media => media.uploaded = true); return Promise.resolve(); } diff --git a/src/reducers/entryDraft.js b/src/reducers/entryDraft.js index c058742d..b00de43a 100644 --- a/src/reducers/entryDraft.js +++ b/src/reducers/entryDraft.js @@ -10,12 +10,14 @@ const entryDraft = (state = Map(), action) => { // Existing Entry return state.withMutations((state) => { state.set('entry', action.payload); + state.setIn(['entry', 'newRecord'], false); state.set('mediaFiles', List()); }); case DRAFT_CREATE_EMPTY: // New Entry return state.withMutations((state) => { state.set('entry', fromJS(action.payload)); + state.setIn(['entry', 'newRecord'], true); state.set('mediaFiles', List()); }); case DRAFT_DISCARD: