diff --git a/lib/notion/getPageProperties.js b/lib/notion/getPageProperties.js index f3a20ea2..39ebb97d 100644 --- a/lib/notion/getPageProperties.js +++ b/lib/notion/getPageProperties.js @@ -5,7 +5,11 @@ import formatDate from '../utils/formatDate' // import { createHash } from 'crypto' import md5 from 'js-md5' import { siteConfig } from '../config' -import { checkContainHttp, sliceUrlFromHttp } from '../utils' +import { + checkContainHttp, + convertUrlStartWithOneSlash, + sliceUrlFromHttp +} from '../utils' import { mapImgUrl } from './mapImage' /** @@ -191,10 +195,10 @@ export function adjustPageProperties(properties, NOTION_CONFIG) { } } - // 最终检查超链接 + // 检查处理外链 properties.href = checkContainHttp(properties?.href) ? sliceUrlFromHttp(properties?.href) - : `/${properties.href}` + : convertUrlStartWithOneSlash(properties?.href) // 设置链接在页内或新页面打开 if (properties.href?.indexOf('http') === 0) { @@ -224,7 +228,11 @@ function generateCustomizeSlug(postProperties, NOTION_CONFIG) { NOTION_CONFIG ).split('/') - const POST_URL_PREFIX_MAPPING_CATEGORY = siteConfig('POST_URL_PREFIX_MAPPING_CATEGORY',{},NOTION_CONFIG) + const POST_URL_PREFIX_MAPPING_CATEGORY = siteConfig( + 'POST_URL_PREFIX_MAPPING_CATEGORY', + {}, + NOTION_CONFIG + ) allSlugPatterns.forEach((pattern, idx) => { if (pattern === '%year%' && postProperties?.publishDay) { @@ -244,8 +252,9 @@ function generateCustomizeSlug(postProperties, NOTION_CONFIG) { } else if (pattern === '%category%' && postProperties?.category) { let categoryPrefix = postProperties.category // 允许映射分类名,通常用来将中文分类映射成英文,美化url. - if(POST_URL_PREFIX_MAPPING_CATEGORY[postProperties?.category]){ - categoryPrefix = POST_URL_PREFIX_MAPPING_CATEGORY[postProperties?.category] + if (POST_URL_PREFIX_MAPPING_CATEGORY[postProperties?.category]) { + categoryPrefix = + POST_URL_PREFIX_MAPPING_CATEGORY[postProperties?.category] } fullPrefix += categoryPrefix } else if (!pattern.includes('%')) { diff --git a/lib/utils/index.js b/lib/utils/index.js index b0652494..b3e5b2da 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -61,6 +61,26 @@ export function sliceUrlFromHttp(str) { } } +/** + * 将相对路径的url test 转为绝对路径 /test + * 判断url如果不是以 /开头,则拼接一个 / + * 同时如果开头有重复的多个 // ,则只保留一个 + * @param {*} str + */ +export function convertUrlStartWithOneSlash(str) { + if (!str) { + return '#' + } + // 判断url是否以 / 开头 + if (!str.startsWith('/')) { + // 如果不是,则在前面拼接一个 / + str = '/' + str + } + // 移除开头的多个连续斜杠,只保留一个 + str = str.replace(/\/+/g, '/') + return str +} + // 检查是否外链 export function checkContainHttp(str) { // 检查字符串是否包含http