Skip to content

Commit

Permalink
新增映射支持: url 前缀 %category%中文分类名映射成英文
Browse files Browse the repository at this point in the history
  • Loading branch information
tangly1024 committed May 13, 2024
1 parent a5bab22 commit c337c06
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
21 changes: 15 additions & 6 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,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) {
Expand All @@ -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('%')) {
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 c337c06

Please sign in to comment.