Skip to content

Commit

Permalink
[feat] 支持随机图片 url 统一配置
Browse files Browse the repository at this point in the history
  • Loading branch information
jiashu1024 committed Nov 10, 2023
1 parent 56c35d2 commit 4c483a7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
8 changes: 8 additions & 0 deletions blog.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ const BLOG = {

BLOG_FAVICON: process.env.NEXT_PUBLIC_FAVICON || '/favicon.ico', // blog favicon 配置, 默认使用 /public/favicon.ico,支持在线图片,如 https://img.imesong.com/favicon.png


RANDOM_IMAGE_URL: process.env.NEXT_PUBLIC_RANDOM_IMAGE_URL || '', // 随机图片API,如果未配置下面的关键字,主页封面,头像,文章封面图都会被替换为随机图片
RANDOM_IMAGE_REPLACE_TEXT: process.env.NEXT_PUBLIC_RANDOM_IMAGE_NOT_REPLACE_TEXT || 'images.unsplash.com', // 触发替换图片的 url 关键字(多个支持用英文逗号分开),只有图片地址中包含此关键字才会替换为上方随机图片url
// eg: images.unsplash.com(notion图床的所有图片都会替换),如果你在 notion 里已经添加了一个随机图片 url,恰巧那个服务跑路或者挂掉,想一键切换所有配图可以将该 url 配置在这里
// 默认下会将你上传到 notion的主页封面图和头像也给替换,建议将主页封面图和头像放在其他图床,在 notion 里配置 link 即可。



// START ************网站字体*****************

FONT_STYLE: process.env.NEXT_PUBLIC_FONT_STYLE || 'font-sans', // ['font-serif','font-sans'] 两种可选,分别是衬线和无衬线: 参考 https://www.jianshu.com/p/55e410bd2115
Expand Down
20 changes: 20 additions & 0 deletions lib/notion/mapImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,26 @@ const mapImgUrl = (img, block, type = 'block', from) => {
}

if (!isEmoji(ret) && ret.indexOf('notion.so/images/page-cover') < 0) {
if (BLOG.RANDOM_IMAGE_URL) {
// 只有配置了随机图片接口,才会替换图片
const texts = BLOG.RANDOM_IMAGE_REPLACE_TEXT
let isReplace = false;
if (texts) {
const textArr = texts.split(',')
// 判断是否包含替换的文本
textArr.forEach(text => {
if (ret.indexOf(text) > -1) {
isReplace = true
}
})
} else {
isReplace = true
}

if (isReplace) {
ret = BLOG.RANDOM_IMAGE_URL
}
}
// 随机图片接口优化 防止因url一致而随机结果相同
const separator = ret.includes('?') ? '&' : '?'
// 拼接唯一识别参数,防止请求的图片被缓存
Expand Down

0 comments on commit 4c483a7

Please sign in to comment.