-
Notifications
You must be signed in to change notification settings - Fork 35
/
utils.ts
35 lines (31 loc) · 1.03 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
import type { SearchWord } from "./types.ts";
/** 合并两次关键词并根据 display_query 去重 */
export function mergeWords(
words: SearchWord[],
another: SearchWord[],
): SearchWord[] {
const obj: Record<string, string> = {};
for (const w of words.concat(another)) {
obj[w.display_query] = w.query;
}
return Object.entries(obj).map(([display_query, query]) => ({
query,
display_query,
}));
}
export async function createReadme(words: SearchWord[]): Promise<string> {
const readme = await Deno.readTextFile("./README.md");
return readme.replace(/<!-- BEGIN -->[\W\w]*<!-- END -->/, createList(words));
}
export function createList(words: SearchWord[]): string {
return `<!-- BEGIN -->
<!-- 最后更新时间 ${Date()} -->
${words.map((x) => `1. [${x.display_query}](https://www.zhihu.com/search?q=${encodeURIComponent(x.query)})`).join("\n")}
<!-- END -->`;
}
export function createArchive(words: SearchWord[], date: string): string {
return `# ${date}\n
共 ${words.length} 条\n
${createList(words)}
`;
}