fix: 无法读取undefined的'split'属性错误

This commit is contained in:
afoim
2025-05-31 16:55:50 +08:00
parent c49d195e5f
commit 590d8580dc
2 changed files with 12 additions and 6 deletions

View File

@@ -14,7 +14,8 @@ import remarkMath from "remark-math";
import remarkSectionize from "remark-sectionize"; import remarkSectionize from "remark-sectionize";
import { AdmonitionComponent } from "./src/plugins/rehype-component-admonition.mjs"; import { AdmonitionComponent } from "./src/plugins/rehype-component-admonition.mjs";
import { GithubCardComponent } from "./src/plugins/rehype-component-github-card.mjs"; import { GithubCardComponent } from "./src/plugins/rehype-component-github-card.mjs";
import { rehypeImageFallback } from "./src/plugins/rehype-image-fallback.mjs"; import rehypeImageFallback from "./src/plugins/rehype-image-fallback.mjs";
import { siteConfig, imageFallbackConfig } from './src/config.ts';
import { parseDirectiveNode } from "./src/plugins/remark-directive-rehype.js"; import { parseDirectiveNode } from "./src/plugins/remark-directive-rehype.js";
import { remarkExcerpt } from "./src/plugins/remark-excerpt.js"; import { remarkExcerpt } from "./src/plugins/remark-excerpt.js";
import { remarkReadingTime } from "./src/plugins/remark-reading-time.mjs"; import { remarkReadingTime } from "./src/plugins/remark-reading-time.mjs";
@@ -66,7 +67,7 @@ export default defineConfig({
rehypePlugins: [ rehypePlugins: [
rehypeKatex, rehypeKatex,
rehypeSlug, rehypeSlug,
rehypeImageFallback, [rehypeImageFallback, imageFallbackConfig],
[ [
rehypeComponents, rehypeComponents,
{ {

View File

@@ -1,16 +1,21 @@
import { visit } from 'unist-util-visit'; import { visit } from 'unist-util-visit';
import { imageFallbackConfig } from '../config.ts';
export default function rehypeImageFallback() { export default function rehypeImageFallback(options = {}) {
const {
enable = true,
originalDomain = 'r2.afo.im',
fallbackDomain = 'pub-d433ca7edaa74994b3d7c40a7fd7d9ac.r2.dev'
} = options;
return (tree) => { return (tree) => {
visit(tree, 'element', (node) => { visit(tree, 'element', (node) => {
if (node.tagName === 'img' && node.properties && node.properties.src) { if (node.tagName === 'img' && node.properties && node.properties.src) {
const src = node.properties.src; const src = node.properties.src;
// 检查是否启用回退功能并且是来自指定域名的图片 // 检查是否启用回退功能并且是来自指定域名的图片
if (imageFallbackConfig.enable && typeof src === 'string' && src.includes(imageFallbackConfig.originalDomain)) { if (enable && typeof src === 'string' && src.includes(originalDomain)) {
// 生成备用 URL // 生成备用 URL
const fallbackSrc = src.replace(imageFallbackConfig.originalDomain, imageFallbackConfig.fallbackDomain); const fallbackSrc = src.replace(originalDomain, fallbackDomain);
// 添加 onerror 属性 // 添加 onerror 属性
node.properties.onerror = `this.onerror=null; this.src='${fallbackSrc}';`; node.properties.onerror = `this.onerror=null; this.src='${fallbackSrc}';`;