Skip to content

Commit bd3f888

Browse files
committed
Add kanji and emoji support
1 parent bebb7a8 commit bd3f888

File tree

4 files changed

+65
-11
lines changed

4 files changed

+65
-11
lines changed

app/lib/permalink_normalizer.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class PermalinkNormalizer
2+
attr_accessor :text, :max_length
3+
4+
def initialize(text, max_length: 50)
5+
self.text = text
6+
self.max_length = max_length
7+
end
8+
9+
def normalize
10+
text
11+
.gsub(/((?!\p{Alnum}|\p{So}).)+/, "-")
12+
.delete_prefix("-")
13+
.delete_suffix("-")
14+
.downcase[0..max_length - 1]
15+
end
16+
end

app/models/concerns/permalinkable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module Permalinkable
1010

1111
def generate_permalink(count = 0)
1212
column_value = attributes.fetch(self.class.permalink_column)
13-
new_permalink = column_value.parameterize[0..49]
13+
new_permalink = PermalinkNormalizer.new(column_value).normalize
1414

1515
if count > 0
1616
new_permalink += "-#{count}"
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# coding: utf-8
2+
require 'rails_helper'
3+
4+
RSpec.describe PermalinkNormalizer do
5+
describe "#normalize" do
6+
subject { PermalinkNormalizer.new(text, max_length: 50).normalize }
7+
8+
let(:text) { "Hello world!" }
9+
10+
it { is_expected.to eql("hello-world") }
11+
12+
context "with a very long title" do
13+
let(:text) { "x" * 200 }
14+
15+
it { is_expected.to eql("x" * 50) }
16+
end
17+
18+
context "with bad characters at start and end" do
19+
let(:text) { "~ hello world ~" }
20+
21+
it { is_expected.to eql("hello-world") }
22+
end
23+
24+
context "with numbers" do
25+
let(:text) { "3 days ago" }
26+
27+
it { is_expected.to eql("3-days-ago") }
28+
end
29+
30+
context "with diacritics" do
31+
let(:text) { "naïve café" }
32+
33+
it { is_expected.to eql("naïve-café") }
34+
end
35+
36+
context "with kanji and kana" do
37+
let(:text) { "初めまして、ダニエルです。" }
38+
39+
it { is_expected.to eql("初めまして-ダニエルです") }
40+
end
41+
42+
context "with emoji" do
43+
let(:text) { "💎 is a gemstone 😃" }
44+
45+
it { is_expected.to eql("💎-is-a-gemstone-😃") }
46+
end
47+
end
48+
end

spec/models/article_spec.rb

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,6 @@
99
FactoryBot.build(:article, title: "Hello world!", category: category)
1010
}
1111

12-
it { is_expected.to eql("hello-world") }
13-
14-
context "with a very long title" do
15-
let(:article) {
16-
FactoryBot.build_stubbed(:article, title: "x" * 200)
17-
}
18-
19-
it { is_expected.to eql("x" * 50) }
20-
end
21-
2212
context "with existing article having the same title" do
2313
before do
2414
FactoryBot.create(:article, title: "Hello world!", category: category)

0 commit comments

Comments
 (0)