related articles converted to hugo templating
This commit is contained in:
parent
cfc7d25449
commit
0c80aa9ee5
3 changed files with 39 additions and 62 deletions
|
|
@ -1,40 +1,16 @@
|
|||
{{ if and (.Site.Params.enableRelatedArticles | default false) .Params.tags }}
|
||||
{{ $postsList :=
|
||||
where (
|
||||
where site.RegularPages "Params.tags" "!=" nil
|
||||
) "Params.tags" "intersect" .Params.tags
|
||||
}}
|
||||
<div class="relatedArticlesContainer">
|
||||
<hr />
|
||||
<h2>More posts like this</h2>
|
||||
<div id="relatedArticles"></div>
|
||||
<div class="postlist {{ if .Site.Params.gridView }}gridView{{ end }}">
|
||||
{{ range first (.Site.Params.relatedArticlesNum | default 2) $postsList }}
|
||||
{{- partial "article_card.html" . -}}
|
||||
{{ end }}
|
||||
</div>
|
||||
</div>
|
||||
{{- partial "js_paginator.html" . -}}
|
||||
<script>
|
||||
function hasAnyTags(articleTags, targetTags) {
|
||||
for (let atag of articleTags) {
|
||||
if (targetTags.includes(atag)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
function getRelatedArticles() {
|
||||
let currentTags = {{ .Params.tags }};
|
||||
let targetContainer = document.getElementById('relatedArticles');
|
||||
targetContainer.innerHTML = '';
|
||||
fetch('/search/index.json').then(res => res.json())
|
||||
.then(res => {
|
||||
let candidates = res.pages.filter(
|
||||
a => hasAnyTags(a.tags, currentTags) && a.link != "{{ .Permalink }}"
|
||||
);
|
||||
let targetNum = Math.min(
|
||||
{{ .Site.Params.relatedArticlesNum | default 2 }},
|
||||
candidates.length
|
||||
);
|
||||
if (candidates.length <= 0) {
|
||||
Array.from(
|
||||
document.getElementsByClassName('relatedArticlesContainer')
|
||||
).map(e => e.style.display = 'none');
|
||||
}
|
||||
for (let i=0; i<targetNum; i++) {
|
||||
targetContainer.innerHTML += renderSingleArticle(candidates[i]);
|
||||
}
|
||||
});
|
||||
}
|
||||
getRelatedArticles();
|
||||
</script>
|
||||
{{ end }}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue