Skip to content

Commit

Permalink
feat: inline link support self parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Innei committed Jul 13, 2023
1 parent cad1038 commit 756de9e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/components/ui/markdown/renderers/LinkRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
isGithubCommitUrl,
isGithubFilePreviewUrl,
isGithubRepoUrl,
isSelfArticleUrl,
isTweetUrl,
isYoutubeUrl,
parseGithubGistUrl,
Expand Down Expand Up @@ -124,6 +125,9 @@ export const LinkRenderer = ({ href }: { href: string }) => {
</FixedRatioContainer>
)
}
case isSelfArticleUrl(url): {
return <LinkCard source="self" id={url.pathname.slice(1)} />
}
}
}
// fallback to default renderer
Expand Down
28 changes: 28 additions & 0 deletions src/lib/link-parser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { isDev } from './env'

export const isTweetUrl = (url: URL) => {
return url.hostname === 'twitter.com' && url.pathname.startsWith('/')
}
Expand Down Expand Up @@ -60,6 +62,32 @@ export const isCodesandboxUrl = (url: URL) => {
)
}

export const isSelfArticleUrl = (url: URL) => {
if (isDev && url.hostname === 'innei.in') return true
return (
url.hostname === location.hostname &&
['posts/', 'notes/'].some((path) => url.pathname.startsWith(path))
)
}

export const parseSelfArticleUrl = (url: URL) => {
const [_, type, ...rest] = url.pathname.split('/')
switch (type) {
case 'posts': {
return {
type,
slug: rest.join('/'),
}
}
case 'notes': {
return {
type,
nid: +rest[0],
}
}
}
}

export const parseGithubRepoUrl = (url: URL) => {
const [_, owner, repo] = url.pathname.split('/')
return {
Expand Down

1 comment on commit 756de9e

@vercel
Copy link

@vercel vercel bot commented on 756de9e Jul 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

shiro – ./

shiro-innei.vercel.app
shiro-git-main-innei.vercel.app
springtide.vercel.app
innei.in

Please sign in to comment.