Last active
December 29, 2015 01:49
-
-
Save gh640/7596111 to your computer and use it in GitHub Desktop.
JavaScript archive page generator code with categories in Blogger.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="archivePage"> | |
</div> | |
<!-- for archive page --> | |
<script> | |
<!-- | |
jQuery(function(){ | |
// 挿入対象DIVを取得 | |
// なければ終了 | |
var archive = jQuery('#archivePage'); | |
if(!archive){ | |
return; | |
} | |
archive.text('loading'); | |
// アーカイブJSONを取得して処理 | |
var url = '/feeds/posts/summary?alt=json&max-results=300'; | |
jQuery.getJSON(url, function(json){ | |
// 01 タグ一覧を取得 | |
// なければ終了 | |
// OUT: categories list | |
// json.feed.category: タグ一覧 | |
var categories = []; | |
var c = json.feed.category; | |
for(var i in c){ | |
categories.push(c[i].term); | |
} | |
if(!categories){ | |
return; | |
} | |
// 02 タグ別の記事リストを作成 | |
// 記事リストを走査し、カテゴリが一致すればタイトルとURLをリストに追加する | |
// OUT: posts list of list (posts[i]: カテゴリ[i]の記事リスト) | |
// json.feed.entry: 記事一覧 | |
// json.feed.entry.title.$t: タイトル | |
// json.feed.entry.link[4].href: URL | |
var posts = new Array(categories.length); | |
var e = json.feed.entry; | |
for(var i in e){ | |
var c = e[i].category; | |
if(!c){ | |
continue; | |
} | |
for(var j in c){ | |
for(var k in categories){ | |
if(c[j].term == categories[k]){ | |
if(typeof posts[k] === 'undefined'){ | |
posts[k] = [[e[i].title.$t, e[i].link[4].href]]; | |
}else{ | |
posts[k].push([e[i].title.$t, e[i].link[4].href]); | |
} | |
} | |
} | |
} | |
} | |
// 03 リストに整形して挿入対象DIVに入れる | |
// div#archivePage > li(categories) > li(posts) | |
var ulcat = jQuery('<ul>'); | |
for(var i in categories){ | |
var licat = jQuery('<li>'); | |
var acat = jQuery('<a>').text(categories[i]); | |
acat.attr('href', '/search/label/' + categories[i]); | |
var ulpost = jQuery('<ul>'); | |
for(var j in posts[i]){ | |
var lipost = jQuery('<li>'); | |
var apost = jQuery('<a>').text(posts[i][j][0]); | |
apost.attr('href', posts[i][j][1]); | |
apost.appendTo(lipost); | |
lipost.appendTo(ulpost); | |
} | |
licat.append(acat, ulpost); | |
ulcat.append(licat); | |
} | |
archive.html(ulcat); | |
}); | |
}); | |
--> | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment