File size: 14,301 Bytes
96dd062
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
---
import { render } from "astro:content";
import * as path from "node:path";
import Comment from "@components/comment/index.astro";
import Markdown from "@components/common/Markdown.astro";
import KatexManager from "@components/features/KatexManager.astro";
import License from "@components/misc/License.astro";
import I18nKey from "@i18n/i18nKey";
import { i18n } from "@i18n/translation";
import MainGridLayout from "@layouts/MainGridLayout.astro";
import { getSortedPosts } from "@utils/content-utils";
import {
	getFileDirFromPath,
	getPostUrlBySlug,
	removeFileExtension,
} from "@utils/url-utils";
import { Icon } from "astro-icon/components";
import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";
import CoverImage from "@/components/common/CoverImage.astro";
import PostMetadata from "@/components/layout/PostMeta.astro";
import SharePoster from "@/components/misc/SharePoster.svelte";
import { coverImageConfig } from "@/config/coverImageConfig";
import { licenseConfig } from "@/config/licenseConfig";
import { profileConfig } from "@/config/profileConfig";
import { siteConfig } from "@/config/siteConfig";
import { sponsorConfig } from "@/config/sponsorConfig";
import { formatDateToYYYYMMDD } from "@/utils/date-utils";
import { processCoverImageSync } from "@/utils/image-utils";
import { url } from "@/utils/url-utils";

export async function getStaticPaths() {
	const blogEntries = await getSortedPosts();
	return blogEntries.map((entry) => {
		// 将 id 转换为 slug(移除扩展名)以匹配路由参数
		const slug = removeFileExtension(entry.id);
		return {
			params: { slug },
			props: { entry },
		};
	});
}

const { entry } = Astro.props;
const { Content, headings } = await render(entry);

const { remarkPluginFrontmatter } = await render(entry);

// 处理随机图:如果image为"api",则从配置的API获取随机图
const processedImage = processCoverImageSync(entry.data.image, entry.id);

let posterCoverUrl = processedImage;
if (processedImage) {
	const isLocal = !(
		processedImage.startsWith("/") ||
		processedImage.startsWith("http") ||
		processedImage.startsWith("https") ||
		processedImage.startsWith("data:")
	);
	if (isLocal) {
		const basePath = getFileDirFromPath(entry.filePath || "");
		const files = import.meta.glob<ImageMetadata>("../../**", {
			import: "default",
		});
		let normalizedPath = path
			.normalize(path.join("../../", basePath, processedImage))
			.replace(/\\/g, "/");
		const file = files[normalizedPath];
		if (file) {
			const img = await file();
			posterCoverUrl = img.src;
		}
	}
}

dayjs.extend(utc);

const jsonLd = {
	"@context": "https://schema.org",
	"@type": "BlogPosting",
	headline: entry.data.title,
	description: entry.data.description || entry.data.title,
	keywords: entry.data.tags,
	author: {
		"@type": "Person",
		name: profileConfig.name,
		url: Astro.site,
	},
	datePublished: formatDateToYYYYMMDD(entry.data.published),
	inLanguage: entry.data.lang
		? entry.data.lang.replace("_", "-")
		: siteConfig.lang.replace("_", "-"),
	// TODO include cover image here
};
---

<MainGridLayout

  banner={processedImage}

  title={entry.data.title}

  description={entry.data.description}

  lang={entry.data.lang}

  setOGTypeArticle={true}

  postSlug={entry.id}

  headings={headings}

>

  <!-- KaTeX 按需加载 - 仅在文章页面 -->

  <KatexManager slot="head" />

  

  <script

    is:inline

    slot="head"

    type="application/ld+json"

    set:html={JSON.stringify(jsonLd)}

  />

  <div

    class="flex w-full rounded-(--radius-large) overflow-hidden relative mb-4"

  >

    <div

      id="post-container"

      class:list={[

        "card-base z-10 px-6 md:px-9 pt-6 pb-4 relative w-full ",

        {},

      ]}

    >

      <!-- word count and reading time -->

      <div

        class="flex flex-row text-black/30 dark:text-white/30 gap-5 mb-3 transition onload-animation"

      >

        <div class="flex flex-row items-center">

          <div

            class="transition h-6 w-6 rounded-md bg-black/5 dark:bg-white/10 text-black/50 dark:text-white/50 flex items-center justify-center mr-2"

          >

            <Icon name="material-symbols:notes-rounded" />

          </div>

          <div class="text-sm">

            {remarkPluginFrontmatter.words}

            {" " + i18n(I18nKey.wordsCount)}

          </div>

        </div>

        <div class="flex flex-row items-center">

          <div

            class="transition h-6 w-6 rounded-md bg-black/5 dark:bg-white/10 text-black/50 dark:text-white/50 flex items-center justify-center mr-2"

          >

            <Icon name="material-symbols:schedule-outline-rounded" />

          </div>

          <div class="text-sm">

            {remarkPluginFrontmatter.minutes}

            {

              " " +

                i18n(

                  remarkPluginFrontmatter.minutes === 1

                    ? I18nKey.minuteCount

                    : I18nKey.minutesCount

                )

            }

          </div>

        </div>

      </div>



      <!-- title -->

      <div class="relative onload-animation">

        <h1

          data-pagefind-body

          data-pagefind-weight="10"

          data-pagefind-meta="title"

          class="transition w-full block font-bold mb-3

                    text-3xl md:text-[2.25rem]/[2.75rem]

                    text-black/90 dark:text-white/90

                    md:before:w-1 before:h-5 before:rounded-md before:bg-(--primary)

                    before:absolute before:top-3 before:-left-4.5"

        >

          {entry.data.title}

        </h1>

      </div>



      <!-- metadata -->

      <div class="onload-animation">

        <PostMetadata

          className="mb-5"

          published={entry.data.published}

          updated={entry.data.updated}

          tags={entry.data.tags}

          category={entry.data.category || undefined}

          id={entry.id}

        />

        {

          !processedImage && (

            <div class="border-(--line-divider) border-dashed border-b mt-3 mb-5" />

          )

        }

      </div>



      <!-- always show cover as long as it has one -->



      {

        processedImage && coverImageConfig.enableInPost && (

          <div style="margin-top:1rem;">

            <CoverImage

              id="post-cover"

              src={processedImage}

              basePath={getFileDirFromPath(entry.filePath || '')}

              class="mb-8 rounded-xl banner-container onload-animation"

              preview={false}

            />

          </div>

        )

      }



      <Markdown class="mb-6 markdown-content onload-animation">

        <Content />

      </Markdown>



      {/* 赞助按钮 & 分享按钮 */}

      {

        (siteConfig.sharePoster || (sponsorConfig.showButtonInPost && siteConfig.pages.sponsor)) && (

          <div class="mb-6 rounded-xl onload-animation">

            <div class="p-6 bg-(--license-block-bg) rounded-xl">

              <div class="flex flex-col sm:flex-row items-center justify-between gap-4">

                <div class="flex items-center gap-3 flex-1">

                  <div class="h-12 w-12 rounded-lg bg-(--primary) flex items-center justify-center text-white dark:text-black/70 shrink-0">

                    <Icon

                      name={sponsorConfig.showButtonInPost &&

                      siteConfig.pages.sponsor

                        ? "material-symbols:favorite"

                        : "material-symbols:share"}

                      class="text-2xl"

                    />

                  </div>

                  <div>

                    <h3 class="text-lg font-semibold text-neutral-900 dark:text-neutral-100 mb-1">

                      {

                        sponsorConfig.showButtonInPost && siteConfig.pages.sponsor

                          ? i18n(I18nKey.sponsorButton)

                          : i18n(I18nKey.shareOnSocial)

                      }

                    </h3>

                    <p class="text-sm text-neutral-600 dark:text-neutral-400">

                      {

                        sponsorConfig.showButtonInPost && siteConfig.pages.sponsor

                          ? i18n(I18nKey.sponsorButtonText)

                          : i18n(I18nKey.shareOnSocialDescription)

                      }

                    </p>

                  </div>

                </div>

                <div class="flex items-center gap-3">

                  {

                    siteConfig.sharePoster && (

                      <SharePoster

                        client:load

                        title={entry.data.title}

                        author={profileConfig.name}

                        description={entry.data.description || entry.data.title}

                        pubDate={formatDateToYYYYMMDD(entry.data.published)}

                        coverImage={posterCoverUrl}

                        url={Astro.url.href}

                        siteTitle={siteConfig.title}

                        avatar={profileConfig.avatar}

                      />

                    )

                  }

                  {

                    sponsorConfig.showButtonInPost && siteConfig.pages.sponsor && (

                      <a

                        href={url("/sponsor/")}

                        class="inline-flex items-center gap-2 px-6 py-3 bg-(--primary) text-white dark:text-black/70 rounded-lg font-medium hover:bg-(--primary)/80 hover:scale-105 active:scale-95 transition-all whitespace-nowrap"

                      >

                        <span>{i18n(I18nKey.sponsor)}</span>

                        <Icon name="fa7-solid:arrow-right" class="text-sm" />

                      </a>

                    )

                  }

                </div>

              </div>

            </div>

          </div>

        )

      }



      {

        licenseConfig.enable && (

          <License

            title={entry.data.title}

            id={entry.id}

            pubDate={entry.data.published}

            author={entry.data.author}

            sourceLink={entry.data.sourceLink}

            licenseName={entry.data.licenseName}

            licenseUrl={entry.data.licenseUrl}

            class="mb-6 rounded-xl license-container onload-animation"

          />

        )

      }

    </div>

  </div>



  <!-- 上次编辑时间 -->

  {

    siteConfig.showLastModified && (() => {

      const lastModified = dayjs(

        entry.data.updated || entry.data.published

      );

      const now = dayjs();

      const daysDiff = now.diff(lastModified, "day");

      // 使用用户定义的阈值,如果没有定义则默认为1天

      const outdatedThreshold = siteConfig.outdatedThreshold ?? 1;

      const shouldShowOutdatedCard = daysDiff >= outdatedThreshold;



      return shouldShowOutdatedCard ? (

        <div class="card-base p-6 mb-4">

          <div class="flex items-center gap-2">

            <div class="transition h-9 w-9 rounded-lg overflow-hidden relative flex items-center justify-center mr-0">

              <Icon

                name="material-symbols:history-rounded"

                class="text-4xl text-(--primary) transition-transform group-hover:translate-x-0.5 bg-(--enter-btn-bg) p-2 rounded-md"

              />

            </div>



            {(() => {

              const dateStr = lastModified.format("YYYY-MM-DD");

              const isOutdated = daysDiff >= 1;



              return (

                <div class="flex flex-col gap-0.1">

                  <div class="text-[1.0rem] leading-tight text-black/75 dark:text-white/75">

                    {`${i18n(I18nKey.lastModifiedPrefix)}${dateStr}${

                      isOutdated

                        ? `,${i18n(I18nKey.lastModifiedDaysAgo).replace("{days}", daysDiff.toString())}`

                        : ""

                    }`}

                  </div>

                  {isOutdated && (

                    <p class="text-[0.8rem] leading-tight text-black/75 dark:text-white/75">

                      {i18n(I18nKey.lastModifiedOutdated)}

                    </p>

                  )}

                </div>

              );

            })()}

          </div>

        </div>

      ) : null;

    })()

  }



  <div

    class="flex flex-col md:flex-row justify-between mb-4 gap-4 overflow-hidden w-full"

  >

    <a

      href={entry.data.nextSlug ? getPostUrlBySlug(entry.data.nextSlug) : "#"}

      class:list={[

        "w-full font-bold overflow-hidden active:scale-95",

        { "pointer-events-none": !entry.data.nextSlug },

      ]}

    >

      {

        entry.data.nextSlug && (

          <div class="btn-card rounded-2xl w-full h-15 max-w-full px-4 flex items-center justify-start! gap-4">

            <Icon

              name="material-symbols:chevron-left-rounded"

              class="text-[2rem] text-(--primary)"

            />

            <div class="overflow-hidden transition text-ellipsis whitespace-nowrap max-w-[calc(100%-3rem)] text-black/75 dark:text-white/75">

              {entry.data.nextTitle}

            </div>

          </div>

        )

      }

    </a>



    <a

      href={entry.data.prevSlug ? getPostUrlBySlug(entry.data.prevSlug) : "#"}

      class:list={[

        "w-full font-bold overflow-hidden active:scale-95",

        { "pointer-events-none": !entry.data.prevSlug },

      ]}

    >

      {

        entry.data.prevSlug && (

          <div class="btn-card rounded-2xl w-full h-15 max-w-full px-4 flex items-center justify-end! gap-4">

            <div class="overflow-hidden transition text-ellipsis whitespace-nowrap max-w-[calc(100%-3rem)] text-black/75 dark:text-white/75">

              {entry.data.prevTitle}

            </div>

            <Icon

              name="material-symbols:chevron-right-rounded"

              class="text-[2rem] text-(--primary)"

            />

          </div>

        )

      }

    </a>

  </div>



  <!-- 评论 -->

  {entry.data.comment && <Comment post={entry} />}

</MainGridLayout>