Skip to content

Commit 56f4c29

Browse files
committed
Support canonical links for section chunks
By adding a `canonical-url` attribute to a section, the chunked HTML page for that section will contain a `<link rel="canonical" ...>` tag in the page header. This tag does not get included in later chunks of the same input document. Fixes #2357
1 parent ee83ca3 commit 56f4c29

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

resources/asciidoctor/lib/chunker/extension.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ def subdoc_attrs(doc, section)
132132
attrs['subdoc'] = true # Mark the subdoc so we don't try and chunk it
133133
attrs['noheader'] = true
134134
attrs['title-separator'] = ''
135+
attrs['canonical-url'] = section.attributes['canonical-url']
135136
attrs.merge! find_related(section)
136137
attrs
137138
end

resources/asciidoctor/lib/chunker/extra_docinfo.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,22 @@ def extra_chunker_head
2424
link_rel('up', attributes['up_section']),
2525
link_rel('prev', attributes['prev_section']),
2626
link_rel('next', attributes['next_section']),
27+
link_rel('canonical', attributes['canonical-url'])
2728
].compact.join "\n"
2829
end
2930

3031
def link_rel(rel, related)
3132
return unless related
3233

33-
extra = related.context == :document ? related.attr('title-extra') : ''
34-
title = "#{strip_tags(link_text(related))}#{extra}"
35-
# We're in an attribute so escape quotes too!
36-
title = title.gsub '"', '&quot;'
37-
%(<link rel="#{rel}" #{link_href related} title="#{title}"/>)
34+
if related.is_a?(String)
35+
%(<link rel="#{rel}" href="#{related}"/>)
36+
else
37+
extra = related.context == :document ? related.attr('title-extra') : ''
38+
title = "#{strip_tags(link_text(related))}#{extra}"
39+
# We're in an attribute so escape quotes too!
40+
title = title.gsub '"', '&quot;'
41+
%(<link rel="#{rel}" #{link_href related} title="#{title}"/>)
42+
end
3843
end
3944
end
4045
end

resources/asciidoctor/spec/chunker_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,38 @@ def dest_file(file)
529529
end
530530
end
531531
end
532+
context 'the section has a canonical link' do
533+
let(:input) do
534+
<<~ASCIIDOC
535+
= Title
536+
537+
[id="otd",canonical-url="bazbar"]
538+
== Outdated
539+
540+
[[current]]
541+
== Current
542+
543+
Words.
544+
ASCIIDOC
545+
end
546+
file_context 'first subpage', 'otd.html' do
547+
let(:next_title) { "Current" }
548+
include_examples 'standard page', 'index', 'current'
549+
it 'contains a <link rel="canonical" ...> header tag' do
550+
expect(contents).to include <<~HTML
551+
<link rel="canonical" href="bazbar"/>
552+
HTML
553+
end
554+
end
555+
file_context 'second subpage', 'current.html' do
556+
let(:prev_title) { "Outdated" }
557+
include_examples 'standard page', 'otd', nil
558+
it 'does not contains a canonical header tag' do
559+
expect(contents).not_to include "canonical"
560+
expect(contents).not_to include "bazbar"
561+
end
562+
end
563+
end
532564
end
533565
context 'when chunk level is 2' do
534566
let(:convert_attributes) do

0 commit comments

Comments
 (0)