Skip to content

Commit

Permalink
Merge branch 'feat/url-prefix-category-mapping' into release/4.5.2
Browse files Browse the repository at this point in the history
  • Loading branch information
tangly1024 committed May 13, 2024
2 parents 617cf1d + c337c06 commit 9481b6a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
24 changes: 20 additions & 4 deletions lib/notion/getPageProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'

/**
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
20 changes: 20 additions & 0 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9481b6a

Please sign in to comment.