forked from justjavac/weibo-trending-hot-search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
42 lines (38 loc) · 1.05 KB
/
utils.ts
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
import type { Word } from "./types.ts";
/**
* 合并两次热门话题并根据**内容**去重,新的覆盖旧的
*
* via https://github.com/justjavac/weibo-trending-hot-search/issues/11#issuecomment-1428187183
*/
export function mergeWords(
words: Word[],
another: Word[],
): Word[] {
const obj: Record<string, string> = {};
for (const w of words.concat(another)) {
obj[w.title] = w.url;
}
return Object.entries(obj).map(([title, url]) => ({
url,
title,
}));
}
export async function createReadme(words: Word[]): Promise<string> {
const readme = await Deno.readTextFile("./README.md");
return readme.replace(/<!-- BEGIN -->[\W\w]*<!-- END -->/, createList(words));
}
export function createList(words: Word[]): string {
return `<!-- BEGIN -->
<!-- 最后更新时间 ${Date()} -->
${
words.map((x) => `1. [${x.title}](https://s.weibo.com/${x.url})`)
.join("\n")
}
<!-- END -->`;
}
export function createArchive(words: Word[], date: string): string {
return `# ${date}\n
共 ${words.length} 条\n
${createList(words)}
`;
}