Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve explicitly assigned header ids #614

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,10 @@ 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,18 @@ 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