Skip to content

Commit d1877e4

Browse files
Florian3ktgodzik
authored andcommitted
Scaladoc: fix generation of unique header ids (scala#22779)
This PR fixes the generation of unique header id's. Previously, the id's were deduplicated globally, now they are deduplicated only within one page. Custom logic for that was removed, we now rely on `HeaderIdGenerator` from flexmark. [Cherry-picked 8734fbd]
1 parent 3a8f0f9 commit d1877e4

File tree

1 file changed

+4
-9
lines changed

1 file changed

+4
-9
lines changed

scaladoc/src/dotty/tools/scaladoc/tasty/comments/markdown/SectionRenderingExtension.scala

+4-9
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,17 @@ object SectionRenderingExtension extends HtmlRenderer.HtmlRendererExtension:
2626
def rendererOptions(opt: MutableDataHolder): Unit = ()
2727

2828
case class AnchorLink(link: String) extends BlankLine(BasedSequence.EmptyBasedSequence())
29-
object SectionHandler extends CustomNodeRenderer[Section]:
30-
val repeatedIds: mutable.Map[(NodeRendererContext, String), Int] = mutable.Map()
29+
class SectionHandler extends CustomNodeRenderer[Section]:
3130
val idGenerator = new HeaderIdGenerator.Factory().create()
31+
idGenerator.setResolveDupes(true)
3232
override def render(node: Section, c: NodeRendererContext, html: HtmlWriter): Unit =
3333
val Section(header, body) = node
34-
val headerText = header.getText.toString
35-
val idSuffix = repeatedIds.getOrElseUpdate((c, headerText), 0)
36-
val ifSuffixStr = if(idSuffix == 0) then "" else idSuffix.toString
37-
repeatedIds.update((c, headerText), idSuffix + 1)
3834

3935
/* #19524 flexmark's `HeaderIdGenerator` does not appear to be thread-safe,
4036
* so we protect its usage with a full `synchronize`.
4137
*/
4238
val id = idGenerator.synchronized {
43-
idGenerator.getId(headerText + ifSuffixStr)
39+
idGenerator.getId(header.getText)
4440
}
4541

4642
val anchor = AnchorLink(s"#$id")
@@ -68,13 +64,12 @@ object SectionRenderingExtension extends HtmlRenderer.HtmlRendererExtension:
6864
object Render extends NodeRenderer:
6965
override def getNodeRenderingHandlers: JSet[NodeRenderingHandler[_]] =
7066
JSet(
71-
new NodeRenderingHandler(classOf[Section], SectionHandler),
67+
new NodeRenderingHandler(classOf[Section], new SectionHandler),
7268
new NodeRenderingHandler(classOf[AnchorLink], AnchorLinkHandler)
7369
)
7470

7571
object Factory extends NodeRendererFactory:
7672
override def apply(options: DataHolder): NodeRenderer = Render
7773

78-
7974
def extend(htmlRendererBuilder: HtmlRenderer.Builder, tpe: String): Unit =
8075
htmlRendererBuilder.nodeRendererFactory(Factory)

0 commit comments

Comments
 (0)