refactor(baoyu-post-to-x): simplify image placeholders from bracket format to XIMGPH

This commit is contained in:
Jim Liu 宝玉
2026-01-25 21:05:08 -06:00
parent 977598d5ae
commit 1afa6f5a58
2 changed files with 7 additions and 7 deletions
@@ -67,7 +67,7 @@ Code blocks become blockquotes (X doesn't support code)
1. **Cover Image**: First image or `cover_image` from frontmatter 1. **Cover Image**: First image or `cover_image` from frontmatter
2. **Remote Images**: Automatically downloaded to temp directory 2. **Remote Images**: Automatically downloaded to temp directory
3. **Placeholders**: Images in content use `[[IMAGE_PLACEHOLDER_N]]` format 3. **Placeholders**: Images in content use `XIMGPH_N` format
4. **Insertion**: Placeholders are found, selected, and replaced with actual images 4. **Insertion**: Placeholders are found, selected, and replaced with actual images
## Markdown to HTML Script ## Markdown to HTML Script
@@ -92,7 +92,7 @@ JSON output:
"coverImage": "/path/to/cover.jpg", "coverImage": "/path/to/cover.jpg",
"contentImages": [ "contentImages": [
{ {
"placeholder": "[[IMAGE_PLACEHOLDER_1]]", "placeholder": "XIMGPH_1",
"localPath": "/tmp/x-article-images/img.png", "localPath": "/tmp/x-article-images/img.png",
"blockIndex": 5 "blockIndex": 5
} }
+5 -5
View File
@@ -280,7 +280,7 @@ export async function parseMarkdown(
let imageCounter = 0; let imageCounter = 0;
const { html, totalBlocks } = convertMarkdownToHtml(body, (src, alt) => { const { html, totalBlocks } = convertMarkdownToHtml(body, (src, alt) => {
const placeholder = `[[IMAGE_PLACEHOLDER_${++imageCounter}]]`; const placeholder = `XIMGPH_${++imageCounter}`;
const currentBlockIndex = images.length; // Will be set properly after HTML generation const currentBlockIndex = images.length; // Will be set properly after HTML generation
images.push({ src, alt, blockIndex: -1 }); // blockIndex set later images.push({ src, alt, blockIndex: -1 }); // blockIndex set later
@@ -292,7 +292,7 @@ export async function parseMarkdown(
let blockIdx = 0; let blockIdx = 0;
for (const line of htmlLines) { for (const line of htmlLines) {
for (let i = 0; i < images.length; i++) { for (let i = 0; i < images.length; i++) {
const placeholder = `[[IMAGE_PLACEHOLDER_${i + 1}]]`; const placeholder = `XIMGPH_${i + 1}`;
if (line.includes(placeholder)) { if (line.includes(placeholder)) {
images[i]!.blockIndex = blockIdx; images[i]!.blockIndex = blockIdx;
} }
@@ -312,7 +312,7 @@ export async function parseMarkdown(
// First image becomes cover if no cover specified // First image becomes cover if no cover specified
if (isFirstImage && !coverImagePath) { if (isFirstImage && !coverImagePath) {
coverImagePath = localPath; coverImagePath = localPath;
coverPlaceholder = `[[IMAGE_PLACEHOLDER_${i + 1}]]`; coverPlaceholder = `XIMGPH_${i + 1}`;
isFirstImage = false; isFirstImage = false;
// Don't add to contentImages, it's the cover // Don't add to contentImages, it's the cover
continue; continue;
@@ -320,7 +320,7 @@ export async function parseMarkdown(
isFirstImage = false; isFirstImage = false;
contentImages.push({ contentImages.push({
placeholder: `[[IMAGE_PLACEHOLDER_${i + 1}]]`, placeholder: `XIMGPH_${i + 1}`,
localPath, localPath,
originalPath: img.src, originalPath: img.src,
blockIndex: img.blockIndex, blockIndex: img.blockIndex,
@@ -331,7 +331,7 @@ export async function parseMarkdown(
let finalHtml = html; let finalHtml = html;
if (coverPlaceholder) { if (coverPlaceholder) {
// Remove the placeholder and its containing <p> tag // Remove the placeholder and its containing <p> tag
finalHtml = finalHtml.replace(new RegExp(`<p>${coverPlaceholder.replace(/[[\]]/g, '\\$&')}</p>\\n?`, 'g'), ''); finalHtml = finalHtml.replace(new RegExp(`<p>${coverPlaceholder}</p>\\n?`, 'g'), '');
} }
// Resolve cover image path // Resolve cover image path