mirror of
https://github.com/JimLiu/baoyu-skills.git
synced 2026-07-31 22:29:48 +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 {
|
export interface ImagePlaceholder {
|
||||||
originalPath: string;
|
originalPath: string;
|
||||||
placeholder: string;
|
placeholder: string;
|
||||||
|
alt?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ResolvedImageInfo extends ImagePlaceholder {
|
export interface ResolvedImageInfo extends ImagePlaceholder {
|
||||||
@@ -23,9 +24,10 @@ export function replaceMarkdownImagesWithPlaceholders(
|
|||||||
const images: ImagePlaceholder[] = [];
|
const images: ImagePlaceholder[] = [];
|
||||||
let imageCounter = 0;
|
let imageCounter = 0;
|
||||||
|
|
||||||
const rewritten = markdown.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, (_match, _alt, src) => {
|
const rewritten = markdown.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, (_match, alt, src) => {
|
||||||
const placeholder = `${placeholderPrefix}${++imageCounter}`;
|
const placeholder = `${placeholderPrefix}${++imageCounter}`;
|
||||||
images.push({
|
images.push({
|
||||||
|
alt,
|
||||||
originalPath: src,
|
originalPath: src,
|
||||||
placeholder,
|
placeholder,
|
||||||
});
|
});
|
||||||
@@ -101,11 +103,10 @@ export async function resolveImagePath(
|
|||||||
return localPath;
|
return localPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (path.isAbsolute(imagePath)) {
|
const resolved = path.isAbsolute(imagePath)
|
||||||
return imagePath;
|
? imagePath
|
||||||
}
|
: path.resolve(baseDir, imagePath);
|
||||||
|
return resolveLocalWithFallback(resolved, logLabel);
|
||||||
return path.resolve(baseDir, imagePath);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function resolveContentImages(
|
export async function resolveContentImages(
|
||||||
@@ -125,3 +126,31 @@ export async function resolveContentImages(
|
|||||||
|
|
||||||
return resolved;
|
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 {
|
export interface ImagePlaceholder {
|
||||||
originalPath: string;
|
originalPath: string;
|
||||||
placeholder: string;
|
placeholder: string;
|
||||||
|
alt?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ResolvedImageInfo extends ImagePlaceholder {
|
export interface ResolvedImageInfo extends ImagePlaceholder {
|
||||||
@@ -23,9 +24,10 @@ export function replaceMarkdownImagesWithPlaceholders(
|
|||||||
const images: ImagePlaceholder[] = [];
|
const images: ImagePlaceholder[] = [];
|
||||||
let imageCounter = 0;
|
let imageCounter = 0;
|
||||||
|
|
||||||
const rewritten = markdown.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, (_match, _alt, src) => {
|
const rewritten = markdown.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, (_match, alt, src) => {
|
||||||
const placeholder = `${placeholderPrefix}${++imageCounter}`;
|
const placeholder = `${placeholderPrefix}${++imageCounter}`;
|
||||||
images.push({
|
images.push({
|
||||||
|
alt,
|
||||||
originalPath: src,
|
originalPath: src,
|
||||||
placeholder,
|
placeholder,
|
||||||
});
|
});
|
||||||
@@ -101,11 +103,10 @@ export async function resolveImagePath(
|
|||||||
return localPath;
|
return localPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (path.isAbsolute(imagePath)) {
|
const resolved = path.isAbsolute(imagePath)
|
||||||
return imagePath;
|
? imagePath
|
||||||
}
|
: path.resolve(baseDir, imagePath);
|
||||||
|
return resolveLocalWithFallback(resolved, logLabel);
|
||||||
return path.resolve(baseDir, imagePath);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function resolveContentImages(
|
export async function resolveContentImages(
|
||||||
@@ -125,3 +126,31 @@ export async function resolveContentImages(
|
|||||||
|
|
||||||
return resolved;
|
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 {
|
export interface ImagePlaceholder {
|
||||||
originalPath: string;
|
originalPath: string;
|
||||||
placeholder: string;
|
placeholder: string;
|
||||||
|
alt?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ResolvedImageInfo extends ImagePlaceholder {
|
export interface ResolvedImageInfo extends ImagePlaceholder {
|
||||||
@@ -23,9 +24,10 @@ export function replaceMarkdownImagesWithPlaceholders(
|
|||||||
const images: ImagePlaceholder[] = [];
|
const images: ImagePlaceholder[] = [];
|
||||||
let imageCounter = 0;
|
let imageCounter = 0;
|
||||||
|
|
||||||
const rewritten = markdown.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, (_match, _alt, src) => {
|
const rewritten = markdown.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, (_match, alt, src) => {
|
||||||
const placeholder = `${placeholderPrefix}${++imageCounter}`;
|
const placeholder = `${placeholderPrefix}${++imageCounter}`;
|
||||||
images.push({
|
images.push({
|
||||||
|
alt,
|
||||||
originalPath: src,
|
originalPath: src,
|
||||||
placeholder,
|
placeholder,
|
||||||
});
|
});
|
||||||
@@ -101,11 +103,10 @@ export async function resolveImagePath(
|
|||||||
return localPath;
|
return localPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (path.isAbsolute(imagePath)) {
|
const resolved = path.isAbsolute(imagePath)
|
||||||
return imagePath;
|
? imagePath
|
||||||
}
|
: path.resolve(baseDir, imagePath);
|
||||||
|
return resolveLocalWithFallback(resolved, logLabel);
|
||||||
return path.resolve(baseDir, imagePath);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function resolveContentImages(
|
export async function resolveContentImages(
|
||||||
@@ -125,3 +126,31 @@ export async function resolveContentImages(
|
|||||||
|
|
||||||
return resolved;
|
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;
|
placeholder: string;
|
||||||
localPath: string;
|
localPath: string;
|
||||||
originalPath: string;
|
originalPath: string;
|
||||||
|
alt?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ParsedMarkdown {
|
interface ParsedMarkdown {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import path from "node:path";
|
|||||||
export interface ImagePlaceholder {
|
export interface ImagePlaceholder {
|
||||||
originalPath: string;
|
originalPath: string;
|
||||||
placeholder: string;
|
placeholder: string;
|
||||||
|
alt?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ResolvedImageInfo extends ImagePlaceholder {
|
export interface ResolvedImageInfo extends ImagePlaceholder {
|
||||||
@@ -23,9 +24,10 @@ export function replaceMarkdownImagesWithPlaceholders(
|
|||||||
const images: ImagePlaceholder[] = [];
|
const images: ImagePlaceholder[] = [];
|
||||||
let imageCounter = 0;
|
let imageCounter = 0;
|
||||||
|
|
||||||
const rewritten = markdown.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, (_match, _alt, src) => {
|
const rewritten = markdown.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, (_match, alt, src) => {
|
||||||
const placeholder = `${placeholderPrefix}${++imageCounter}`;
|
const placeholder = `${placeholderPrefix}${++imageCounter}`;
|
||||||
images.push({
|
images.push({
|
||||||
|
alt,
|
||||||
originalPath: src,
|
originalPath: src,
|
||||||
placeholder,
|
placeholder,
|
||||||
});
|
});
|
||||||
@@ -101,11 +103,10 @@ export async function resolveImagePath(
|
|||||||
return localPath;
|
return localPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (path.isAbsolute(imagePath)) {
|
const resolved = path.isAbsolute(imagePath)
|
||||||
return imagePath;
|
? imagePath
|
||||||
}
|
: path.resolve(baseDir, imagePath);
|
||||||
|
return resolveLocalWithFallback(resolved, logLabel);
|
||||||
return path.resolve(baseDir, imagePath);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function resolveContentImages(
|
export async function resolveContentImages(
|
||||||
@@ -125,3 +126,31 @@ export async function resolveContentImages(
|
|||||||
|
|
||||||
return resolved;
|
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