-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: fix editorconfig and format scripts
Signed-off-by: Frost Ming <[email protected]>
- Loading branch information
Showing
7 changed files
with
519 additions
and
521 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} |
Oops, something went wrong.