refactor: rename checkStartWithHttp to isHttpLink

- Update function name and implementation in utils/index.js
- Replace all occurrences in getPageProperties.js, post.js, and BlogPostCard.js
- Improve function documentation and use regex for better URL matching
This commit is contained in:
anime
2025-07-08 23:04:41 +08:00
parent 645edd7ead
commit b16512ac9a
4 changed files with 17 additions and 20 deletions

View File

@@ -4,7 +4,7 @@ import formatDate from '../utils/formatDate'
// import { createHash } from 'crypto'
import md5 from 'js-md5'
import { siteConfig } from '../config'
import { checkStartWithHttp, convertUrlStartWithOneSlash, getLastSegmentFromUrl, isMailOrTelLink } from '../utils'
import { convertUrlStartWithOneSlash, getLastSegmentFromUrl, isHttpLink, isMailOrTelLink } from '../utils'
import { extractLangPrefix } from '../utils/pageId'
import { mapImgUrl } from './mapImage'
import notionAPI from '@/lib/notion/getNotionAPI'
@@ -187,7 +187,7 @@ export function adjustPageProperties(properties, NOTION_CONFIG) {
}
// http or https 开头的视为外链
if (checkStartWithHttp(properties?.href)) {
if (isHttpLink(properties?.href)) {
properties.href = properties?.slug
properties.target = '_blank'
} else if (isMailOrTelLink(properties?.href)) {
@@ -238,7 +238,7 @@ export function adjustPageProperties(properties, NOTION_CONFIG) {
*/
function generateCustomizeSlug(postProperties, NOTION_CONFIG) {
// 外链不处理
if (checkStartWithHttp(postProperties.slug)) {
if (isHttpLink(postProperties.slug)) {
return postProperties.slug
}
let fullPrefix = ''

View File

@@ -89,19 +89,16 @@ export function isUrl(str) {
return false
}
return str?.indexOf('/') === 0 || checkStartWithHttp(str)
return str?.indexOf('/') === 0 || isHttpLink(str)
}
// 检查是否外链
export function checkStartWithHttp(str) {
// 检查字符串是否包含http
if (str?.indexOf('http:') === 0 || str?.indexOf('https:') === 0) {
// 如果包含找到http的位置
return true
} else {
// 不包含
return false
}
/**
* 判断是否是 http(s) 开头的链接(外部网页)
* @param str
* @returns {boolean}
*/
export function isHttpLink(str) {
return typeof str === 'string' && /^https?:\/\//i.test(str)
}
/**

View File

@@ -1,7 +1,7 @@
/**
* 文章相关工具
*/
import { checkStartWithHttp } from '.'
import { isHttpLink } from '.'
import { getPostBlocks } from '@/lib/db/getSiteData'
import { getPageTableOfContents } from '@/lib/notion/getPageTableOfContents'
import { siteConfig } from '@/lib/config'
@@ -59,7 +59,7 @@ export function checkSlugHasNoSlash(row) {
}
return (
(slug.match(/\//g) || []).length === 0 &&
!checkStartWithHttp(slug) &&
!isHttpLink(slug) &&
row.type.indexOf('Menu') < 0
)
}
@@ -76,7 +76,7 @@ export function checkSlugHasOneSlash(row) {
}
return (
(slug.match(/\//g) || []).length === 1 &&
!checkStartWithHttp(slug) &&
!isHttpLink(slug) &&
row.type.indexOf('Menu') < 0
)
}
@@ -94,7 +94,7 @@ export function checkSlugHasMorThanTwoSlash(row) {
return (
(slug.match(/\//g) || []).length >= 2 &&
row.type.indexOf('Menu') < 0 &&
!checkStartWithHttp(slug)
!isHttpLink(slug)
)
}

View File

@@ -1,5 +1,5 @@
import { siteConfig } from '@/lib/config'
import { checkStartWithHttp } from '@/lib/utils'
import { isHttpLink } from '@/lib/utils'
import Link from 'next/link'
import { useRouter } from 'next/router'
import NotionIcon from './NotionIcon'
@@ -23,7 +23,7 @@ const BlogPostCard = ({ post, className }) => {
return (
<Link
href={post?.href}
target={checkStartWithHttp(post.slug) ? '_blank' : '_self'}
target={isHttpLink(post.slug) ? '_blank' : '_self'}
passHref>
<div
key={post.id}