strapi/packages/strapi-utils/lib/stringFormatting.js
Marvin König fa31b5dfc2
Fix file names containing reserved and unsafe URL characters
This fixes the `generateFileName` function in the Strapi upload plugin,
so that reserved and unsafe charactes for URLs are replaced with
underscores.

Signed-off-by: Marvin König <dev@mkqavi.com>
2020-05-13 13:17:26 +02:00

41 lines
894 B
JavaScript

'use strict';
const slugify = require('@sindresorhus/slugify');
const nameToSlug = (name, options = { separator: '-' }) => slugify(name, options);
const nameToCollectionName = name => slugify(name, { separator: '_' });
const getCommonBeginning = (str1 = '', str2 = '') => {
let common = '';
let index = 0;
while (index < str1.length && index < str2.length) {
if (str1[index] === str2[index]) {
common += str1[index];
index += 1;
} else {
break;
}
}
return common;
};
const escapeQuery = (query, charsToEscape, escapeChar = '\\') => {
return query
.split('')
.reduce(
(escapedQuery, char) =>
charsToEscape.includes(char)
? `${escapedQuery}${escapeChar}${char}`
: `${escapedQuery}${char}`,
''
);
};
module.exports = {
nameToSlug,
nameToCollectionName,
getCommonBeginning,
escapeQuery,
};