Skip to content

Commit 4b7dda4

Browse files
authored
Improved meta title suffix (#4)
1 parent 416900a commit 4b7dda4

File tree

6 files changed

+27
-16
lines changed

6 files changed

+27
-16
lines changed

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ This creates an initializer:
2626
```ruby
2727
Perron.configure do |config|
2828
config.site_name = "AppRefresher"
29-
30-
# Override the defaults (meta) title suffix
31-
# Default: `— Perron.configuration.site_name`
32-
# config.title_suffix = nil
3329
end
3430
```
3531

lib/generators/perron/templates/initializer.rb.tt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ Perron.configure do |config|
1111

1212
# config.default_url_options = {host: "apprefresher.com", protocol: "https", trailing_slash: true}
1313

14-
# Override the defaults (meta) title suffix
15-
# Default: `— Perron.configuration.site_name`
16-
# config.title_suffix = nil
17-
1814
# Set default meta values
1915
# Examples:
2016
# - `config.metadata.description = "AI-powered tool to keep your knowledge base articles images/screenshots and content up-to-date"`
2117
# - `config.metadata.author = "Rails Designer"`
18+
19+
# Set meta title suffix
20+
# config.metadata.title_suffix = nil
21+
# config.metadata.title_separator = " — "
2222
end

lib/perron/configuration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ def initialize
2323
@config.include_root = false
2424

2525
@config.site_name = nil
26-
@config.title_suffix = nil
2726

2827
@config.allowed_extensions = [".erb", ".md"]
2928
@config.exclude_from_public = %w[assets storage]
@@ -36,6 +35,7 @@ def initialize
3635
}
3736

3837
@config.metadata = ActiveSupport::OrderedOptions.new
38+
@config.metadata.title_separator = " — "
3939
end
4040

4141
def input = "app/content"

lib/perron/metatags.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def tags
2929
frontmatter = @resource&.metadata&.stringify_keys || {}
3030
defaults = @config.metadata
3131

32-
title = frontmatter["title"] || defaults["title"] || @config.site_name
32+
title = frontmatter["title"] || defaults["title"] || @config.site_name || Rails.application.name.underscore.camelize
3333
description = frontmatter["description"] || defaults["description"]
3434
author = frontmatter["author"] || defaults["author"]
3535
image = frontmatter["image"] || defaults["image"]
@@ -59,9 +59,12 @@ def tags
5959
end
6060

6161
def title_tag(content)
62-
tag.title(
63-
content.then { (it == @config.site_name) ? it : "#{it} #{@config.title_suffix || "— #{@config.site_name}"}" }
64-
)
62+
resource_title = content.to_s.strip
63+
title_suffix = Perron.configuration.metadata.title_suffix&.strip
64+
65+
suffix = (title_suffix if title_suffix.present? && resource_title != title_suffix)
66+
67+
tag.title([resource_title, suffix].compact.join(Perron.configuration.metadata.title_separator))
6568
end
6669

6770
def meta_tag(attributes)

test/dummy/config/application.rb

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

1111
require "perron"
1212

13-
module Dummy
13+
module AppRefresher
1414
class Application < Rails::Application
1515
config.load_defaults Rails::VERSION::STRING.to_f
1616

test/metatags_test.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class MetatagsTest < ActiveSupport::TestCase
88
setup do
99
Perron.configure do |config|
10-
config.site_name = "AppRefresher"
10+
config.metadata.title_suffix = "AppRefresher"
1111
end
1212
end
1313

@@ -18,7 +18,19 @@ class MetatagsTest < ActiveSupport::TestCase
1818
assert_match "<title>Sample Post — AppRefresher</title>", metatags
1919
end
2020

21-
test "renders title using site_name when resource has no title" do
21+
test "renders only the page title when the page title is the same as the suffix" do
22+
Perron.configuration.metadata.title_suffix = nil
23+
24+
resource = Perron::Resource.new("test/dummy/app/content/pages/about.md")
25+
html = Perron::Metatags.new(resource).render
26+
27+
assert_match "<title>About</title>", html
28+
assert_no_match "—", html
29+
end
30+
31+
test "renders title using Rails application name when resource has no title" do
32+
Perron.configuration.metadata.title_suffix = nil
33+
2234
resource = Perron::Resource.new("test/dummy/app/content/pages/root.md")
2335
html = Perron::Metatags.new(resource).render
2436

0 commit comments

Comments
 (0)