From eaa43488e56ea195167efc725d87b0afe050e4a0 Mon Sep 17 00:00:00 2001 From: Eamonn Bell Date: Thu, 22 Jun 2017 14:05:22 +0100 Subject: [PATCH] Remove case sensitivity of names of fields used in slug construction --- src/backends/backend.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/backends/backend.js b/src/backends/backend.js index 12d2541e..8a5054e7 100644 --- a/src/backends/backend.js +++ b/src/backends/backend.js @@ -26,7 +26,24 @@ class LocalStorageAuthStore { const slugFormatter = (template = "{{slug}}", entryData) => { const date = new Date(); - const identifier = entryData.get("title", entryData.get("path")); + + const getIdentifier = (entryData) => { + const validIdentifierFields = ["title", "path"]; + const identifiers = validIdentifierFields.map((field) => { + return entryData.find((_, key) => { + return key.toLowerCase() === field; + }); + }); + + const identifier = identifiers.find(i => typeof i !== 'undefined'); + + if (typeof identifier === 'undefined') { + throw new Error("Collection must have a field name that is a valid entry identifier"); + } else { + return identifier; + } + }; + return template.replace(/\{\{([^\}]+)\}\}/g, (_, field) => { switch (field) { case "year": @@ -36,7 +53,7 @@ const slugFormatter = (template = "{{slug}}", entryData) => { case "day": return (`0${ date.getDate() }`).slice(-2); case "slug": - return slug(identifier.trim(), {lower: true}); + return slug(getIdentifier(entryData).trim(), {lower: true}); default: return slug(entryData.get(field, "").trim(), {lower: true}); }