Skip to content

Commit

Permalink
Preserve explicitly assigned header ids
Browse files Browse the repository at this point in the history
  • Loading branch information
milessabin committed May 21, 2024
1 parent b48cf4b commit 1fe750c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private[link] class DocumentTargets(document: Document, slugBuilder: String => S
linkDefinitionResolver(selector, ld.target, ld.title)

case h @ DecoratedHeader(deco, _, _, _) =>
val selector = TargetIdSelector(slugBuilder(h.extractText))
val selector = TargetIdSelector(slugBuilder(h.options.id.getOrElse(h.extractText)))
val level = levels.levelFor(deco)
val finalHeader = TargetReplacer.lift { case DecoratedHeader(_, content, _, opt) =>
Header(level, content, opt + Id(selector.id))
Expand All @@ -140,7 +140,7 @@ private[link] class DocumentTargets(document: Document, slugBuilder: String => S
)

case h @ Header(level, _, _) =>
val selector = TargetIdSelector(slugBuilder(h.extractText))
val selector = TargetIdSelector(slugBuilder(h.options.id.getOrElse(h.extractText)))
TargetResolver.create(
selector,
internalResolver(selector),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ private[laika] class LinkResolver(root: DocumentTreeRoot, slugBuilder: String =>
case FootnoteLabel.Autosymbol => replaceBlock(f, AutosymbolSelector)
}
case c: Citation => replaceBlock(c, TargetIdSelector(slugBuilder(c.label)))
case h: DecoratedHeader => replaceBlock(h, TargetIdSelector(slugBuilder(h.extractText)))
case h: Header => replaceBlock(h, TargetIdSelector(slugBuilder(h.extractText)))
case h: DecoratedHeader => replaceBlock(h, TargetIdSelector(slugBuilder(h.options.id.getOrElse(h.extractText))))
case h: Header => replaceBlock(h, TargetIdSelector(slugBuilder(h.options.id.getOrElse(h.extractText))))

case d: BlockDirectives.DirectiveInstance if d.directive.exists(_.name == "fragment") =>
RewriteAction.Replace(d.resolve(cursor))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,16 @@ class RewriteRulesSpec extends FunSuite with ParagraphCompanionShortcuts with Te
runRootWithoutTitles(rootElem, expected)
}

test(
"decorated headers - respect explicitly assigned ids"
) {
val rootElem = RootElement(
DecoratedHeader(Underline('#'), List(Text("Title")), SourceCursor.Generated).withId("explicit")
)
val expected = RootElement(
Section(Header(1, List(Text("Title")), Id("explicit") + Style.section), Nil)
)
runRootWithoutTitles(rootElem, expected)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,12 @@ class RewriteRulesSpec extends FunSuite with ParagraphCompanionShortcuts with Te
runRoot(rootElem, expected)
}

test("header ids - respect explicitly assigned ids") {
val rootElem = RootElement(Header(1, List(Text("Header"))).withId("explicit"))
val expected = RootElement(Title(List(Text("Header")), Id("explicit") + Style.title))
runRoot(rootElem, expected)
}

test("duplicate ids - append auto-increment numbers") {
val header = Header(1, "Header")
val rootElem = RootElement(header, header)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class SectionNumberSpec extends FunSuite with DocumentTreeAssertions {
def isIncluded(level: Int): Boolean = depth.forall(_ >= level)

def header(level: Int, title: Int, style: String = "section"): Header =
Header(level, List(Text(s"Title $title")), Id(s"title$title") + Styles(style))
Header(level, List(Text(s"Title $title")), Id(s"title-$title") + Styles(style))

def tree(content: RootElement): DocumentTree = {
val autonumberConfig = ConfigParser
Expand Down

0 comments on commit 1fe750c

Please sign in to comment.