Skip to content

Commit 034580a

Browse files
committed
fix: anchor being overwritten when multiple headers have the same name
Signed-off-by: Lewis Birks <[email protected]>
1 parent 5e345b5 commit 034580a

File tree

2 files changed

+35
-39
lines changed

2 files changed

+35
-39
lines changed

docs/asciidoc/packaging/packaging.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ application.
99

1010
[TIP]
1111
====
12-
The https://jooby.io/#getting-started[jooby-cli] takes care of configures everything for single jar
12+
The link:/#getting-started[jooby-cli] takes care of configures everything for single jar
1313
distribution. Next example shows how to do it in case you created your application manually.
1414
====
1515

docs/src/main/java/io/jooby/adoc/DocGenerator.java

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import static io.jooby.SneakyThrows.throwingConsumer;
99
import static io.jooby.SneakyThrows.throwingFunction;
10+
import static java.util.function.Predicate.not;
1011

1112
import java.io.File;
1213
import java.io.IOException;
@@ -23,7 +24,6 @@
2324
import java.util.List;
2425
import java.util.UUID;
2526
import java.util.concurrent.atomic.AtomicInteger;
26-
import java.util.stream.Collectors;
2727
import java.util.stream.Stream;
2828

2929
import org.apache.commons.io.FileUtils;
@@ -37,7 +37,6 @@
3737
import org.jsoup.nodes.Document;
3838
import org.jsoup.nodes.Element;
3939

40-
import me.tongfei.progressbar.ProgressBar;
4140
import me.tongfei.progressbar.ProgressBarBuilder;
4241
import me.tongfei.progressbar.ProgressBarStyle;
4342

@@ -324,7 +323,7 @@ private static String toJavaName(String tagName) {
324323
private static String document(Path index) {
325324
try {
326325
Document doc = Jsoup.parse(index.toFile(), "UTF-8");
327-
tocItems(doc);
326+
headerIds(doc);
328327
languageTab(doc);
329328
clipboard(doc);
330329
externalLink(doc);
@@ -381,45 +380,43 @@ private static void languageTab(Document doc) {
381380
primaryContent.appendTo(primary);
382381
secondaryContent.appendTo(primary);
383382
secondaryContent.addClass("hidden").addClass("option-2");
384-
;
385383
}
386384
}
387385

388-
private static void tocItems(Document doc) {
389-
tocItems(doc, 2);
390-
tocItems(doc, 3);
391-
tocItems(doc, 4);
386+
private static void headerIds(Document doc) {
387+
headerIds(doc, 2);
388+
headerIds(doc, 3);
389+
headerIds(doc, 4);
390+
headerIds(doc, 5);
392391
}
393392

394-
private static void tocItems(Document doc, int level) {
395-
doc.select("h" + level)
396-
.forEach(
397-
h -> {
398-
if (!h.hasClass("discrete")) {
399-
String id = h.attr("id");
400-
LinkedHashSet<String> name = new LinkedHashSet<>();
401-
int parent = level - 1;
402-
Element p = h.parents().select("h" + parent).first();
403-
if (p != null && !p.hasClass("discrete")) {
404-
String parentId = p.attr("id");
405-
if (parentId != null && parentId.length() > 0) {
406-
name.add(parentId);
407-
}
408-
}
409-
name.add(id.replaceAll("([a-zA-Z0-9-]+)-\\d+$", "$1"));
410-
String newId = name.stream().collect(Collectors.joining("-"));
411-
if (!id.equals(newId)) {
412-
h.attr("id", newId);
413-
doc.select("a")
414-
.forEach(
415-
a -> {
416-
if (a.attr("href").equals("#" + id) && a.attr("class").length() > 0) {
417-
a.attr("href", "#" + newId);
418-
}
419-
});
420-
}
393+
private static void headerIds(Document doc, int level) {
394+
doc.select("h" + level).stream()
395+
.filter(not(DocGenerator::isDiscrete))
396+
.forEach(h -> {
397+
String id = h.attr("id");
398+
LinkedHashSet<String> name = new LinkedHashSet<>();
399+
int parent = level - 1;
400+
Element p = h.parents().select("h" + parent).first();
401+
if (p != null && !isDiscrete(p)) {
402+
String parentId = p.attr("id");
403+
if (!parentId.isEmpty()) {
404+
name.add(parentId);
421405
}
422-
});
406+
}
407+
name.add(id.replaceAll("([a-zA-Z0-9-]+)-\\d+$", "$1"));
408+
String newId = String.join("-", name);
409+
if (!id.equals(newId)) {
410+
h.attr("id", newId);
411+
h.select("a").stream()
412+
.filter(a -> a.attr("href").equals("#" + id) && !a.attr("class").isEmpty())
413+
.forEach(a -> a.attr("href", "#" + newId));
414+
}
415+
});
416+
}
417+
418+
private static boolean isDiscrete(Element p) {
419+
return p.hasClass("discrete");
423420
}
424421

425422
private static void clipboard(Document doc) {
@@ -449,8 +446,7 @@ public static Path basedir() {
449446
public static String version() {
450447
try {
451448
Document doc = Jsoup.parse(basedir().getParent().resolve("pom.xml").toFile(), "utf-8");
452-
String version = doc.selectFirst("version").text().trim();
453-
return version;
449+
return doc.selectFirst("version").text().trim();
454450
} catch (IOException x) {
455451
throw new IllegalStateException(x);
456452
}

0 commit comments

Comments
 (0)