Skip to content

Commit

Permalink
chore: fix editorconfig and format scripts
Browse files Browse the repository at this point in the history
Signed-off-by: Frost Ming <[email protected]>
  • Loading branch information
frostming committed Nov 27, 2024
1 parent 083aecb commit 34241ef
Show file tree
Hide file tree
Showing 7 changed files with 519 additions and 521 deletions.
6 changes: 3 additions & 3 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
root = true

[*]
indent_style = tab
indent_style = space
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.yml]
indent_style = space
[*.{yml,ts,js}]
indent_size = 2
76 changes: 38 additions & 38 deletions src/answer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ import { Question } from "./question";
import { fixImagesAndLinks, createTemplate, extractReference, FetchError } from "./lib";

export type Answer = {
content: string;
excerpt: string;
author: {
name: string;
url: string;
headline: string;
avatar_url: string;
};
voteup_count: number;
comment_count: number;
question: Question;
created_time: number;
content: string;
excerpt: string;
author: {
name: string;
url: string;
headline: string;
avatar_url: string;
};
voteup_count: number;
comment_count: number;
question: Question;
created_time: number;
}

const template = createTemplate`
Expand Down Expand Up @@ -97,31 +97,31 @@ const questionTemplate = createTemplate`
`;

export async function answer(id: string, redirect: boolean, env: Env): Promise<string> {
const url = `https://api.zhihu.com/v4/answers/${id}?include=content%2Cexcerpt%2Cauthor%2Cvoteup_count%2Ccomment_count%2Cquestion%2Ccreated_time%2Cquestion.detail`;
const response = await fetch(url);
if (!response.ok) {
throw new FetchError(response.statusText, response);
}
const data = await response.json<Answer>();
const createdTime = new Date(data.created_time * 1000);
const url = `https://api.zhihu.com/v4/answers/${id}?include=content%2Cexcerpt%2Cauthor%2Cvoteup_count%2Ccomment_count%2Cquestion%2Ccreated_time%2Cquestion.detail`;
const response = await fetch(url);
if (!response.ok) {
throw new FetchError(response.statusText, response);
}
const data = await response.json<Answer>();
const createdTime = new Date(data.created_time * 1000);

return template({
title: data.question.title,
url: new URL(`${data.question.id}/answer/${id}`, `https://www.zhihu.com/question/`).href,
content: await fixImagesAndLinks(data.content),
reference: await extractReference(data.content),
excerpt: data.excerpt,
author: data.author.name,
created_time: createdTime.toISOString(),
created_time_formatted: createdTime.toDateString(),
voteup_count: data.voteup_count.toString(),
comment_count: data.comment_count.toString(),
question: data.question.detail.trim().length > 0 ? questionTemplate({
question: await fixImagesAndLinks(data.question.detail),
}) : '',
redirect: redirect ? 'true' : 'false',
author_url: data.author.url.replace("api.", ""),
headline: data.author.headline,
avatar_url: data.author.avatar_url,
});
return template({
title: data.question.title,
url: new URL(`${data.question.id}/answer/${id}`, `https://www.zhihu.com/question/`).href,
content: await fixImagesAndLinks(data.content),
reference: await extractReference(data.content),
excerpt: data.excerpt,
author: data.author.name,
created_time: createdTime.toISOString(),
created_time_formatted: createdTime.toDateString(),
voteup_count: data.voteup_count.toString(),
comment_count: data.comment_count.toString(),
question: data.question.detail.trim().length > 0 ? questionTemplate({
question: await fixImagesAndLinks(data.question.detail),
}) : '',
redirect: redirect ? 'true' : 'false',
author_url: data.author.url.replace("api.", ""),
headline: data.author.headline,
avatar_url: data.author.avatar_url,
});
}
215 changes: 107 additions & 108 deletions src/article.ts
Original file line number Diff line number Diff line change
@@ -1,133 +1,132 @@
import { fixImagesAndLinks, createTemplate, extractReference, FetchError } from "./lib";

export type Article = {
title: string;
content: string;
excerpt: string;
author: {
name: string;
url: string;
headline: string;
avatar_url: string;
};
created: number;
voteup_count: number;
comment_count: number;
image_url: string;
column: {
title: string;
description: string;
};
title: string;
content: string;
excerpt: string;
author: {
name: string;
url: string;
headline: string;
avatar_url: string;
};
created: number;
voteup_count: number;
comment_count: number;
image_url: string;
column: {
title: string;
description: string;
};
}

const template = createTemplate`
<!DOCTYPE html>
<html lang="zh">
<head>
<title>${"title"} | FxZhihu</title>
<meta charset="UTF-8">
<meta property="og:type" content="website">
<meta property="og:title" content="${"title"} | FxZhihu">
<meta property="og:site_name" content="FxZhihu / Fixup Zhihu">
<meta property="og:url" content="${"url"}">
<title>${"title"} | FxZhihu</title>
<meta charset="UTF-8">
<meta property="og:type" content="website">
<meta property="og:title" content="${"title"} | FxZhihu">
<meta property="og:site_name" content="FxZhihu / Fixup Zhihu">
<meta property="og:url" content="${"url"}">
<meta property="og:image" content="${"image_url"}">
<meta property="og:description" content="${"excerpt"}">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="FxZhihu" />
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="FxZhihu" />
<meta name="twitter:creator" content="@${"author"}" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/yue.css">
<script>
const redirect = ${"redirect"};
if (redirect) {
window.location.replace("${"url"}");
}
</script>
<style>
.origin_image {
width: 100%;
}
figure {
margin:1.4em 0;
}
figure img {
width: 100%;
}
img {
vertical-align: middle;
}
<script>
const redirect = ${"redirect"};
if (redirect) {
window.location.replace("${"url"}");
}
</script>
<style>
.origin_image {
width: 100%;
}
figure {
margin:1.4em 0;
}
figure img {
width: 100%;
}
img {
vertical-align: middle;
}
.author {
display: flex;
gap: 1em;
}
#avatar {
width: 100px;
height: 100px;
}
.author > div {
flex: 1;
}
a[data-draft-type="link-card"] {
display: block;
}
</style>
</style>
display: flex;
gap: 1em;
}
#avatar {
width: 100px;
height: 100px;
}
.author > div {
flex: 1;
}
a[data-draft-type="link-card"] {
display: block;
}
</style>
</head>
<body style="max-width: 1000px; margin: 0 auto; padding: 0 1em 0 1em;" class="yue">
<header>
<header>
<img class="origin_image" src="${"image_url"}"/>
<h1><a href="${"url"}">${"title"}</a></h1>
<h1><a href="${"url"}">${"title"}</a></h1>
<div class="author">
<img class="avatar" id="avatar" src="${"avatar_url"}" />
<div>
<h2 rel="author">
<a href="${"author_url"}" target="_blank">@${"author"}</a>
</h2>
<p> ${"headline"} </p>
</div>
</div>
<time datetime="${"created_time"}">发表于 ${"created_time_formatted"}</time>
<p rel="stats"style="color: #999; font-size: 0.9em;">${"voteup_count"} 👍 / ${"comment_count"} 💬</p>
</header>
<article>
${"content"}
${"reference"}
<hr>
<div class="column" style="margin: 1em 0; padding: 0.5em 1em; border: 2px solid #999; border-radius: 5px;">
<h2>专栏:${"column_title"}</h2>
<p>${"column_description"}</p>
</div>
</article>
<img class="avatar" id="avatar" src="${"avatar_url"}" />
<div>
<h2 rel="author">
<a href="${"author_url"}" target="_blank">@${"author"}</a>
</h2>
<p> ${"headline"} </p>
</div>
</div>
<time datetime="${"created_time"}">发表于 ${"created_time_formatted"}</time>
<p rel="stats"style="color: #999; font-size: 0.9em;">${"voteup_count"} 👍 / ${"comment_count"} 💬</p>
</header>
<article>
${"content"}
${"reference"}
<hr>
<div class="column" style="margin: 1em 0; padding: 0.5em 1em; border: 2px solid #999; border-radius: 5px;">
<h2>专栏:${"column_title"}</h2>
<p>${"column_description"}</p>
</div>
</article>
</body>
</html>
`;

export async function article(id: string, redirect: boolean, env: Env): Promise<string> {
const url = new URL(id, `https://api.zhihu.com/article/`);
const response = await fetch(url);
if (!response.ok) {
throw new FetchError(response.statusText, response);
}
const data = await response.json<Article>();
const createdTime = new Date(data.created * 1000);
const url = new URL(id, `https://api.zhihu.com/article/`);
const response = await fetch(url);
if (!response.ok) {
throw new FetchError(response.statusText, response);
}
const data = await response.json<Article>();
const createdTime = new Date(data.created * 1000);

return template({
title: data.title,
url: new URL(id, `https://zhuanlan.zhihu.com/p/`).href,
content: await fixImagesAndLinks(data.content),
reference: await extractReference(data.content),
excerpt: data.excerpt,
author: data.author.name,
created_time: createdTime.toISOString(),
created_time_formatted: createdTime.toDateString(),
voteup_count: data.voteup_count.toString(),
comment_count: data.comment_count.toString(),
column_title: data.column.title,
column_description: data.column.description,
redirect: redirect ? 'true' : 'false',
author_url: data.author.url.replace("api.", ""),
headline: data.author.headline,
avatar_url: data.author.avatar_url,
image_url: data.image_url,
});
return template({
title: data.title,
url: new URL(id, `https://zhuanlan.zhihu.com/p/`).href,
content: await fixImagesAndLinks(data.content),
reference: await extractReference(data.content),
excerpt: data.excerpt,
author: data.author.name,
created_time: createdTime.toISOString(),
created_time_formatted: createdTime.toDateString(),
voteup_count: data.voteup_count.toString(),
comment_count: data.comment_count.toString(),
column_title: data.column.title,
column_description: data.column.description,
redirect: redirect ? 'true' : 'false',
author_url: data.author.url.replace("api.", ""),
headline: data.author.headline,
avatar_url: data.author.avatar_url,
image_url: data.image_url,
});
}
Loading

0 comments on commit 34241ef

Please sign in to comment.