Skip to content
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
@@ -0,0 +1 @@
* Add: generate TOC as simplified server side rendering for every page for crawlers.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.testingisdocumenting.znai.html;

import org.testingisdocumenting.znai.parser.PageSectionIdTitle;
import org.testingisdocumenting.znai.search.PageLocalSearchEntries;
import org.testingisdocumenting.znai.search.PageSearchEntry;
import org.testingisdocumenting.znai.structure.TableOfContents;
Expand All @@ -29,10 +28,11 @@
public class ServerSideSimplifiedRenderer {
static final String LOADING_INDICATOR = ResourceUtils.textContent("template/initial-page-loading.html");

public static String renderToc(TableOfContents toc) {
public static String renderToc(TableOfContents toc, String docId) {
return section(
toc.getTocItems().stream()
.map(ServerSideSimplifiedRenderer::renderTocLink)
.filter((tocItem -> !tocItem.isIndex()))
.map((tocItem) -> ServerSideSimplifiedRenderer.renderTocLink(tocItem, docId))
.collect(Collectors.joining("\n")));
}

Expand All @@ -43,22 +43,11 @@ public static String renderPageTextContent(PageLocalSearchEntries pageSearchEntr
.collect(Collectors.joining("\n")));
}

private static String renderTocLink(TocItem tocItem) {
String rootLink = aHref(
tocItem.getDirName() + "/" + tocItem.getFileNameWithoutExtension() + "/",
private static String renderTocLink(TocItem tocItem, String docId) {
String rootLink = aHref("/" + docId + "/" + tocItem.getDirName() + "/" + tocItem.getFileNameWithoutExtension() + "/",
tocItem.getPageTitle());

String subLinks = tocItem.getPageSectionIdTitles().stream()
.map(section -> renderTocSubLink(tocItem, section))
.collect(Collectors.joining(""));

return article(rootLink + subLinks);
}

private static String renderTocSubLink(TocItem tocItem, PageSectionIdTitle pageSectionIdTitle) {
return aHref(
tocItem.getDirName() + "/" + tocItem.getFileNameWithoutExtension() + "/#" + pageSectionIdTitle.getId(),
tocItem.getPageTitle() + " " + pageSectionIdTitle.getTitle());
return article(rootLink);
}

private static String renderPageEntry(PageSearchEntry entry) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -708,12 +708,8 @@ private HtmlPageAndPageProps createRedirectingToFirstTocItemHtmlPageAndProps() {
private RenderSupplier createServerSideRenderer(TocItem tocItem) {
PageLocalSearchEntries pageSearchEntries = localSearchEntries.searchEntriesByTocItem(tocItem);

if (tocItem.isIndex()) {
return () -> ServerSideSimplifiedRenderer.renderPageTextContent(pageSearchEntries) +
ServerSideSimplifiedRenderer.renderToc(toc);
}

return () -> ServerSideSimplifiedRenderer.renderPageTextContent(pageSearchEntries);
return () -> ServerSideSimplifiedRenderer.renderPageTextContent(pageSearchEntries) +
ServerSideSimplifiedRenderer.renderToc(toc, docMeta.getId());
}

private void deployAuxiliaryFiles() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,18 @@ class ServerSideSimplifiedRendererTest {

@Test
void "should render TOC with page sections"() {
ServerSideSimplifiedRenderer.renderToc(toc).should ==
ServerSideSimplifiedRenderer.renderToc(toc, "my/doc").should ==
'<section style="max-width: 640px; margin-left: auto; margin-right: auto;">\n' +
'<article>\n' +
'<a href="chapter-a/page-one/">Page One</a>\n' +
'<a href="chapter-a/page-one/#ps0">Page One PS0</a>\n' +
'<a href="/my/doc/chapter-a/page-one/">Page One</a>\n' +
'</article>\n' +
'\n' +
'<article>\n' +
'<a href="chapter-a/page-two/">Page Two</a>\n' +
'<a href="chapter-a/page-two/#ps1">Page Two PS1</a>\n' +
'<a href="chapter-a/page-two/#ps2">Page Two PS2</a>\n' +
'<a href="/my/doc/chapter-a/page-two/">Page Two</a>\n' +
'</article>\n' +
'\n' +
'<article>\n' +
'<a href="chapter-b/page-one/">Page One</a>\n' +
'<a href="chapter-b/page-one/#ps3">Page One PS3</a>\n' +
'<a href="chapter-b/page-one/#ps4">Page One PS4</a>\n' +
'<a href="/my/doc/chapter-b/page-one/">Page One</a>\n' +
'</article>\n' +
'</section>\n'
}
Expand Down