Skip to content

Commit

Permalink
♻️ breadcrumbs: Use Hugo-native script instead of JavaScript (#138)
Browse files Browse the repository at this point in the history
This commit refactors the existing breadcrumb bar to use a Hugo-native way of constructing the path. It also removes the duplicate "Home" and site title options, so the view is cleaner. Related to PR #133.
  • Loading branch information
Neha9849 authored Jun 30, 2022
1 parent 860384d commit 685ec61
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
4 changes: 4 additions & 0 deletions assets/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -1263,4 +1263,8 @@ pre code::-webkit-scrollbar-thumb {

footer a:hover{
color: var(--text-color-dark) !important;
}

.breadcrumbLink{
text-transform: capitalize;
}
29 changes: 12 additions & 17 deletions layouts/partials/breadcrumb.html
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
<script language="JavaScript">

String.prototype.capitalizeFirstLetter = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
}

var path = "<a style='margin-left:2%' href='{{ .Site.BaseURL}}'>Home</a>";
var href = document.location.href;
var s = href.split("/");
for (var i=3;i<(s.length-2);i++) {
path+=" &gt; <a href=\""+href.substring(0,href.indexOf("/"+s[i])+s[i].length+1)+"/\">"+s[i].capitalizeFirstLetter()+"</a> ";
}
i=s.length-1;
path+="<a href=\""+href.substring(0,href.indexOf(s[i])+s[i].length)+"\">"+s[i]+"</a>";
path+=" &gt; <b>{{.Title}}</b>";
document.writeln(path);
</script>
{{ $url := replace .Permalink ( printf "%s" .Site.BaseURL) "" }}
{{ $.Scratch.Add "path" .Site.BaseURL }}
<div class="breadcrumbs">
<a href="{{ .Site.BaseURL}}">Home</a>
{{ range $index, $element := split $url "/" }}
{{ $.Scratch.Add "path" $element }}
{{ if ne $element "" }}
&gt; <a href="{{ $.Scratch.Get "path" }}" class="breadcrumbLink">{{ . }}</a>
{{ $.Scratch.Add "path" "/" }}
{{ end }}
{{ end }}
</div>

0 comments on commit 685ec61

Please sign in to comment.