Anchors for nested chapters

This commit is contained in:
Alex Kotov 2024-10-18 00:37:14 +04:00
parent 3ef42983d6
commit 71ec8dc7f6
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
6 changed files with 48 additions and 11 deletions

View file

@ -1,16 +1,22 @@
[ [
{ {
"slug": "1", "slug": "1-юникод",
"title": "Chapter 1", "title": "Chapter 1",
"chapters": [ "chapters": [
] ]
}, },
{ {
"slug": "2-юникод", "slug": "2",
"title": "Chapter 2", "title": "Chapter 2",
"chapters": [ "chapters": [
{
"slug": "2-nested",
"title": "Chapter 2.1",
"chapters": [
]
}
] ]
} }
] ]

View file

@ -27,6 +27,10 @@ Chapter 1 paragraph text
Chapter 2 paragraph text Chapter 2 paragraph text
### Chapter 2.1
Chapter 2.1 paragraph text
## Related articles ## Related articles

View file

@ -63,18 +63,24 @@ The Rty
</div> </div>
</div> </div>
</div> </div>
<h2 id="chapter-1">Chapter 1</h2> <h2 id="chapter-1-юникод">Chapter 1</h2>
<p> <p>
<span> <span>
Chapter 1 paragraph text Chapter 1 paragraph text
</span> </span>
</p> </p>
<h2 id="chapter-2-юникод">Chapter 2</h2> <h2 id="chapter-2">Chapter 2</h2>
<p> <p>
<span> <span>
Chapter 2 paragraph text Chapter 2 paragraph text
</span> </span>
</p> </p>
<h3 id="chapter-2-nested">Chapter 2.1</h3>
<p>
<span>
Chapter 2.1 paragraph text
</span>
</p>
<div> <div>
<hr/> <hr/>
<h2>Related articles</h2> <h2>Related articles</h2>

View file

@ -33,7 +33,7 @@ end
article.chapter '1', 'Chapter 1' do |chapter| article.chapter '1-юникод', 'Chapter 1' do |chapter|
chapter.paragraph do |paragraph| chapter.paragraph do |paragraph|
paragraph.text 'Chapter 1 paragraph text' paragraph.text 'Chapter 1 paragraph text'
end end
@ -41,10 +41,18 @@ end
article.chapter '2-юникод', 'Chapter 2' do |chapter| article.chapter '2', 'Chapter 2' do |chapter|
chapter.paragraph do |paragraph| chapter.paragraph do |paragraph|
paragraph.text 'Chapter 2 paragraph text' paragraph.text 'Chapter 2 paragraph text'
end end
chapter.chapter 'nested', 'Chapter 2.1' do |chapter|
chapter.paragraph do |paragraph|
paragraph.text 'Chapter 2.1 paragraph text'
end
end
end end

View file

@ -1,2 +1,2 @@
Annotation paragraph text Chapter 1 Chapter 1 paragraph text Chapter 2 Annotation paragraph text Chapter 1 Chapter 1 paragraph text Chapter 2
Chapter 2 paragraph text Chapter 2 paragraph text Chapter 2.1 Chapter 2.1 paragraph text

View file

@ -54,12 +54,27 @@ module Repubmark
def chapters def chapters
{ {
slug: @slug, slug: anchor,
title: @title, title: @title,
chapters: @chapters.map(&:chapters).freeze, chapters: @chapters.map(&:chapters).freeze,
}.freeze }.freeze
end end
##################
# Helper methods #
##################
def anchor
if parent.instance_of?(self.class) && @level > 2
"#{parent.anchor}-#@slug"
elsif (anchor_prefix =
String(config[:chapter_anchor_prefix]).strip).empty?
@slug
else
"#{anchor_prefix}-#@slug"
end.freeze
end
################### ###################
# Builder methods # # Builder methods #
################### ###################
@ -118,9 +133,7 @@ module Repubmark
end end
def build_title_html def build_title_html
anchor_prefix = String(config[:chapter_anchor_prefix]).strip %(<h#@level id="#{anchor}">#@title</h#@level>\n) if @level != 1
anchor = %( id="#{anchor_prefix}-#@slug") unless anchor_prefix.empty?
"<h#@level#{anchor}>#@title</h#@level>\n" if @level != 1
end end
def build_title_gemtext def build_title_gemtext