Skip to content

Commit

Permalink
Merge pull request tangly1024#2951 from tangly1024/fix/url-prefix-not…
Browse files Browse the repository at this point in the history
…ion-config

Fix/url prefix notion config
  • Loading branch information
tangly1024 authored Nov 13, 2024
2 parents 47b8d97 + e5a848c commit 1ec3ef5
Show file tree
Hide file tree
Showing 8 changed files with 352 additions and 352 deletions.
473 changes: 232 additions & 241 deletions lib/db/getSiteData.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion lib/notion/getNotionConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ export async function getConfigMapFromConfigPage(allPages) {
// 只导入生效的配置
if (config.enable) {
// console.log('[Notion配置]', config.key, config.value)
notionConfig[config.key] = config.value
notionConfig[config.key] = config.value || ''
// 配置不能是undefined,至少是空字符串
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions lib/notion/getPageProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,7 @@ export function adjustPageProperties(properties, NOTION_CONFIG) {
// 1.按照用户配置的URL_PREFIX 转换一下slug
// 2.为文章添加一个href字段,存储最终调整的路径
if (properties.type === 'Post') {
if (siteConfig('POST_URL_PREFIX', '', NOTION_CONFIG)) {
properties.slug = generateCustomizeSlug(properties, NOTION_CONFIG)
}
properties.slug = generateCustomizeSlug(properties, NOTION_CONFIG)
properties.href = properties.slug ?? properties.id
} else if (properties.type === 'Page') {
properties.href = properties.slug ?? properties.id
Expand Down Expand Up @@ -245,12 +243,14 @@ function generateCustomizeSlug(postProperties, NOTION_CONFIG) {
return postProperties.slug
}
let fullPrefix = ''
const allSlugPatterns = siteConfig(
'POST_URL_PREFIX',
'',
NOTION_CONFIG
).split('/')

let allSlugPatterns = NOTION_CONFIG?.POST_URL_PREFIX
if (allSlugPatterns === undefined) {
allSlugPatterns = siteConfig('POST_URL_PREFIX', '', NOTION_CONFIG).split(
'/'
)
} else {
allSlugPatterns = allSlugPatterns.split('/')
}
const POST_URL_PREFIX_MAPPING_CATEGORY = siteConfig(
'POST_URL_PREFIX_MAPPING_CATEGORY',
{},
Expand Down
125 changes: 63 additions & 62 deletions lib/notion/getPostBlocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@ import { deepClone, delay } from '../utils'
* @param {*} slice
* @returns
*/
export async function getPage(id, from, slice) {
const cacheKey = 'page_block_' + id
export async function getPage(id, from = null, slice) {
const cacheKey = `page_block_${id}`
let pageBlock = await getDataFromCache(cacheKey)
if (pageBlock) {
// console.log('[API<<--缓存]', `from:${from}`, cacheKey)
return filterPostBlocks(id, pageBlock, slice)
// console.debug('[API<<--缓存]', `from:${from}`, cacheKey)
return convertNotionBlocksToPost(id, pageBlock, slice)
}

// 抓取最新数据
pageBlock = await getPageWithRetry(id, from)

if (pageBlock) {
await setDataToCache(cacheKey, pageBlock)
return filterPostBlocks(id, pageBlock, slice)
return convertNotionBlocksToPost(id, pageBlock, slice)
}
return pageBlock
}
Expand Down Expand Up @@ -69,7 +70,7 @@ export async function getPageWithRetry(id, from, retryAttempts = 3) {
}

/**
* 获取到的页面BLOCK特殊处理
* Notion页面BLOCK格式化处理
* 1.删除冗余字段
* 2.比如文件、视频、音频、url格式化
* 3.代码块等元素兼容
Expand All @@ -78,72 +79,72 @@ export async function getPageWithRetry(id, from, retryAttempts = 3) {
* @param {*} slice 截取数量
* @returns
*/
function filterPostBlocks(id, blockMap, slice) {
function convertNotionBlocksToPost(id, blockMap, slice) {
const clonePageBlock = deepClone(blockMap)
let count = 0
const blocksToProcess = Object.keys(clonePageBlock?.block || {})

// 循环遍历文档的每个block
for (let i = 0; i < blocksToProcess.length; i++) {
const blockId = blocksToProcess[i]
const b = clonePageBlock?.block[blockId]

if (slice && slice > 0 && count > slice) {
delete clonePageBlock?.block[blockId]
continue
}

// 当BlockId等于PageId时移除
if (b?.value?.id === id) {
// 此block含有敏感信息
delete b?.value?.properties
continue
}

count++

if (b?.value?.type === 'sync_block' && b?.value?.children) {
const childBlocks = b.value.children
// 移除同步块
delete clonePageBlock.block[blockId]
// 用子块替代同步块
childBlocks.forEach((childBlock, index) => {
const newBlockId = `${blockId}_child_${index}`
clonePageBlock.block[newBlockId] = childBlock
blocksToProcess.splice(i + index + 1, 0, newBlockId)
})
// 重新处理新加入的子块
i--
continue
}

// 处理 c++、c#、汇编等语言名字映射
if (b?.value?.type === 'code') {
if (b?.value?.properties?.language?.[0][0] === 'C++') {
b.value.properties.language[0][0] = 'cpp'
const blockId = blocksToProcess[i]
const b = clonePageBlock?.block[blockId]

if (slice && slice > 0 && count > slice) {
delete clonePageBlock?.block[blockId]
continue
}
if (b?.value?.properties?.language?.[0][0] === 'C#') {
b.value.properties.language[0][0] = 'csharp'

// 当BlockId等于PageId时移除
if (b?.value?.id === id) {
// 此block含有敏感信息
delete b?.value?.properties
continue
}
if (b?.value?.properties?.language?.[0][0] === 'Assembly') {
b.value.properties.language[0][0] = 'asm6502'

count++

if (b?.value?.type === 'sync_block' && b?.value?.children) {
const childBlocks = b.value.children
// 移除同步块
delete clonePageBlock.block[blockId]
// 用子块替代同步块
childBlocks.forEach((childBlock, index) => {
const newBlockId = `${blockId}_child_${index}`
clonePageBlock.block[newBlockId] = childBlock
blocksToProcess.splice(i + index + 1, 0, newBlockId)
})
// 重新处理新加入的子块
i--
continue
}

// 处理 c++、c#、汇编等语言名字映射
if (b?.value?.type === 'code') {
if (b?.value?.properties?.language?.[0][0] === 'C++') {
b.value.properties.language[0][0] = 'cpp'
}
if (b?.value?.properties?.language?.[0][0] === 'C#') {
b.value.properties.language[0][0] = 'csharp'
}
if (b?.value?.properties?.language?.[0][0] === 'Assembly') {
b.value.properties.language[0][0] = 'asm6502'
}
}

// 如果是文件,或嵌入式PDF,需要重新加密签名
if (
(b?.value?.type === 'file' ||
b?.value?.type === 'pdf' ||
b?.value?.type === 'video' ||
b?.value?.type === 'audio') &&
b?.value?.properties?.source?.[0][0] &&
b?.value?.properties?.source?.[0][0].indexOf('amazonaws.com') > 0
) {
const oldUrl = b?.value?.properties?.source?.[0][0]
const newUrl = `https://notion.so/signed/${encodeURIComponent(oldUrl)}?table=block&id=${b?.value?.id}`
b.value.properties.source[0][0] = newUrl
}
}

// 如果是文件,或嵌入式PDF,需要重新加密签名
if (
(b?.value?.type === 'file' ||
b?.value?.type === 'pdf' ||
b?.value?.type === 'video' ||
b?.value?.type === 'audio') &&
b?.value?.properties?.source?.[0][0] &&
b?.value?.properties?.source?.[0][0].indexOf('amazonaws.com') > 0
) {
const oldUrl = b?.value?.properties?.source?.[0][0]
const newUrl = `https://notion.so/signed/${encodeURIComponent(oldUrl)}?table=block&id=${b?.value?.id}`
b.value.properties.source[0][0] = newUrl
}
}

// 去掉不用的字段
if (id === BLOG.NOTION_PAGE_ID) {
Expand Down
47 changes: 26 additions & 21 deletions lib/sitemap.xml.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@

import fs from 'fs'
import BLOG from '@/blog.config'
import fs from 'fs'

export async function generateSitemapXml({ allPages }) {
const urls = [{
loc: `${BLOG.LINK}`,
lastmod: new Date().toISOString().split('T')[0],
changefreq: 'daily'
}, {
loc: `${BLOG.LINK}/archive`,
lastmod: new Date().toISOString().split('T')[0],
changefreq: 'daily'
}, {
loc: `${BLOG.LINK}/category`,
lastmod: new Date().toISOString().split('T')[0],
changefreq: 'daily'
}, {
loc: `${BLOG.LINK}/tag`,
lastmod: new Date().toISOString().split('T')[0],
changefreq: 'daily'
}]

const urls = [
{
loc: `${BLOG.LINK}`,
lastmod: new Date().toISOString().split('T')[0],
changefreq: 'daily'
},
{
loc: `${BLOG.LINK}/archive`,
lastmod: new Date().toISOString().split('T')[0],
changefreq: 'daily'
},
{
loc: `${BLOG.LINK}/category`,
lastmod: new Date().toISOString().split('T')[0],
changefreq: 'daily'
},
{
loc: `${BLOG.LINK}/tag`,
lastmod: new Date().toISOString().split('T')[0],
changefreq: 'daily'
}
]
// 循环页面生成
allPages?.forEach(post => {
const slugWithoutLeadingSlash = post?.slug?.startsWith('/') ? post?.slug?.slice(1) : post.slug
const slugWithoutLeadingSlash = post?.slug?.startsWith('/')
? post?.slug?.slice(1)
: post.slug
urls.push({
loc: `${BLOG.LINK}/${slugWithoutLeadingSlash}`,
lastmod: new Date(post?.publishDay).toISOString().split('T')[0],
Expand Down
2 changes: 1 addition & 1 deletion themes/starter/components/FAQ.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const FAQ = () => {
return (
<>
{/* <!-- ====== FAQ Section Start --> */}
<section className='relative z-20 overflow-hidden bg-white pb-8 pt-20 dark:bg-dark lg:pb-[50px] lg:pt-[120px]'>
<section className='relative overflow-hidden bg-white pb-8 pt-20 dark:bg-dark lg:pb-[50px] lg:pt-[120px]'>
<div className='container mx-auto'>
<div className='-mx-4 flex flex-wrap'>
<div className='w-full px-4'>
Expand Down
34 changes: 18 additions & 16 deletions themes/starter/components/Hero.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/* eslint-disable @next/next/no-img-element */
import LazyImage from '@/components/LazyImage'
import { siteConfig } from '@/lib/config'
import CONFIG from '../config'

/**
* 英雄大图区块
*/
export const Hero = props => {
const CONFIG = props?.NOTION_CONFIG || CONFIG
const config = props?.NOTION_CONFIG || CONFIG
return (
<>
{/* <!-- ====== Hero Section Start --> */}
Expand All @@ -20,49 +22,49 @@ export const Hero = props => {
data-wow-delay='.2s'>
{/* 主标题 */}
<h1 className='mb-6 text-3xl font-bold leading-snug text-white sm:text-4xl sm:leading-snug lg:text-5xl lg:leading-[1.2]'>
{siteConfig('STARTER_HERO_TITLE_1', null, CONFIG)}
{siteConfig('STARTER_HERO_TITLE_1', null, config)}
</h1>
{/* 次标题 */}
<p className='mx-auto mb-9 max-w-[600px] text-base font-medium text-white sm:text-lg sm:leading-[1.44]'>
{siteConfig('STARTER_HERO_TITLE_2', null, CONFIG)}
{siteConfig('STARTER_HERO_TITLE_2', null, config)}
</p>
{/* 按钮组 */}
<ul className='mb-10 flex flex-wrap items-center justify-center gap-5'>
{siteConfig('STARTER_HERO_BUTTON_1_TEXT', null, CONFIG) && (
{siteConfig('STARTER_HERO_BUTTON_1_TEXT', null, config) && (
<li>
<a
href={siteConfig('STARTER_HERO_BUTTON_1_URL')}
className='inline-flex items-center justify-center rounded-md bg-white px-7 py-[14px] text-center text-base font-medium text-dark shadow-1 transition duration-300 ease-in-out hover:bg-gray-2 hover:text-body-color'>
{siteConfig('STARTER_HERO_BUTTON_1_TEXT', null, CONFIG)}
{siteConfig('STARTER_HERO_BUTTON_1_TEXT', null, config)}
</a>
</li>
)}
{siteConfig('STARTER_HERO_BUTTON_2_TEXT', null, CONFIG) && (
{siteConfig('STARTER_HERO_BUTTON_2_TEXT', null, config) && (
<li>
<a
href={siteConfig(
'STARTER_HERO_BUTTON_2_URL',
null,
CONFIG
config
)}
target='_blank'
className='flex items-center rounded-md bg-white/[0.12] px-6 py-[14px] text-base font-medium text-white transition duration-300 ease-in-out hover:bg-white hover:text-dark'
rel='noreferrer'>
{siteConfig(
'STARTER_HERO_BUTTON_2_ICON',
null,
CONFIG
config
) && (
<img
className='mr-4'
src={siteConfig(
'STARTER_HERO_BUTTON_2_ICON',
null,
CONFIG
config
)}
/>
)}
{siteConfig('STARTER_HERO_BUTTON_2_TEXT', null, CONFIG)}
{siteConfig('STARTER_HERO_BUTTON_2_TEXT', null, config)}
</a>
</li>
)}
Expand All @@ -71,7 +73,7 @@ export const Hero = props => {
</div>

{/* 产品预览图片 */}
{siteConfig('STARTER_HERO_PREVIEW_IMAGE', null, CONFIG) && (
{siteConfig('STARTER_HERO_PREVIEW_IMAGE', null, config) && (
<div className='w-full px-4'>
<div
className='wow fadeInUp relative z-10 mx-auto max-w-[845px]'
Expand All @@ -82,10 +84,10 @@ export const Hero = props => {
src={siteConfig(
'STARTER_HERO_PREVIEW_IMAGE',
null,
CONFIG
config
)}
alt={siteConfig('TITLE', null, CONFIG)}
title={siteConfig('TITLE', null, CONFIG)}
alt={siteConfig('TITLE', null, config)}
title={siteConfig('TITLE', null, config)}
className='mx-auto max-w-full rounded-t-xl rounded-tr-xl'
/>
</div>
Expand All @@ -104,15 +106,15 @@ export const Hero = props => {
</div>
</div>
{/* 横幅图片 */}
{siteConfig('STARTER_HERO_BANNER_IMAGE', null, CONFIG) && (
{siteConfig('STARTER_HERO_BANNER_IMAGE', null, config) && (
<div className='container'>
<LazyImage
priority
className='w-full'
src={siteConfig(
'STARTER_HERO_BANNER_IMAGE',
null,
CONFIG
config
)}></LazyImage>
</div>
)}
Expand Down
2 changes: 1 addition & 1 deletion themes/starter/components/Pricing.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const Pricing = () => {
{/* <!-- ====== Pricing Section Start --> */}
<section
id='pricing'
className='relative z-20 overflow-hidden bg-white pb-12 pt-20 dark:bg-dark lg:pb-[90px] lg:pt-[120px]'>
className='relative overflow-hidden bg-white pb-12 pt-20 dark:bg-dark lg:pb-[90px] lg:pt-[120px]'>
<div className='container mx-auto'>
<div className='-mx-4 flex flex-wrap'>
<div className='w-full px-4'>
Expand Down

0 comments on commit 1ec3ef5

Please sign in to comment.