Merge branch 'feat/url-prefix-category-mapping' into release/4.5.2

This commit is contained in:
tangly1024.com
2024-05-13 11:39:05 +08:00
2 changed files with 40 additions and 4 deletions

View File

@@ -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,6 +228,12 @@ function generateCustomizeSlug(postProperties, NOTION_CONFIG) {
NOTION_CONFIG
).split('/')
const POST_URL_PREFIX_MAPPING_CATEGORY = siteConfig(
'POST_URL_PREFIX_MAPPING_CATEGORY',
{},
NOTION_CONFIG
)
allSlugPatterns.forEach((pattern, idx) => {
if (pattern === '%year%' && postProperties?.publishDay) {
const formatPostCreatedDate = new Date(postProperties?.publishDay)
@@ -240,7 +250,13 @@ function generateCustomizeSlug(postProperties, NOTION_CONFIG) {
} else if (pattern === '%slug%') {
fullPrefix += postProperties.slug ?? postProperties.id
} else if (pattern === '%category%' && postProperties?.category) {
fullPrefix += postProperties.category
let categoryPrefix = postProperties.category
// 允许映射分类名通常用来将中文分类映射成英文美化url.
if (POST_URL_PREFIX_MAPPING_CATEGORY[postProperties?.category]) {
categoryPrefix =
POST_URL_PREFIX_MAPPING_CATEGORY[postProperties?.category]
}
fullPrefix += categoryPrefix
} else if (!pattern.includes('%')) {
fullPrefix += pattern
} else {

View File

@@ -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