diff options
Diffstat (limited to '_layouts')
-rw-r--r-- | _layouts/default.html | 4 | ||||
-rw-r--r-- | _layouts/post.html | 26 |
2 files changed, 27 insertions, 3 deletions
diff --git a/_layouts/default.html b/_layouts/default.html index 509ef6a..20595ac 100644 --- a/_layouts/default.html +++ b/_layouts/default.html @@ -28,11 +28,11 @@ gtag('config', '{{ site.google_analytics }}'); var lastUpdated = new Date("{{ site.time | date: "%FT%T%z" }}"); function getSearchJSON(callback) { - var searchData = JSON.parse(localStorage.getItem(lastUpdated)); + var searchData = JSON.parse(localStorage.getItem("blog_" + lastUpdated.valueOf())); if (!searchData) { localStorage.clear(); $.getJSON("/search.json", function (data) { - localStorage.setItem(lastUpdated, JSON.stringify(data)); + localStorage.setItem("blog_" + lastUpdated.valueOf(), JSON.stringify(data)); callback(data); }); } else { diff --git a/_layouts/post.html b/_layouts/post.html index 59835ee..2ad1fdd 100644 --- a/_layouts/post.html +++ b/_layouts/post.html @@ -82,7 +82,31 @@ layout: default {% endif %} <br /> <br /> -<p id="suggest-container"><button onclick="getSuggestBlog('{{ page.url }}')">查看推荐文章</button></p> +<p id="suggest-container"></p> +<script> +var blogurl = "{{ page.url }}"; +var suggest = $("#suggest-container")[0]; +suggest.innerHTML = "Loading..."; +$.get(BlogAPI + "/suggest?id=" + blogurl + "&update=" + lastUpdated.valueOf(), function (data) { + if (data.length) { + getSearchJSON(function (search) { + suggest.innerHTML = '<b>推荐文章</b><hr style="margin: 0 0 5px"/>'; + const searchMap = new Map(search.map(item => [item.url, item])); + const merged = data.map(suggestObj => { + const searchObj = searchMap.get(suggestObj.id); + return searchObj ? { ...searchObj } : null; + }); + merged.forEach(element => { + if (element) { + suggest.innerHTML += "<a href=" + element.url + ">" + element.title + "</a> - " + element.date + "<br />"; + } + }); + }); + } else { + suggest.innerHTML = "暂无推荐文章……"; + } +}); +</script> <div class="pagination"> {% if page.previous.url %} <span class="prev"> |