Skip to content

Commit

Permalink
[minisite] breadcrumb support
Browse files Browse the repository at this point in the history
  • Loading branch information
rmannibucau committed Feb 16, 2024
1 parent 78a5bf3 commit 0c83da2
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
3 changes: 3 additions & 0 deletions _documentation/src/main/minisite/content/mojos.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@ Some specific attributes enables to customize the generation. Here is their list
* `minisite-highlightjs-skip` enables to not setup highlight.js for the page (useful with swagger-ui for example).
* `minisite-nav-prev-label`/`minisite-nav-next-label` enables to add a bottom page "previous"/"next" link to another page, this attribute defines its label.
* `minisite-nav-prev-link`/`minisite-nav-next-link` enables to add a bottom page "previous"/"next" link to another page, this attribute defines its link (label is required), if label is defined but not the link, the link is the label lowercased with iphens instead of spaces and html extension.
* `minisite-breadcrumb` enables to define a page breadcrumb (navigation tree) at the top of the content. Syntax is the following: `:minisite-breadcrumb: Home[#] > Another Page[link-to-another-page.html] > This page`. If there is a `[xxx]`, then `xxx` is considered as a link.

TIP: for a good documentation it is highly recommended to use nav and breadcrumb features.

=== Index generation

Expand Down
48 changes: 48 additions & 0 deletions minisite-core/src/main/java/io/yupiik/tools/minisite/MiniSite.java
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,54 @@ protected String getDefaultInterpolation(final String key, final Page page,
return readingTimeComputer.toReadingTime(page.content);
case "xyz": // passthrough
return "{{xyz}}";
case "breadcrumb":
String def = page.attributes.get("minisite-breadcrumb");
if (def == null || def.isBlank()) {
return "";
}
// parse: Home[#] > Another Page[link-to-another-page.html] > This page
final StringBuilder out = new StringBuilder("<nav aria-label=\"breadcrumb\" style=\"padding-left: 0;\">\n" +
" <ol class=\"breadcrumb\" style=\"margin-left: 0;background-color: unset;padding-left: 0;\">\n");
int from = 0;
def = def.replace("&gt;", ">");
while (from < def.length()) {
final int end = def.indexOf('>', from);
final String segment;
if (end < 0) {
segment = def.substring(from).strip();
from = def.length();
} else {
segment = def.substring(from, end).strip();
from = end + 1;
}

out.append(" <li class=\"breadcrumb-item")
.append(from == def.length() ? " active" : "").append("\"")
.append(from == def.length() ? " aria-current=\"page\"" : "")
.append(">");
final int link = segment.indexOf('[');
if (link < 0) {
out.append(segment);
} else {
final int endLink = segment.indexOf(']', link);
if (endLink < 0) {
out.append(segment);
} else {
final String text = segment.substring(0, link).strip();
out.append("<a href=\"").append(segment.substring(link + 1, endLink).strip()).append("\">")
.append("Home".equalsIgnoreCase(text) ? // home is worth an icon cause it is particular
"<svg viewBox=\"0 0 24 24\" " +
"style=\"height: 1.4rem;position: relative;top: 1px;vertical-align: top;width: 1.1rem;\">" +
"<path d=\"M10 19v-5h4v5c0 .55.45 1 1 1h3c.55 0 1-.45 1-1v-7h1.7c.46 0 .68-.57.33-.87L12.67 3.6c-.38-.34-.96-.34-1.34 0l-8.36 7.53c-.34.3-.13.87.33.87H5v7c0 .55.45 1 1 1h3c.55 0 1-.45 1-1z\"" +
" fill=\"currentColor\"></path>" +
"</svg>" :
text)
.append("</a>");
}
}
out.append("</li>\n");
}
return out.append(" </ol>\n</nav>").toString();
default:
try {
return findPageTemplate(getTemplatesDir(), key);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div id="{{{pageClass}}}-container" class="container page-content {{{pageClass}}}">
{{{title}}}
{{{breadcrumb}}}{{{title}}}
<div class="page-content-body">
{{{body}}}
{{{defaultEndOfContent?emptyIfMissing=true}}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@

@MiniSiteConfigurationBuilderProvider
class MiniSiteTest {
@Test
void breadcrumb(final MiniSiteConfigurationBuilderProvider.Asserts asserts,
final MiniSiteConfiguration.MiniSiteConfigurationBuilder builder) {
new MiniSite(builder.build()).run();
asserts.assertContains("page.html", "<nav aria-label=\"breadcrumb\" style=\"padding-left: 0;\">\n" +
" <ol class=\"breadcrumb\" style=\"margin-left: 0;background-color: unset;padding-left: 0;\">\n" +
" <li class=\"breadcrumb-item\"><a href=\"/\"><svg viewBox=\"0 0 24 24\" style=\"height: 1.4rem;position: relative;top: 1px;vertical-align: top;width: 1.1rem;\"><path d=\"M10 19v-5h4v5c0 .55.45 1 1 1h3c.55 0 1-.45 1-1v-7h1.7c.46 0 .68-.57.33-.87L12.67 3.6c-.38-.34-.96-.34-1.34 0l-8.36 7.53c-.34.3-.13.87.33.87H5v7c0 .55.45 1 1 1h3c.55 0 1-.45 1-1z\" fill=\"currentColor\"></path></svg></a></li>\n" +
" <li class=\"breadcrumb-item\"><a href=\"foo.html\">Foo</a></li>\n" +
" <li class=\"breadcrumb-item active\" aria-current=\"page\">Page 1</li>\n" +
" </ol>\n" +
"</nav>");
}

@Test
void footerNav(final MiniSiteConfigurationBuilderProvider.Asserts asserts,
final MiniSiteConfiguration.MiniSiteConfigurationBuilder builder) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
= Page 1
:minisite-breadcrumb: Home[/] > Foo[foo.html] > Page 1

Content.

0 comments on commit 0c83da2

Please sign in to comment.