From 2ba38a30b4e978374323bdcf9b27143b35843397 Mon Sep 17 00:00:00 2001 From: Ad-closeNN <1709301095@qq.com> Date: Fri, 1 May 2026 21:38:46 +0800 Subject: [PATCH] =?UTF-8?q?feat(theme):=20=E5=9C=A8=20LLM=20=E4=B8=AD?= =?UTF-8?q?=E6=89=93=E5=BC=80=20Markdown?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/posts/[...slug].astro | 193 +++++++++++++++++++++++++++----- 1 file changed, 167 insertions(+), 26 deletions(-) diff --git a/src/pages/posts/[...slug].astro b/src/pages/posts/[...slug].astro index d3075c1..1f7f581 100644 --- a/src/pages/posts/[...slug].astro +++ b/src/pages/posts/[...slug].astro @@ -46,9 +46,9 @@ const normalizedPostSources = Object.fromEntries( ]), ); -function getPostSourcePath(entryId: string) { +function getPostSourceKey(entryId: string) { const normalizedEntryPath = `../../content/posts/${entryId}`.toLowerCase(); - const sourceKey = Object.keys(postSources).find((key) => { + return Object.keys(postSources).find((key) => { const normalizedKey = key.toLowerCase(); return ( normalizedKey === normalizedEntryPath || @@ -56,17 +56,24 @@ function getPostSourcePath(entryId: string) { normalizedKey === `${normalizedEntryPath}.mdx` ); }); +} +function getPostSourcePath(sourceKey: string | undefined) { return sourceKey?.replace("../../content/posts/", "src/content/posts/"); } function getGitHubPostSourceUrl(repo: string | undefined, sourcePath: string | undefined) { if (!repo || !sourcePath) return ""; - const repoUrl = repo.includes("://") ? repo : `https://github.com/${repo}`; - const normalizedRepo = repoUrl.replace(/\.git$/, "").replace(/\/+$/, ""); + const normalizedRepo = repo.replace(/\.git$/, "").replace(/\/+$/, ""); + const repoPath = normalizedRepo.includes("://") + ? new URL(normalizedRepo).pathname.replace(/^\/+|\/+$/g, "") + : normalizedRepo.replace(/^\/+|\/+$/g, ""); + const [owner, repository] = repoPath.split("/"); + if (!owner || !repository) return ""; + const encodedSourcePath = sourcePath.split("/").map(encodeURIComponent).join("/"); - return `${normalizedRepo}/blob/main/${encodedSourcePath}?plain=1`; + return `https://raw.githubusercontent.com/${encodeURIComponent(owner)}/${encodeURIComponent(repository)}/refs/heads/main/${encodedSourcePath}`; } function parseAiSummaryValue(value: string) { @@ -120,10 +127,34 @@ function getAiSummaryMeta(entryId: string) { } const aiSummaryMeta = getAiSummaryMeta(entry.id); -const postSourceUrl = getGitHubPostSourceUrl( - siteConfig.githubRepo, - getPostSourcePath(entry.id), -); +const postSourceKey = getPostSourceKey(entry.id); +const postSourcePath = getPostSourcePath(postSourceKey); +const postSourceUrl = getGitHubPostSourceUrl(siteConfig.githubRepo, postSourcePath); +const copyPageText = postSourceKey ? postSources[postSourceKey] : ""; +const aiPrompt = `Read from ${postSourceUrl} so I can ask questions about it.`; +const encodedAiPrompt = encodeURIComponent(aiPrompt); +const copyPageAiLinks = [ + { + label: "ChatGPT", + icon: "gpt", + href: `https://chatgpt.com/?q=${encodedAiPrompt}`, + }, + { + label: "Gemini", + icon: "gemini", + href: `https://gemini.google.com/app?q=${encodedAiPrompt}`, + }, + { + label: "Claude", + icon: "claude", + href: `https://claude.ai/new?q=${encodedAiPrompt}`, + }, + { + label: "Grok", + icon: "grok", + href: `https://grok.com/?q=${encodedAiPrompt}`, + }, +]; const jsonLd = { "@context": "https://schema.org", @@ -182,17 +213,59 @@ const isOutdated = entry.data.outdated;