From 3dceb80bf7f3cf4b7c27635d3757bef68412c54a Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Mon, 10 Dec 2018 21:52:36 +0100 Subject: [PATCH] #702 - Improved TOC rendering. We now override the toc.js originally provided by Spring Docs to customize the collapseDepth property and also jump to the #reference anchor if no anchor is set. This should be improved to rather provide a custom docinfo.html that includes an additional JavaScript file to tweak the tocbot settings and anchor defaulting. --- parent/pom.xml | 37 ++++---- resources/src/main/resources/js/toc.js | 114 +++++++++++++++++++++++++ 2 files changed, 136 insertions(+), 15 deletions(-) create mode 100644 resources/src/main/resources/js/toc.js diff --git a/parent/pom.xml b/parent/pom.xml index 0c20232c..19b32772 100644 --- a/parent/pom.xml +++ b/parent/pom.xml @@ -281,21 +281,6 @@ org.apache.maven.plugins maven-dependency-plugin - - unpack-shared-resources - - unpack-dependencies - - generate-resources - - ${project.groupId} - spring-data-build-resources - zip - true - ${shared.resources} - - - unpack-doc-resources @@ -310,6 +295,20 @@ ${generated-asciidoc-sources.directory} + + unpack-shared-resources + + unpack-dependencies + + generate-resources + + ${project.groupId} + spring-data-build-resources + zip + true + ${shared.resources} + + @@ -446,6 +445,14 @@ + + + + + + + + diff --git a/resources/src/main/resources/js/toc.js b/resources/src/main/resources/js/toc.js new file mode 100644 index 00000000..cb1deaf2 --- /dev/null +++ b/resources/src/main/resources/js/toc.js @@ -0,0 +1,114 @@ +var toctitle = document.getElementById('toctitle'); +var path = window.location.pathname; +if (toctitle != null) { + var oldtoc = toctitle.nextElementSibling; + var newtoc = document.createElement('div'); + newtoc.setAttribute('id', 'tocbot'); + newtoc.setAttribute('class', 'js-toc desktop-toc'); + oldtoc.setAttribute('class', 'mobile-toc'); + oldtoc.parentNode.appendChild(newtoc); + tocbot.init({ + contentSelector: '#content', + headingSelector: 'h1, h2, h3, h4, h5', + positionFixedSelector: 'body', + fixedSidebarOffset: 90, + smoothScroll: false, + // Spring Data specific + collapseDepth: 2 + }); + if (!path.endsWith("index.html") && !path.endsWith("/")) { + var link = document.createElement("a"); + link.setAttribute("href", "index.html"); + link.innerHTML = " Back to index"; + var block = document.createElement("div"); + block.setAttribute('class', 'back-action'); + block.appendChild(link); + var toc = document.getElementById('toc'); + var next = document.getElementById('toctitle').nextElementSibling; + toc.insertBefore(block, next); + } +} + +var headerHtml = '
\n' + + '

\n' + + '\n' + + '\n' + + '\n' + + '\n' + + '\n' + + '\n' + + '\n' + + '\n' + + '\n' + + '\n' + + '\n' + + '\n' + + '\n' + + '\n' + + '\n' + + '\n' + + '\n' + + '\n' + + '\n' + + '\n' + + '\n' + + '\n' + + '\n' + + '\n' + + '\n' + + '\n' + + '\n' + + '\n' + + '\n' + + '\n' + + '\n' + + '\n' + + '\n' + + '\n' + + '\n' + + '\n' + + '\n' + + '\n' + + '\n' + + '\n' + + '

\n' + + '
'; + +var header = document.createElement("div"); +header.innerHTML = headerHtml; +document.body.insertBefore(header, document.body.firstChild); + +// Spring Data specifics: default section is the reference one +if (window.location.hash == '') { + window.location.hash = '#reference' +}