Skip to content

Commit

Permalink
feat: 提取获取地址方法
Browse files Browse the repository at this point in the history
  • Loading branch information
songxingguo committed Jun 6, 2024
1 parent b49f170 commit 911b46e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 18 deletions.
38 changes: 26 additions & 12 deletions .astro/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,30 +126,44 @@ declare module 'astro:content' {

type ContentEntryMap = {
"posts": {
"从零开始搭建在线个人简历.md": {
id: "从零开始搭建在线个人简历.md";
slug: "从零开始搭建在线个人简历";
"技术/ThreeDemo.md": {
id: "技术/ThreeDemo.md";
slug: "技术/threedemo";
body: string;
collection: "posts";
data: InferEntrySchema<"posts">
} & { render(): Render[".md"] };
"使用Github Action部署静态网站.md": {
id: "使用Github Action部署静态网站.md";
slug: "使用github-action部署静态网站";
"技术/从零开始搭建在线个人简历.md": {
id: "技术/从零开始搭建在线个人简历.md";
slug: "技术/从零开始搭建在线个人简历";
body: string;
collection: "posts";
data: InferEntrySchema<"posts">
} & { render(): Render[".md"] };
"多媒体前端手册.md": {
id: "多媒体前端手册.md";
slug: "多媒体前端手册";
"技术/使用Github Action部署静态网站.md": {
id: "技术/使用Github Action部署静态网站.md";
slug: "技术/使用github-action部署静态网站";
body: string;
collection: "posts";
data: InferEntrySchema<"posts">
} & { render(): Render[".md"] };
"如何将语雀文章发布到Hexo博客.md": {
id: "如何将语雀文章发布到Hexo博客.md";
slug: "如何将语雀文章发布到hexo博客";
"技术/多媒体前端手册.md": {
id: "技术/多媒体前端手册.md";
slug: "技术/多媒体前端手册";
body: string;
collection: "posts";
data: InferEntrySchema<"posts">
} & { render(): Render[".md"] };
"技术/如何将语雀文章发布到Hexo博客.md": {
id: "技术/如何将语雀文章发布到Hexo博客.md";
slug: "技术/如何将语雀文章发布到hexo博客";
body: string;
collection: "posts";
data: InferEntrySchema<"posts">
} & { render(): Render[".md"] };
"技术/如何配置泛域名证书.md": {
id: "技术/如何配置泛域名证书.md";
slug: "技术/如何配置泛域名证书";
body: string;
collection: "posts";
data: InferEntrySchema<"posts">
Expand Down
7 changes: 3 additions & 4 deletions src/components/Link.astro
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
---
import { getPostUrl } from "~/utils";
interface Props {
href: string;
originalUrl?: string;
}
const { href, originalUrl } = Astro.props;
const host = originalUrl?.split("/")[2] || ""; // 获取域名地址
const original = host.split(".").slice(0, -2).join("."); //获取子域名
const url = !original ? href : originalUrl;
const { href, originalUrl = "" } = Astro.props;
const { original, url } = getPostUrl(href, originalUrl);
---

<a href={url}
Expand Down
7 changes: 5 additions & 2 deletions src/pages/atom.xml.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import rss from "@astrojs/rss";
import { getPosts } from "~/utils";
import { getPosts, getPostUrl } from "~/utils";
import { THEME_CONFIG } from "~/theme.config";
import type { APIContext } from "astro";
import sanitizeHtml from "sanitize-html";
Expand All @@ -17,8 +17,11 @@ export async function GET(_context: APIContext) {
description: desc,
site: website,
items: posts.map((post) => {
const { originalUrl = "" } = post.data;
const href = `/posts/${post.slug}/`;
const { url } = getPostUrl(href, originalUrl);
return {
link: `/posts/${post.slug}/`,
link: url,
author: author,
content: sanitizeHtml(parser.render(post.body), { allowedTags }),
title: post.data.title,
Expand Down
7 changes: 7 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,10 @@ export function getPathFromCategory(
const mappingPath = category_map.find((l) => l.name === category);
return mappingPath ? mappingPath.path : category;
}

export function getPostUrl(href: string, originalUrl: string) {
const host = originalUrl?.split("/")[2] || ""; // 获取域名地址
const original = host.split(".").slice(0, -2).join("."); //获取子域名
const url = !original ? href : originalUrl;
return { original, url };
}

0 comments on commit 911b46e

Please sign in to comment.