Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions packages/kg-utils/lib/slugify.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
const semver = require('semver');

module.exports = function (inputString = '', {ghostVersion = '4.0', type = 'mobiledoc'} = {}) {
/**
* Helper function to create a slug from a string
* @param {string} inputString - The string to slugify
* @param {RegExp} symbolRegex - Regex for symbols to remove
* @returns {string}
*/
function createSlug(inputString, symbolRegex) {
return encodeURIComponent(
inputString.trim()
.toLowerCase()
.replace(symbolRegex, '')
.replace(/\s+/g, '-')
.replace(/^-|-{2,}|-$/g, '')
);
}

module.exports = function (inputString = '', {ghostVersion = '6.0', type = 'mobiledoc'} = {}) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a heads up that I changed the default to 6.0

const version = semver.coerce(ghostVersion);

if (typeof inputString !== 'string' || (inputString || '').trim() === '') {
Expand All @@ -19,15 +35,15 @@ module.exports = function (inputString = '', {ghostVersion = '4.0', type = 'mobi
.replace(/-{2,}/g, '-')
.toLowerCase();
}
}

// new slugs introduced in 4.0
// allows all chars except symbols but will urlEncode everything
// produces %-encoded chars in src but browsers show real chars in status bar and url bar
if (semver.satisfies(version, '<6.x')) {
return createSlug(inputString, /[\][!"#$%&'()*+,./:;<=>?@\\^_{|}~]/g);
} else {
// new slugs introduced in 4.0
// allows all chars except symbols but will urlEncode everything
// produces %-encoded chars in src but browsers show real chars in status bar and url bar
return encodeURIComponent(inputString.trim()
.toLowerCase()
.replace(/[\][!"#$%&'()*+,./:;<=>?@\\^_{|}~]/g, '')
.replace(/\s+/g, '-')
.replace(/^-|-{2,}|-$/g, '')
);
// For ghost versions 6.x and above, remove additional symbols
return createSlug(inputString, /[\][!"#$%&'()*+,./:;<=>?@\\^_{|}~‘’“”`¡¿–—•]/g);
}
};
17 changes: 12 additions & 5 deletions packages/kg-utils/test/slugify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,34 @@ describe('slugify()', function () {
});
});

describe('4.x', function () {
describe('>=4.x <6.x', function () {
it('replaces all white space with "-"', function () {
slugify('test one\t two')
slugify('test one\t two', {ghostVersion: '4.0'})
.should.equal('test-one-two');
});

it('strips symbols', function () {
slugify('test! one? {two}')
slugify('test! one? {two}', {ghostVersion: '4.0'})
.should.equal('test-one-two');
});

it('%-encodes chars', function () {
const slug = slugify('ñéïñ');
const slug = slugify('ñéïñ', {ghostVersion: '4.0'});

slug.should.equal('%C3%B1%C3%A9%C3%AF%C3%B1');
decodeURIComponent(slug).should.equal('ñéïñ');
});

it('removes leading/trailing "-" and collapses "-" groups', function () {
slugify(' \ttest one two! \t')
slugify(' \ttest one two! \t', {ghostVersion: '4.0'})
.should.equal('test-one-two');
});
});

describe('>=6.x', function () {
it('removes additional symbols', function () {
slugify('test “fancy” ‘quotes’ – — dashes ¡and! other¿ • stuff`')
.should.equal('test-fancy-quotes-dashes-and-other-stuff');
});
});
});