Skip to content

Commit

Permalink
#87 validateUrlを削除
Browse files Browse the repository at this point in the history
  • Loading branch information
miyaji255 committed Feb 10, 2024
1 parent fa77ed9 commit ba5b5dd
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 59 deletions.
4 changes: 2 additions & 2 deletions astro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import remarkLinkCard from 'remark-link-card'
import RemarkLinkRewrite, {
type RemarkLinkRewriteOptions,
} from 'remark-link-rewrite'
import { formatUrl } from './src/utils/validateUrl'
import { getFormatUrl } from './src/utils/validateUrl'

const { SITE_URL } = loadEnv(process.env.NODE_ENV!, process.cwd(), '')

Expand Down Expand Up @@ -43,7 +43,7 @@ export default defineConfig({
remarkLinkCard,
[
RemarkLinkRewrite,
{ replacer: formatUrl } satisfies RemarkLinkRewriteOptions,
{ replacer: getFormatUrl(SITE_URL) } satisfies RemarkLinkRewriteOptions,
],
],
},
Expand Down
2 changes: 1 addition & 1 deletion src/components/activity/EventsSection.astro
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const items: Item[] = [
image: welcomeImage,
description:
'プログラミングの基礎、機械学習、3Dモデリングなど様々なテーマで、数カ月間にわたり講習会が開催されます。新歓イベントの一環として開催しており、どなたでも参加できます。※画像は2022年度のものです。',
link: { href: '/workshop', text: '講習会について詳しく' },
link: { href: '/workshop/', text: '講習会について詳しく' },
},
{
title: '5月 いちょう祭',
Expand Down
9 changes: 1 addition & 8 deletions src/components/common/InlineLink.astro
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
---
import { formatUrl } from '@/utils/validateUrl'
import type { HTMLAttributes } from 'astro/types'
type Props = Omit<HTMLAttributes<'a'>, 'slot'>
const { class: className, href, ...attrs } = Astro.props
const formattedHref = href && formatUrl(href)
---

<a
href={formattedHref}
class:list={['text-blue-600 underline', className]}
{...attrs}
>
<a href={href} class:list={['text-blue-600 underline', className]} {...attrs}>
<slot />
</a>
2 changes: 1 addition & 1 deletion src/components/faq/FaqSection.astro
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import FaqCard from './FaqCard.astro'
普段はDiscordサーバーで雑談をしているほか、月に一度週末にオンラインで集まって部会を開催しています。また、大阪大学の大学祭であるいちょう祭(5月)とまちかね祭(11月)への出展は部員全員で協力して行います。
</p>
<p>
詳しくは<InlineLink href="/activity">
詳しくは<InlineLink href="/activity/">
活動紹介
</InlineLink>をご覧ください。
</p>
Expand Down
4 changes: 2 additions & 2 deletions src/components/index/activity/ActivitySection.astro
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const items: Item[] = [
image: welcomeImage,
description:
'プログラミングの基礎、機械学習、3Dモデリングなど様々なテーマで、数カ月間にわたり講習会が開催されます。新歓イベントの一環として開催しており、どなたでも参加できます。※画像は2022年度のものです。',
link: { href: '/workshop', text: '講習会について詳しく' },
link: { href: '/workshop/', text: '講習会について詳しく' },
},
{
title: '5月 いちょう祭',
Expand Down Expand Up @@ -52,7 +52,7 @@ const items: Item[] = [
}
</ul>
<div class="flex justify-center">
<LinkButton href="/activity">活動紹介について詳しく</LinkButton>
<LinkButton href="/activity/">活動紹介について詳しく</LinkButton>
</div>
</div>
</Section>
2 changes: 1 addition & 1 deletion src/components/index/faq/FaqSection.astro
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import LinkButton from '@/components/common/button/LinkButton.astro'
「部室はあるの?」「活動には必ず参加しないといけないの?」「部費はかかるの?」など、よくある質問はこちらから。
</div>
<div class="flex justify-center">
<LinkButton href="/faq">FAQを確認する</LinkButton>
<LinkButton href="/faq/">FAQを確認する</LinkButton>
</div>
</div>
</Section>
2 changes: 1 addition & 1 deletion src/components/index/join/JoinSection.astro
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import LinkButton from '@/components/common/button/LinkButton.astro'
まずは公開サーバーに参加し、部内の雰囲気を感じてみませんか?
</div>
<div class="flex justify-center">
<LinkButton href="/join" variant="white">Join OUCC!</LinkButton>
<LinkButton href="/join/" variant="white">Join OUCC!</LinkButton>
</div>
</div>
</Section>
2 changes: 1 addition & 1 deletion src/components/layout/Footer.astro
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ import FooterLink from './FooterLink.astro'
</div>
<div class="flex flex-wrap items-center justify-center gap-4 px-4">
<FooterLink to="/oucc-rules.pdf">規約</FooterLink>
<FooterLink to="/privacy">プライバシーポリシー</FooterLink>
<FooterLink to="/privacy/">プライバシーポリシー</FooterLink>
</div>
</footer>
3 changes: 0 additions & 3 deletions src/components/layout/nav/BlogNav.astro
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
import { BlogCategoryMapping } from '@/content/config'
import { validateUrl } from '@/utils/validateUrl'
const current = Astro.url.pathname
const navList = [
Expand All @@ -21,8 +20,6 @@ const navList = [
isHere: '/blog/tags/' === current,
},
] as const satisfies { target: string; title: string; isHere: boolean }[]
for (const { target } of navList) validateUrl(target)
---

<div class="flex flex-nowrap gap-3 px-9 font-sans text-lg font-medium">
Expand Down
7 changes: 2 additions & 5 deletions src/components/layout/nav/Header.astro
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Image from '@/components/common/Image.astro'
import ouccImage from '@/assets/oucc.svg'
import LinkButton from '@/components/common/button/LinkButton.astro'
import Navigation from './Navigation.astro'
import { formatUrl } from '@/utils/validateUrl'
interface Props {
showJoinLink?: boolean
Expand All @@ -19,8 +18,6 @@ const {
rootPath = '/',
headerTitle,
} = Astro.props
const formattedRootPath = formatUrl(rootPath)
---

<header
Expand All @@ -42,7 +39,7 @@ const formattedRootPath = formatUrl(rootPath)
class="w-5"
/>
</button>
<a href={formattedRootPath} class="flex basis-0 items-center">
<a href={rootPath} class="flex basis-0 items-center">
<span class="aspect-square h-full">
<Image
src={ouccImage}
Expand All @@ -55,7 +52,7 @@ const formattedRootPath = formatUrl(rootPath)
</a>
</div>
<div class="flex shrink-0 flex-col justify-center">
{showJoinLink && <LinkButton href="/join">Join OUCC!</LinkButton>}
{showJoinLink && <LinkButton href="/join/">Join OUCC!</LinkButton>}
</div>
{
/**
Expand Down
12 changes: 6 additions & 6 deletions src/components/layout/nav/Navigation.astro
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import DrWani from './DrWani.astro'
</div>
<ul class="flex flex-col text-center">
<NavigationListItem to="/">トップページ</NavigationListItem>
<NavigationListItem to="/join">Join OUCC!</NavigationListItem>
<NavigationListItem to="/activity">活動紹介</NavigationListItem>
<NavigationListItem to="/workshop">講習会</NavigationListItem>
<NavigationListItem to="/faq">FAQ</NavigationListItem>
<NavigationListItem to="/contact">お問い合わせ</NavigationListItem>
<NavigationListItem to="/blog">ブログ</NavigationListItem>
<NavigationListItem to="/join/">Join OUCC!</NavigationListItem>
<NavigationListItem to="/activity/">活動紹介</NavigationListItem>
<NavigationListItem to="/workshop/">講習会</NavigationListItem>
<NavigationListItem to="/faq/">FAQ</NavigationListItem>
<NavigationListItem to="/contact/">お問い合わせ</NavigationListItem>
<NavigationListItem to="/blog/">ブログ</NavigationListItem>
<NavigationListItem to="https://twitter.com/OUCC" targetBlank>
X (旧Twitter)
</NavigationListItem>
Expand Down
5 changes: 1 addition & 4 deletions src/components/layout/nav/NavigationListItem.astro
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
---
import { formatUrl } from '@/utils/validateUrl'
interface Props {
to: string
targetBlank?: boolean
}
const { to, targetBlank } = Astro.props
const formattedTo = formatUrl(to)
---

<li class="border-t last:border-b">
<a
href={formattedTo}
href={to}
target={targetBlank ? '_blank' : undefined}
class:list={[
'block bg-white p-4 font-bold',
Expand Down
2 changes: 1 addition & 1 deletion src/layouts/BlogLayout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const ogImageUrl = new URL(image, Astro.site).toString()
showJoinLink={false}
isSticky={false}
headerTitle="BLOG"
rootPath="/blog"
rootPath="/blog/"
/>
<BlogNav />
<div class="bg-dot bg-dot-secondary flex-grow">
Expand Down
35 changes: 12 additions & 23 deletions src/utils/validateUrl.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,21 @@
/** 対象のURL */
const pattern = new RegExp(
`^(${import.meta.env.SITE.replaceAll('.', '\\.')}|/)`,
)

/**
* oucc.orgに向けたURLの末尾に/をつけます
*/
export function formatUrl(path: string | URL): string {
if (path instanceof URL) return formatUrl(path.href)

if (!path.match(pattern)) return path

const [url, hash] = path.split('#')
export function getFormatUrl(base: string = import.meta.env.SITE) {
/** 対象のURL */
const pattern = new RegExp(`^(${base.replaceAll('.', '\\.')}|/)`)

if (url!.endsWith('/')) return path
return hash ? `${url}/#${hash}` : url + '/'
}
return function formatUrl(path: string | URL): string {
if (path instanceof URL) return formatUrl(path.href)

/**
* oucc.orgに向けたURLの末尾に/がない場合、例外を発生させます
* @throws {Error} - URLが条件を満たさない場合に発生する例外
*/
export function validateUrl(path: string): void {
if (!path.match(pattern)) return
if (!path.match(pattern)) return path

const [url] = path.split('#')
const [url, hash] = path.split('#')

if (url!.endsWith('/')) return
if (url!.endsWith('/')) return path
// ファイル拡張子
if (url!.match(/\.(a-zA-Z0-9)+$/)) return path

throw new Error(`URLの末尾には/が必要です。 url:${url}`)
return hash ? `${url}/#${hash}` : url + '/'
}
}

0 comments on commit ba5b5dd

Please sign in to comment.