mirror of
https://github.com/JimLiu/baoyu-skills.git
synced 2026-07-12 05:51:44 +08:00
fix: preserve weibo image metadata in shared md package
This commit is contained in:
@@ -7,6 +7,7 @@ import path from "node:path";
|
||||
export interface ImagePlaceholder {
|
||||
originalPath: string;
|
||||
placeholder: string;
|
||||
alt?: string;
|
||||
}
|
||||
|
||||
export interface ResolvedImageInfo extends ImagePlaceholder {
|
||||
@@ -23,9 +24,10 @@ export function replaceMarkdownImagesWithPlaceholders(
|
||||
const images: ImagePlaceholder[] = [];
|
||||
let imageCounter = 0;
|
||||
|
||||
const rewritten = markdown.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, (_match, _alt, src) => {
|
||||
const rewritten = markdown.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, (_match, alt, src) => {
|
||||
const placeholder = `${placeholderPrefix}${++imageCounter}`;
|
||||
images.push({
|
||||
alt,
|
||||
originalPath: src,
|
||||
placeholder,
|
||||
});
|
||||
@@ -101,11 +103,10 @@ export async function resolveImagePath(
|
||||
return localPath;
|
||||
}
|
||||
|
||||
if (path.isAbsolute(imagePath)) {
|
||||
return imagePath;
|
||||
}
|
||||
|
||||
return path.resolve(baseDir, imagePath);
|
||||
const resolved = path.isAbsolute(imagePath)
|
||||
? imagePath
|
||||
: path.resolve(baseDir, imagePath);
|
||||
return resolveLocalWithFallback(resolved, logLabel);
|
||||
}
|
||||
|
||||
export async function resolveContentImages(
|
||||
@@ -125,3 +126,31 @@ export async function resolveContentImages(
|
||||
|
||||
return resolved;
|
||||
}
|
||||
|
||||
function resolveLocalWithFallback(resolved: string, logLabel: string): string {
|
||||
if (fs.existsSync(resolved)) {
|
||||
return resolved;
|
||||
}
|
||||
|
||||
const ext = path.extname(resolved);
|
||||
const base = ext ? resolved.slice(0, -ext.length) : resolved;
|
||||
const alternatives = [
|
||||
`${base}.webp`,
|
||||
`${base}.jpg`,
|
||||
`${base}.jpeg`,
|
||||
`${base}.png`,
|
||||
`${base}.gif`,
|
||||
`${base}_original.png`,
|
||||
`${base}_original.jpg`,
|
||||
].filter((candidate) => candidate !== resolved);
|
||||
|
||||
for (const alternative of alternatives) {
|
||||
if (!fs.existsSync(alternative)) continue;
|
||||
console.error(
|
||||
`[${logLabel}] Image fallback: ${path.basename(resolved)} -> ${path.basename(alternative)}`,
|
||||
);
|
||||
return alternative;
|
||||
}
|
||||
|
||||
return resolved;
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import path from "node:path";
|
||||
export interface ImagePlaceholder {
|
||||
originalPath: string;
|
||||
placeholder: string;
|
||||
alt?: string;
|
||||
}
|
||||
|
||||
export interface ResolvedImageInfo extends ImagePlaceholder {
|
||||
@@ -23,9 +24,10 @@ export function replaceMarkdownImagesWithPlaceholders(
|
||||
const images: ImagePlaceholder[] = [];
|
||||
let imageCounter = 0;
|
||||
|
||||
const rewritten = markdown.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, (_match, _alt, src) => {
|
||||
const rewritten = markdown.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, (_match, alt, src) => {
|
||||
const placeholder = `${placeholderPrefix}${++imageCounter}`;
|
||||
images.push({
|
||||
alt,
|
||||
originalPath: src,
|
||||
placeholder,
|
||||
});
|
||||
@@ -101,11 +103,10 @@ export async function resolveImagePath(
|
||||
return localPath;
|
||||
}
|
||||
|
||||
if (path.isAbsolute(imagePath)) {
|
||||
return imagePath;
|
||||
}
|
||||
|
||||
return path.resolve(baseDir, imagePath);
|
||||
const resolved = path.isAbsolute(imagePath)
|
||||
? imagePath
|
||||
: path.resolve(baseDir, imagePath);
|
||||
return resolveLocalWithFallback(resolved, logLabel);
|
||||
}
|
||||
|
||||
export async function resolveContentImages(
|
||||
@@ -125,3 +126,31 @@ export async function resolveContentImages(
|
||||
|
||||
return resolved;
|
||||
}
|
||||
|
||||
function resolveLocalWithFallback(resolved: string, logLabel: string): string {
|
||||
if (fs.existsSync(resolved)) {
|
||||
return resolved;
|
||||
}
|
||||
|
||||
const ext = path.extname(resolved);
|
||||
const base = ext ? resolved.slice(0, -ext.length) : resolved;
|
||||
const alternatives = [
|
||||
`${base}.webp`,
|
||||
`${base}.jpg`,
|
||||
`${base}.jpeg`,
|
||||
`${base}.png`,
|
||||
`${base}.gif`,
|
||||
`${base}_original.png`,
|
||||
`${base}_original.jpg`,
|
||||
].filter((candidate) => candidate !== resolved);
|
||||
|
||||
for (const alternative of alternatives) {
|
||||
if (!fs.existsSync(alternative)) continue;
|
||||
console.error(
|
||||
`[${logLabel}] Image fallback: ${path.basename(resolved)} -> ${path.basename(alternative)}`,
|
||||
);
|
||||
return alternative;
|
||||
}
|
||||
|
||||
return resolved;
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import path from "node:path";
|
||||
export interface ImagePlaceholder {
|
||||
originalPath: string;
|
||||
placeholder: string;
|
||||
alt?: string;
|
||||
}
|
||||
|
||||
export interface ResolvedImageInfo extends ImagePlaceholder {
|
||||
@@ -23,9 +24,10 @@ export function replaceMarkdownImagesWithPlaceholders(
|
||||
const images: ImagePlaceholder[] = [];
|
||||
let imageCounter = 0;
|
||||
|
||||
const rewritten = markdown.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, (_match, _alt, src) => {
|
||||
const rewritten = markdown.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, (_match, alt, src) => {
|
||||
const placeholder = `${placeholderPrefix}${++imageCounter}`;
|
||||
images.push({
|
||||
alt,
|
||||
originalPath: src,
|
||||
placeholder,
|
||||
});
|
||||
@@ -101,11 +103,10 @@ export async function resolveImagePath(
|
||||
return localPath;
|
||||
}
|
||||
|
||||
if (path.isAbsolute(imagePath)) {
|
||||
return imagePath;
|
||||
}
|
||||
|
||||
return path.resolve(baseDir, imagePath);
|
||||
const resolved = path.isAbsolute(imagePath)
|
||||
? imagePath
|
||||
: path.resolve(baseDir, imagePath);
|
||||
return resolveLocalWithFallback(resolved, logLabel);
|
||||
}
|
||||
|
||||
export async function resolveContentImages(
|
||||
@@ -125,3 +126,31 @@ export async function resolveContentImages(
|
||||
|
||||
return resolved;
|
||||
}
|
||||
|
||||
function resolveLocalWithFallback(resolved: string, logLabel: string): string {
|
||||
if (fs.existsSync(resolved)) {
|
||||
return resolved;
|
||||
}
|
||||
|
||||
const ext = path.extname(resolved);
|
||||
const base = ext ? resolved.slice(0, -ext.length) : resolved;
|
||||
const alternatives = [
|
||||
`${base}.webp`,
|
||||
`${base}.jpg`,
|
||||
`${base}.jpeg`,
|
||||
`${base}.png`,
|
||||
`${base}.gif`,
|
||||
`${base}_original.png`,
|
||||
`${base}_original.jpg`,
|
||||
].filter((candidate) => candidate !== resolved);
|
||||
|
||||
for (const alternative of alternatives) {
|
||||
if (!fs.existsSync(alternative)) continue;
|
||||
console.error(
|
||||
`[${logLabel}] Image fallback: ${path.basename(resolved)} -> ${path.basename(alternative)}`,
|
||||
);
|
||||
return alternative;
|
||||
}
|
||||
|
||||
return resolved;
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ interface ImageInfo {
|
||||
placeholder: string;
|
||||
localPath: string;
|
||||
originalPath: string;
|
||||
alt?: string;
|
||||
}
|
||||
|
||||
interface ParsedMarkdown {
|
||||
|
||||
@@ -7,6 +7,7 @@ import path from "node:path";
|
||||
export interface ImagePlaceholder {
|
||||
originalPath: string;
|
||||
placeholder: string;
|
||||
alt?: string;
|
||||
}
|
||||
|
||||
export interface ResolvedImageInfo extends ImagePlaceholder {
|
||||
@@ -23,9 +24,10 @@ export function replaceMarkdownImagesWithPlaceholders(
|
||||
const images: ImagePlaceholder[] = [];
|
||||
let imageCounter = 0;
|
||||
|
||||
const rewritten = markdown.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, (_match, _alt, src) => {
|
||||
const rewritten = markdown.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, (_match, alt, src) => {
|
||||
const placeholder = `${placeholderPrefix}${++imageCounter}`;
|
||||
images.push({
|
||||
alt,
|
||||
originalPath: src,
|
||||
placeholder,
|
||||
});
|
||||
@@ -101,11 +103,10 @@ export async function resolveImagePath(
|
||||
return localPath;
|
||||
}
|
||||
|
||||
if (path.isAbsolute(imagePath)) {
|
||||
return imagePath;
|
||||
}
|
||||
|
||||
return path.resolve(baseDir, imagePath);
|
||||
const resolved = path.isAbsolute(imagePath)
|
||||
? imagePath
|
||||
: path.resolve(baseDir, imagePath);
|
||||
return resolveLocalWithFallback(resolved, logLabel);
|
||||
}
|
||||
|
||||
export async function resolveContentImages(
|
||||
@@ -125,3 +126,31 @@ export async function resolveContentImages(
|
||||
|
||||
return resolved;
|
||||
}
|
||||
|
||||
function resolveLocalWithFallback(resolved: string, logLabel: string): string {
|
||||
if (fs.existsSync(resolved)) {
|
||||
return resolved;
|
||||
}
|
||||
|
||||
const ext = path.extname(resolved);
|
||||
const base = ext ? resolved.slice(0, -ext.length) : resolved;
|
||||
const alternatives = [
|
||||
`${base}.webp`,
|
||||
`${base}.jpg`,
|
||||
`${base}.jpeg`,
|
||||
`${base}.png`,
|
||||
`${base}.gif`,
|
||||
`${base}_original.png`,
|
||||
`${base}_original.jpg`,
|
||||
].filter((candidate) => candidate !== resolved);
|
||||
|
||||
for (const alternative of alternatives) {
|
||||
if (!fs.existsSync(alternative)) continue;
|
||||
console.error(
|
||||
`[${logLabel}] Image fallback: ${path.basename(resolved)} -> ${path.basename(alternative)}`,
|
||||
);
|
||||
return alternative;
|
||||
}
|
||||
|
||||
return resolved;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user