From 527b02e8b385ff5d2c93b4f661da831325546a33 Mon Sep 17 00:00:00 2001 From: sauls8t Date: Fri, 30 Mar 2018 11:27:25 +0100 Subject: [PATCH] Improve JS slugify process To handle non-recognized charsets. --- core/stringutil/slug.go | 2 ++ core/stringutil/slug_test.go | 4 ++++ gui/app/utils/string.js | 13 ++++++++---- gui/tests/unit/utils/slug-test.js | 33 +++++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 gui/tests/unit/utils/slug-test.js diff --git a/core/stringutil/slug.go b/core/stringutil/slug.go index f36f2e89..b587486c 100644 --- a/core/stringutil/slug.go +++ b/core/stringutil/slug.go @@ -16,6 +16,8 @@ import ( "unicode" ) +// TODO" replace with https://github.com/mcmatts/slug + // MakeSlug creates a slug, suitable for use in a URL, from a string func MakeSlug(str string) string { slg := strings.Map( diff --git a/core/stringutil/slug_test.go b/core/stringutil/slug_test.go index c6879b2b..d914f8d8 100644 --- a/core/stringutil/slug_test.go +++ b/core/stringutil/slug_test.go @@ -13,8 +13,12 @@ package stringutil import "testing" +// go test github.com/documize/community/core/stringutil -run TestSlug func TestSlug(t *testing.T) { + st(t, "Hello World", "hello-world") st(t, " Zip--up ", "zip-up") + st(t, "Общее", "obshee") + st(t, "哈威", "哈威") } func st(t *testing.T, in, out string) { diff --git a/gui/app/utils/string.js b/gui/app/utils/string.js index 3fa66de3..a028d069 100644 --- a/gui/app/utils/string.js +++ b/gui/app/utils/string.js @@ -1,19 +1,24 @@ // Copyright 2016 Documize Inc. . All rights reserved. // -// This software (Documize Community Edition) is licensed under +// This software (Documize Community Edition) is licensed under // GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html // // You can operate outside the AGPL restrictions by purchasing // Documize Enterprise Edition and obtaining a commercial license -// by contacting . +// by contacting . // // https://documize.com // Make url friendly slug from specified text. // Has to handle non-english character text "Общее" text! function makeSlug(text) { - return slug(text, { mode: 'rfc3986', lower: true}); - //return text.toLowerCase().replace(/[^\w ]+/g, '').replace(/ +/g, '-'); + let s = slug(text, { mode: 'rfc3986', lower: true}); + + // If slug does not recognise characters (e.g. chinese) + // send back original characters. + if (s === '') s = text; + + return s; } function makeId(len) { diff --git a/gui/tests/unit/utils/slug-test.js b/gui/tests/unit/utils/slug-test.js new file mode 100644 index 00000000..d4f9a26c --- /dev/null +++ b/gui/tests/unit/utils/slug-test.js @@ -0,0 +1,33 @@ +// Copyright 2016 Documize Inc. . All rights reserved. +// +// This software (Documize Community Edition) is licensed under +// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html +// +// You can operate outside the AGPL restrictions by purchasing +// Documize Enterprise Edition and obtaining a commercial license +// by contacting . +// +// https://documize.com + +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; +import stringUtil from 'documize/utils/string'; + +module('Unit | Utility | Slug', function (hooks) { + setupTest(hooks); + + test('english slug', function (assert) { + let result = stringUtil.makeSlug('Hello Slug'); + assert.equal(result, 'hello-slug', 'slug: ' + result); + }); + + test('cyrillic slug', function (assert) { + let result = stringUtil.makeSlug('Общее'); + assert.equal(result, 'obshee', 'slug: ' + result); + }); + + test('chinese slug', function (assert) { + let result = stringUtil.makeSlug('哈威'); + assert.equal(result, '哈威', 'slug: ' + result); + }); +});