-
Notifications
You must be signed in to change notification settings - Fork 1
/
blogSort.html
61 lines (53 loc) · 1.54 KB
/
blogSort.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>YML Blog Sorter</title>
</head>
<body>
<h1>YML Blog Sorter</h1>
<label for="input">Input:</label>
<br>
<textarea id="input" rows="10" cols="80"></textarea>
<br>
<button onclick="sortBlogs()">Sort Blogs</button>
<br>
<label for="output">Output:</label>
<br>
<textarea id="output" rows="10" cols="80" readonly></textarea>
<script>
function parseYML(ymlText) {
const lines = ymlText.trim().split("\n");
const blogs = [];
let currentBlog = null;
for (const line of lines) {
if (line.startsWith('- local:')) {
if (currentBlog) {
blogs.push(currentBlog);
}
currentBlog = { content: [] };
currentBlog.content.push(line);
} else if (currentBlog) {
currentBlog.content.push(line);
if (line.startsWith(' date:')) {
const dateStr = line.split(' date:')[1].trim();
currentBlog.date = new Date(dateStr);
}
}
}
if (currentBlog) {
blogs.push(currentBlog);
}
return blogs;
}
function sortBlogs() {
const input = document.getElementById('input').value;
const blogs = parseYML(input);
blogs.sort((a, b) => a.date - b.date);
const sortedOutput = blogs.map(blog => blog.content.join('\n')).join('\n');
document.getElementById('output').value = sortedOutput;
}
</script>
</body>
</html>