Move slug sanitizer to a seperate function.
This commit is contained in:
parent
4e5a004010
commit
72492749d9
@ -5,8 +5,7 @@ import GitGatewayBackend from "./git-gateway/implementation";
|
|||||||
import { resolveFormat } from "../formats/formats";
|
import { resolveFormat } from "../formats/formats";
|
||||||
import { selectListMethod, selectEntrySlug, selectEntryPath, selectAllowNewEntries, selectFolderEntryExtension } from "../reducers/collections";
|
import { selectListMethod, selectEntrySlug, selectEntryPath, selectAllowNewEntries, selectFolderEntryExtension } from "../reducers/collections";
|
||||||
import { createEntry } from "../valueObjects/Entry";
|
import { createEntry } from "../valueObjects/Entry";
|
||||||
import { sanitizeIRI } from "../lib/urlHelper";
|
import { sanitizeSlug } from "../lib/urlHelper";
|
||||||
import sanitizeFilename from 'sanitize-filename';
|
|
||||||
|
|
||||||
class LocalStorageAuthStore {
|
class LocalStorageAuthStore {
|
||||||
storageKey = "netlify-cms-user";
|
storageKey = "netlify-cms-user";
|
||||||
@ -43,7 +42,7 @@ const slugFormatter = (template = "{{slug}}", entryData) => {
|
|||||||
return identifier;
|
return identifier;
|
||||||
};
|
};
|
||||||
|
|
||||||
let slug = template.replace(/\{\{([^\}]+)\}\}/g, (_, field) => {
|
const slug = template.replace(/\{\{([^\}]+)\}\}/g, (_, field) => {
|
||||||
switch (field) {
|
switch (field) {
|
||||||
case "year":
|
case "year":
|
||||||
return date.getFullYear();
|
return date.getFullYear();
|
||||||
@ -56,21 +55,14 @@ const slugFormatter = (template = "{{slug}}", entryData) => {
|
|||||||
default:
|
default:
|
||||||
return entryData.get(field, "").trim();
|
return entryData.get(field, "").trim();
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
|
// Convert slug to lower-case
|
||||||
// Convert slug to lower-case;
|
.toLocaleLowerCase()
|
||||||
slug = slug.toLocaleLowerCase();
|
|
||||||
|
|
||||||
// Replace periods and spaces with dashes.
|
// Replace periods and spaces with dashes.
|
||||||
slug = slug.replace(/[.\s]/g, '-');
|
.replace(/[.\s]/g, '-');
|
||||||
// Sanitize as IRI (i18n URI) and as filename.
|
|
||||||
slug = sanitizeIRI(slug, {replacement: "-"});
|
|
||||||
slug = sanitizeFilename(slug, {replacement: "-"});
|
|
||||||
|
|
||||||
// Remove any doubled or trailing replacement characters (that were added in the sanitizers).
|
return sanitizeSlug(slug);
|
||||||
slug = slug.replace(/-+/g, '-').replace(/-$/, '');
|
|
||||||
|
|
||||||
return slug;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class Backend {
|
class Backend {
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
import url from 'url';
|
import url from 'url';
|
||||||
|
import sanitizeFilename from 'sanitize-filename';
|
||||||
|
import { isString, escapeRegExp } from 'lodash';
|
||||||
|
|
||||||
function getUrl(url, direct) {
|
function getUrl(url, direct) {
|
||||||
return `${ direct ? '/#' : '' }${ url }`;
|
return `${ direct ? '/#' : '' }${ url }`;
|
||||||
@ -28,6 +30,23 @@ export function sanitizeIRI(str, { replacement }) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function sanitizeSlug(str, { replacement = '-' }) {
|
||||||
|
if (!isString(str)) throw "`sanitizeSlug` only accepts strings as input.";
|
||||||
|
if (!isString(replacement)) throw "the `sanitizeSlug` replacement character must be a string.";
|
||||||
|
let slug = str;
|
||||||
|
|
||||||
|
// Sanitize as IRI (i18n URI) and as filename.
|
||||||
|
slug = sanitizeIRI(slug, {replacement});
|
||||||
|
slug = sanitizeFilename(slug, {replacement});
|
||||||
|
|
||||||
|
// Remove any doubled or trailing replacement characters (that were added in the sanitizers).
|
||||||
|
const doubleReplacement = new RegExp('(?:' + escapeRegExp(replacement) + ')+', 'g');
|
||||||
|
const trailingReplacment = new RegExp(escapeRegExp(replacement) + '$')
|
||||||
|
slug = slug.replace(doubleReplacement, '-').replace(trailingReplacment, '');
|
||||||
|
|
||||||
|
return slug;
|
||||||
|
}
|
||||||
|
|
||||||
export function urlize(string) {
|
export function urlize(string) {
|
||||||
const sanitized = makePathSanitized(string);
|
const sanitized = makePathSanitized(string);
|
||||||
const parsedURL = url.parse(sanitized);
|
const parsedURL = url.parse(sanitized);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user