mirror of
https://github.com/JimLiu/baoyu-skills.git
synced 2026-08-01 22:59:48 +08:00
feat: support Obsidian wikilink images
Support Obsidian image wikilinks alongside standard markdown images, preserve mixed image order, and resolve Obsidian default Attachments/ paths. Co-authored-by: Chao Zheng <10296164+zcqqq@users.noreply.github.com>
This commit is contained in:
@@ -54,3 +54,76 @@ test("CLI forwards wrapper title and package render options", async () => {
|
||||
/<body[^>]*style="[^"]*font-family: Menlo, Monaco, 'Courier New', monospace;[^"]*font-size: 18px/,
|
||||
);
|
||||
});
|
||||
|
||||
test("CLI renders Obsidian wikilink images with alt text and Attachments fallback", async () => {
|
||||
const root = await makeTempDir("baoyu-markdown-to-html-wikilink-cli-");
|
||||
const attachmentsDir = path.join(root, "Attachments");
|
||||
await fs.mkdir(attachmentsDir, { recursive: true });
|
||||
await fs.writeFile(path.join(root, "a.png"), "a", "utf-8");
|
||||
await fs.writeFile(path.join(attachmentsDir, "b.webp"), "b", "utf-8");
|
||||
|
||||
const markdownPath = path.join(root, "article.md");
|
||||
await fs.writeFile(
|
||||
markdownPath,
|
||||
[
|
||||
"## Section",
|
||||
"",
|
||||
"![[a.png]]",
|
||||
"",
|
||||
"![[b.webp|B alt]]",
|
||||
].join("\n"),
|
||||
"utf-8",
|
||||
);
|
||||
|
||||
const { stdout } = await execFileAsync(
|
||||
process.execPath,
|
||||
[
|
||||
"--import",
|
||||
"tsx",
|
||||
SCRIPT_PATH,
|
||||
markdownPath,
|
||||
"--keep-title",
|
||||
],
|
||||
{ cwd: SCRIPT_DIR },
|
||||
);
|
||||
|
||||
const result = JSON.parse(stdout.trim()) as {
|
||||
contentImages: Array<{
|
||||
alt?: string;
|
||||
localPath: string;
|
||||
originalPath: string;
|
||||
placeholder: string;
|
||||
}>;
|
||||
htmlPath: string;
|
||||
};
|
||||
|
||||
assert.deepEqual(
|
||||
result.contentImages.map(({ alt, localPath, originalPath, placeholder }) => ({
|
||||
alt,
|
||||
localPath,
|
||||
originalPath,
|
||||
placeholder,
|
||||
})),
|
||||
[
|
||||
{
|
||||
alt: "",
|
||||
localPath: path.join(root, "a.png"),
|
||||
originalPath: "a.png",
|
||||
placeholder: "MDTOHTMLIMGPH_1",
|
||||
},
|
||||
{
|
||||
alt: "B alt",
|
||||
localPath: path.join(attachmentsDir, "b.webp"),
|
||||
originalPath: "b.webp",
|
||||
placeholder: "MDTOHTMLIMGPH_2",
|
||||
},
|
||||
],
|
||||
);
|
||||
|
||||
const html = await fs.readFile(result.htmlPath, "utf-8");
|
||||
assert.match(html, /<img src="a\.png" data-local-path="[^"]+a\.png" alt=""/);
|
||||
assert.match(
|
||||
html,
|
||||
/<img src="b\.webp" data-local-path="[^"]+Attachments[^"]+b\.webp" alt="B alt"/,
|
||||
);
|
||||
});
|
||||
|
||||
@@ -27,6 +27,7 @@ interface ImageInfo {
|
||||
placeholder: string;
|
||||
localPath: string;
|
||||
originalPath: string;
|
||||
alt?: string;
|
||||
}
|
||||
|
||||
interface MermaidImageInfo {
|
||||
@@ -58,6 +59,14 @@ type ConvertMarkdownOptions = Partial<Omit<CliOptions, "inputPath">> & {
|
||||
mermaid?: MermaidCliOptions;
|
||||
};
|
||||
|
||||
function escapeHtmlAttribute(value: string): string {
|
||||
return value
|
||||
.replace(/&/g, "&")
|
||||
.replace(/"/g, """)
|
||||
.replace(/</g, "<")
|
||||
.replace(/>/g, ">");
|
||||
}
|
||||
|
||||
export async function convertMarkdown(
|
||||
markdownPath: string,
|
||||
options?: ConvertMarkdownOptions,
|
||||
@@ -160,7 +169,12 @@ export async function convertMarkdown(
|
||||
|
||||
let finalContent = fs.readFileSync(finalHtmlPath, "utf-8");
|
||||
for (const image of contentImages) {
|
||||
const imgTag = `<img src="${image.originalPath}" data-local-path="${image.localPath}" style="display: block; width: 100%; margin: 1.5em auto;">`;
|
||||
const altAttr = image.alt !== undefined
|
||||
? ` alt="${escapeHtmlAttribute(image.alt)}"`
|
||||
: "";
|
||||
const imgTag = `<img src="${escapeHtmlAttribute(image.originalPath)}" `
|
||||
+ `data-local-path="${escapeHtmlAttribute(image.localPath)}"${altAttr} `
|
||||
+ `style="display: block; width: 100%; margin: 1.5em auto;">`;
|
||||
finalContent = finalContent.replace(image.placeholder, imgTag);
|
||||
}
|
||||
fs.writeFileSync(finalHtmlPath, finalContent, "utf-8");
|
||||
|
||||
Reference in New Issue
Block a user