File size: 3,999 Bytes
1b72d7e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
import BLOG from '@/blog.config'
import { siteConfig } from '../config'
/**
* 图片映射
* 1. 如果是 /xx.xx 相对路径格式,则转化为 完整notion域名图片
* 2. 如果是 bookmark类型的block 图片封面无需处理
* @param {*} img
* @param {*} value
* @returns
*/
const mapImgUrl = (img, block, type = 'block', from = 'post') => {
if (!img) {
return null
}
let ret = null
// 相对目录,则视为notion的自带图片
if (img.startsWith('/')) {
ret = BLOG.NOTION_HOST + img
} else {
ret = img
}
// Notion 图床转换为永久地址
const isNotionSignImg = ret.indexOf('https://www.notion.so/image') !== 0 && (ret.indexOf('secure.notion-static.com') > 0 || ret.indexOf('prod-files-secure') > 0)
const isImgBlock = BLOG.IMG_URL_TYPE === 'Notion' || type !== 'block'
if (isNotionSignImg && isImgBlock) {
ret = BLOG.NOTION_HOST + '/image/' + encodeURIComponent(ret) + '?table=' + type + '&id=' + block.id
}
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优化,确保每一篇文章的图片url唯一
if (ret && ret.length > 4) {
// 图片接口拼接唯一识别参数,防止请求的图片被缓,而导致随机结果相同
const separator = ret.includes('?') ? '&' : '?'
ret = `${ret.trim()}${separator}t=${block.id}`
}
}
// 统一压缩图片
if (from === 'pageCoverThumbnail' || block.type === 'image' || block.type === 'page') {
const width = block?.format?.block_width
ret = compressImage(ret, width)
}
return ret
}
/**
* 是否是emoji图标
* @param {*} str
* @returns
*/
function isEmoji(str) {
const emojiRegex = /[\u{1F300}-\u{1F6FF}\u{1F1E0}-\u{1F1FF}\u{2600}-\u{26FF}\u{2700}-\u{27BF}\u{1F900}-\u{1F9FF}\u{1F018}-\u{1F270}\u{238C}\u{2B06}\u{2B07}\u{2B05}\u{27A1}\u{2194}-\u{2199}\u{2194}\u{21A9}\u{21AA}\u{2934}\u{2935}\u{25AA}\u{25AB}\u{25FE}\u{25FD}\u{25FB}\u{25FC}\u{25B6}\u{25C0}\u{1F200}-\u{1F251}]/u;
return emojiRegex.test(str);
}
/**
* 压缩图片
* 1. Notion图床可以通过指定url-query参数来压缩裁剪图片 例如 ?xx=xx&width=400
* 2. UnPlash 图片可以通过api q=50 控制压缩质量 width=400 控制图片尺寸
* @param {*} image
*/
const compressImage = (image, width, quality = 50, fmt = 'webp') => {
if (!image || image.indexOf('http') !== 0) {
return image
}
if (!width || width === 0) {
width = siteConfig('IMAGE_COMPRESS_WIDTH')
}
// 将URL解析为一个对象
const urlObj = new URL(image)
// 获取URL参数
const params = new URLSearchParams(urlObj.search)
// Notion图床
if (image.indexOf(BLOG.NOTION_HOST) === 0 && image.indexOf('amazonaws.com') > 0) {
params.set('width', width)
params.set('cache', 'v2')
// 生成新的URL
urlObj.search = params.toString()
return urlObj.toString()
} else if (image.indexOf('https://images.unsplash.com/') === 0) {
// 压缩unsplash图片
// 将q参数的值替换
params.set('q', quality)
// 尺寸
params.set('width', width)
// 格式
params.set('fmt', fmt)
params.set('fm', fmt)
// 生成新的URL
urlObj.search = params.toString()
return urlObj.toString()
} else if (image.indexOf('https://your_picture_bed') === 0) {
// 此处还可以添加您的自定义图传的封面图压缩参数。
// .e.g
return 'do_somethin_here'
}
return image
}
export { mapImgUrl, compressImage }
|