Skip to content

Commit

Permalink
update footer
Browse files Browse the repository at this point in the history
  • Loading branch information
zizdlp committed Aug 25, 2024
1 parent 86e32bf commit a177e86
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,55 @@ import { Metadata } from "next";
import { SearchParams } from "@/types/interface";
import { FetchError } from "@/fetchs/util";
import { logger } from "@/utils/logger";

async function fetchMarkdownContent({
username,
reponame,
href,
locale,
}: {
username: string;
reponame: string;
href: string;
locale: string;
}) {
const xforward = headers().get("x-forwarded-for") ?? "";
const agent = headers().get("User-Agent") ?? "";
const response = await fetchServerWithAuthWrapper({
endpoint: FetchServerWithAuthWrapperEndPoint.GET_MARKDOWN_CONTENT,
xforward,
agent,
tags: [],
values: {
username,
repo_name: decodeURIComponent(reponame),
relative_path: decodeURIComponent(href).replace(/,/g, "/"),
lang: locale || "en",
},
});

if (response.error) {
throw new FetchError(response.message, response.status);
}
return response;
}

function parseMarkdownList(markdownList: string): string[] {
const reg = /href="#(.*?)"/g;
const matches = markdownList.match(reg);
return matches ? matches.map((val) => val.slice(7, -1)) : [];
}

export async function generateMetadata({
params,
}: {
params: { href: string; username: string; reponame: string };
}): Promise<Metadata> {
var url = decodeURIComponent(params.href); // 将 params.href 转换为字符串
var parts = url.split(",");
var lastPart = parts[parts.length - 1];
var firstPart = parts[0] ?? "";
const url = decodeURIComponent(params.href);
const parts = url.split(",");
const [firstPart, lastPart] = [parts[0] ?? "", parts[parts.length - 1]];
return {
title: parts.length > 1 ? firstPart + ": " + lastPart : lastPart,
title: parts.length > 1 ? `${firstPart}: ${lastPart}` : lastPart,
};
}

Expand All @@ -30,56 +68,38 @@ export default async function MarkdownPage({
}) {
try {
const currentPage = Number(searchParams?.page) || 1;
const { username, reponame, href, locale } = params;
// const delay = Math.floor(Math.random() * 4000) + 1400;
// await new Promise((resolve) => setTimeout(resolve, delay));
const xforward = headers().get("x-forwarded-for") ?? "";
const agent = headers().get("User-Agent") ?? "";
const data = await fetchServerWithAuthWrapper({
endpoint: FetchServerWithAuthWrapperEndPoint.GET_MARKDOWN_CONTENT,
xforward: xforward,
agent: agent,
tags: [],
values: {
username: params.username,
repo_name: decodeURIComponent(params.reponame),
relative_path: decodeURIComponent(params.href).split(",").join("/"),
lang: params.locale == "" ? "en" : params.locale,
},
const data = await fetchMarkdownContent({
username,
reponame,
href,
locale,
});
if (data.error) {
throw new FetchError(data.message, data.status);
}

const { markdown, prev, next, footers, updated_at, theme_color } = data;
const markdownText = markdown.main_content;
const markdownID = markdown.markdown_id;
const href_seg = markdown.relative_path.split("/");
const href = href_seg.slice(0, -1).join("/");
let markdownList = "";
let sectionIds: string[] = [];

if (markdown.table_content) {
markdownList = markdown.table_content;
const reg = /href="#(.*?)"/g;
const res = markdownList.match(reg);

if (res) {
sectionIds = res.map((value: string) => value.slice(7, -1));
}
}
const hrefSegments = markdown.relative_path.split("/");
const prefixPath = hrefSegments.slice(0, -1).join("/");
const markdownList = markdown.table_content || "";
const sectionIds = markdown.table_content
? parseMarkdownList(markdown.table_content)
: [];

return (
<WikiInfo
markdowntext={markdownText}
markdownlist={markdownList}
prefixPath={href}
prefixPath={prefixPath}
NavBarOpen={true}
sectionIds={sectionIds}
markdown_id={markdownID}
currentPage={currentPage}
searchParams={searchParams}
username={params.username}
repo_name={params.reponame}
username={username}
repo_name={reponame}
prev={prev}
next={next}
footers={footers}
Expand All @@ -88,8 +108,11 @@ export default async function MarkdownPage({
/>
);
} catch (error) {
let e = error as FetchError;
logger.error(`fetch MarkdownPage failed:${e.message}`, e.status);
const fetchError = error as FetchError;
logger.error(
`Failed to fetch MarkdownPage: ${fetchError.message}`,
fetchError.status
);
return <NotFound />;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,52 @@ import { fetchServerWithAuthWrapper } from "@/fetchs/server_with_auth";
import { FetchServerWithAuthWrapperEndPoint } from "@/fetchs/server_with_auth_util";
import { headers } from "next/headers";
import { FetchError } from "@/fetchs/util";
export default async function RepoDetail({
params: { reponame, username, locale },
}: {
params: { reponame: string; username: string; locale: string };
}) {
let home_page = "readme";

interface RepoDetailParams {
reponame: string;
username: string;
locale: string;
}

async function fetchHomePage({ reponame, username, locale }: RepoDetailParams) {
const xforward = headers().get("x-forwarded-for") ?? "";
const agent = headers().get("User-Agent") ?? "";

try {
const xforward = headers().get("x-forwarded-for") ?? "";
const agent = headers().get("User-Agent") ?? "";
const data = await fetchServerWithAuthWrapper({
endpoint: FetchServerWithAuthWrapperEndPoint.GET_FIRST_DOCUMENT,
xforward: xforward,
agent: agent,
xforward,
agent,
tags: [],
values: {
username: username,
username,
repo_name: decodeURIComponent(reponame),
lang: locale == "" ? "en" : locale,
lang: locale || "en",
},
});
console.log("error:", data);

if (data.error) {
throw new FetchError(data.message, data.status);
}
home_page = data.relative_path;

return data.relative_path;
} catch (error) {
console.error("Error fetching home page:", error);
return null;
}
}

export default async function RepoDetail({
params,
}: {
params: RepoDetailParams;
}) {
const { reponame, username, locale } = params;
const homePage = await fetchHomePage({ reponame, username, locale });

if (!homePage) {
return <NotFound />;
}
redirect(`/workspace/${username}/o/${reponame}/${home_page}`); // Navigate to the new post page

redirect(`/workspace/${username}/o/${reponame}/${homePage}`);
}
4 changes: 2 additions & 2 deletions zbook_frontend/src/components/MainContentFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default async function MainContentFooter({
<div className="not-prose my-12 grid lg:grid-cols-2 gap-4">
{prev && (
<div
className={`group relative rounded-xl border-2 border-slate-200 dark:border-slate-800 hover:dark:border-${theme_color}-600 hover:border-${theme_color}-500 hover:bg-${theme_color}-400/10 dark:hover:bg-slate-800 `}
className={`group relative rounded-xl border border-slate-200 dark:border-slate-800 hover:dark:border-${theme_color}-600 hover:border-${theme_color}-500 hover:bg-${theme_color}-400/10 dark:hover:bg-slate-800 `}
>
<div className="relative overflow-hidden rounded-xl py-3 px-6 text-left">
<h2 className="mt-1 font-display text-base text-slate-900 dark:text-white">
Expand All @@ -45,7 +45,7 @@ export default async function MainContentFooter({

{next && (
<div
className={`group relative rounded-xl border-2 border-slate-200 dark:border-slate-800 hover:dark:border-${theme_color}-600 hover:border-${theme_color}-500 hover:bg-${theme_color}-400/10 dark:hover:bg-slate-800 ${prev ? "" : "lg:col-start-2"}`}
className={`group relative rounded-xl border border-slate-200 dark:border-slate-800 hover:dark:border-${theme_color}-600 hover:border-${theme_color}-500 hover:bg-${theme_color}-400/10 dark:hover:bg-slate-800 ${prev ? "" : "lg:col-start-2"}`}
>
<div className="relative overflow-hidden rounded-xl py-3 px-6 text-right">
<h2 className="mt-1 font-display text-base text-slate-900 dark:text-white">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export default function SearchMarkdownComponent(props: ProfileProps) {
<SearchItemWrapper>
<div
onClick={() => {
console.log("refresh page");
setSideBarReload(!sideBarReload);
// refreshPage("/", true, false);
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ export default function RepoSideBarSetting({
if (matches) {
page_type = matches[2] || "";
}
console.log("page_type:", page_type);
}

const t = useTranslations("SideBar");
Expand Down

0 comments on commit a177e86

Please sign in to comment.