Skip to content

Commit

Permalink
Merge pull request #673 from typelevel/core/flattening-section-builder
Browse files Browse the repository at this point in the history
SectionBuilder flattens BlockSequence without id or styles
  • Loading branch information
jenshalm authored Jan 1, 2025
2 parents 53f313e + 777128f commit d30a695
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
15 changes: 11 additions & 4 deletions core/shared/src/main/scala/laika/internal/nav/SectionBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,28 @@ private[laika] object SectionBuilder extends RewriteRulesBuilder {

def buildSections(document: RootElement): RootElement = {

def flatten(blocks: Seq[Block]): Seq[Block] = blocks.flatMap {
case seq: BlockSequence if seq.options.styles.isEmpty && !seq.hasId => flatten(seq.content)
case b: Block => Seq(b)
}

val flattenedDocument = flatten(document.content)

val docPosition = if (autonumberConfig.documents) position else TreePosition.root

val (titleSection, rest) =
if (!extractTitle) (Nil, document.content)
if (!extractTitle) (Nil, flattenedDocument)
else {

val title = document.content.collectFirst { case h: Header =>
val title = flattenedDocument.collectFirst { case h: Header =>
if (autonumberConfig.documents)
Title(addNumber(h.content, docPosition), h.options + Style.title)
else Title(h.content, h.options + Style.title)
}

title.fold((document.content, Seq.empty[Block])) { titleBlock =>
title.fold((flattenedDocument, Seq.empty[Block])) { titleBlock =>
val (preface, rest) =
document.content.splitAt(document.content.indexWhere(_.isInstanceOf[Header]))
flattenedDocument.splitAt(flattenedDocument.indexWhere(_.isInstanceOf[Header]))
(preface :+ titleBlock, rest.tail)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class StandardDirectiveSpec extends FunSuite
|@:todo(FIXME LATER)
|
|bb""".stripMargin
run(input, p("aa"), BlockSequence(Nil), p("bb"))
run(input, p("aa"), p("bb"))
}

test("todo directive as span") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class StandardBlockDirectivesSpec extends FunSuite with ParagraphCompanionShortc
| 1st Para
|
| 2nd Para""".stripMargin
run(input, BlockSequence(simplePars))
run(input, simplePars *)
}

test("container - sequence of two paragraphs with two custom styles") {
Expand Down

0 comments on commit d30a695

Please sign in to comment.