-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag.html
117 lines (100 loc) · 3.6 KB
/
tag.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
---
layout: document
title: Tag
permalink: /tag/
public: true
---
<div>
<ul id="tag-list" class="tag-list"></ul>
</div>
<div id="tag-collection"></div>
<script src="/js/axios.min.js"></script>
<script>
;(function() {
axios.get(`/data/tag_count.json`, {})
.then((resp) => {
if (resp == null || resp.data == null) {
return;
}
const tags = resp.data;
let template = '';
for (let i = 0; i < tags.length; i++) {
const tag = tags[i];
template += `
<li class="tag-item">
<a href="#${tag.name}" onclick="showTag('${tag.name}')">${tag.name}<sup>${tag.size}</sup></a>
</li>`
}
document.getElementById('tag-list').innerHTML = template;
showInitTag();
});
})();
/*
* 주어진 태그에 해당하는 모든 문서의 목록을 보여줍니다.
*/
function showTag(tagName) {
axios.get(`/data/tag/${tagName}.json`, {})
.then((resp) => createLinks(resp, tagName));
}
/*
* 주어진 태그에 해당하는 모든 문서의 링크를 만들어 줍니다.
*/
function createLinks(resp, tagName) {
if (resp.data == null) {
return;
}
const data = resp.data;
const documents = [];
let listItems = '';
for (let i = 0; i < data.length; i++) {
const idText = getDocumentElementId(tagName, data[i]);
listItems += `<li id="${idText}"></li>`;
}
const template = `
<h3>${tagName}</h3>
<ul class="post-list"> ${listItems} </ul>`;
document.getElementById('tag-collection').innerHTML = template;
for (let i = 0; i < data.length; i ++) {
setTimeout(() => insertDocumentLink(data[i], tagName), 0);
}
}
/*
* 문서 하나의 링크를 생성합니다.
*/
function insertDocumentLink(documentId, tagName) {
axios.get(`/data/metadata/${documentId}.json`, {})
.then((resp) => {
if (resp == null || resp.data == null) {
return;
}
const d = resp.data;
const updatedDate = d.updated.replace(/^(\d{4}-\d{2}-\d{2}).*$/, '$1')
const summaryText = (d.summary == null || /^\s*$/.test(d.summary))
? ('')
: ('-' + d.summary);
const subDocument = (d.children && d.children.length > 0)
? `- 서브 문서: ${d.children.length} 개`
: '';
const link = `
<a class="post-link" href="${d.url}">
<span>${d.title}</span>
<div class="post-meta" style="float: right;" title="${d.updated}">${updatedDate}</div>
<div class="post-excerpt">${summaryText}</div>
<div class="post-sub-document">${subDocument}</div>
</a>`;
const idText = getDocumentElementId(tagName, documentId);
document.getElementById(idText).innerHTML = link;
});
}
function getDocumentElementId(tagName, documentId) {
return `tag-${tagName}-${documentId}`;
}
function showInitTag() {
const url = window.location.href;
const req = /#([^\s]+)$/.exec(url);
if(Array.isArray(req)) {
let tagName = decodeURI(req.pop());
showTag(tagName);
}
}
</script>