Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/core/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,18 @@ export async function renderMarkdownToHtml(
markdown: string,
): Promise<RenderedPageDocument> {
const renderedMarkdown = autolinkBareUrls(stripWikilinks(markdown.trim()));
const defaultProtocols = defaultSchema.protocols as
| Record<string, Array<string> | null | undefined>
| undefined;
const defaultSrcProtocolsValue = defaultProtocols?.["src"];
const defaultSrcProtocols = Array.isArray(defaultSrcProtocolsValue)
? defaultSrcProtocolsValue
: [];
const sanitizeSchema = {
...defaultSchema,
protocols: {
...defaultSchema.protocols,
src: [...(defaultSchema.protocols?.["src"] ?? []), "data"],
src: [...defaultSrcProtocols, "data"],
},
};
const rawHtml = String(
Expand Down
44 changes: 42 additions & 2 deletions src/core/publish-markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const LOCAL_IMAGE_EXTENSIONS = new Set([
".svg",
".avif",
]);
const EXCALIDRAW_MARKDOWN_SUFFIX = ".excalidraw.md";
const EXCALIDRAW_EXPORT_EXTENSIONS = [".svg", ".png", ".webp", ".jpg", ".jpeg"];

const OBSIDIAN_IMAGE_EMBED_RE = /!\[\[([^\]\n]+)\]\]/g;
const MARKDOWN_IMAGE_RE = /!\[([^\]]*)\]\(([^)\n]+)\)/g;
Expand Down Expand Up @@ -161,7 +163,11 @@ async function resolveLocalImageAsset(
baseDir: string,
rawTarget: string,
): Promise<{ alt: string; dataUrl: string } | null> {
const resolvedPath = path.resolve(baseDir, rawTarget);
const excalidrawExportPath = await resolveExcalidrawExportPath(
baseDir,
rawTarget,
);
const resolvedPath = excalidrawExportPath ?? path.resolve(baseDir, rawTarget);
const extension = path.extname(resolvedPath).toLowerCase();

if (!LOCAL_IMAGE_EXTENSIONS.has(extension)) {
Expand All @@ -171,14 +177,48 @@ async function resolveLocalImageAsset(
try {
const content = await readFile(resolvedPath);
return {
alt: escapeMarkdownLabel(path.basename(rawTarget)),
alt: escapeMarkdownLabel(
path.basename(
excalidrawExportPath === null
? rawTarget
: rawTarget.replace(/\.excalidraw\.md$/i, extension),
),
),
dataUrl: buildDataUrl(content, extension),
};
} catch {
return null;
}
}

async function resolveExcalidrawExportPath(
baseDir: string,
rawTarget: string,
): Promise<string | null> {
if (!rawTarget.toLowerCase().endsWith(EXCALIDRAW_MARKDOWN_SUFFIX)) {
return null;
}

const absoluteTarget = path.resolve(baseDir, rawTarget);
const exportBasePath = absoluteTarget.slice(
0,
-EXCALIDRAW_MARKDOWN_SUFFIX.length,
);

for (const extension of EXCALIDRAW_EXPORT_EXTENSIONS) {
const candidatePath = `${exportBasePath}${extension}`;

try {
await readFile(candidatePath);
return candidatePath;
} catch {
// Try the next export format.
}
}

return null;
}

function buildDataUrl(content: Buffer, extension: string): string {
return `data:${mimeTypeForExtension(extension)};base64,${content.toString("base64")}`;
}
Expand Down
66 changes: 66 additions & 0 deletions tests/integration/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,72 @@ Look:
const rawMarkdown = await rawResponse.text();
expect(rawMarkdown).toContain("![[diagram.svg|320x200]]");
});

it("renders Excalidraw embeds from sibling exported images while preserving the raw note", async () => {
const root = await mkdtemp(path.join(os.tmpdir(), "publish-it-cli-excal-"));
const configDir = path.join(root, "config");
const mappingPath = path.join(root, ".pub");
const cwd = path.join(root, "workspace");
const notePath = path.join(cwd, "note.md");
const drawingPath = path.join(cwd, "landscape.excalidraw.md");
const exportPath = path.join(cwd, "landscape.svg");

server = await startTestServer(root);
await mkdir(cwd, { recursive: true });
await writeFile(
drawingPath,
`---
excalidraw-plugin: parsed
---
`,
"utf8",
);
await writeFile(
exportPath,
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10"><rect width="10" height="10" fill="orange"/></svg>',
"utf8",
);
await writeFile(
notePath,
`---
title: Excalidraw Embed
---

Diagram:

![[landscape.excalidraw.md]]
`,
"utf8",
);

await runCli(["claim", "restuta", "--api-base", server.origin], {
cwd,
env: {
PUB_CONFIG_DIR: configDir,
PUB_MAPPING_PATH: mappingPath,
},
});

const publishResult = await runCli(
["publish", notePath, "--api-base", server.origin],
{
cwd,
env: {
PUB_CONFIG_DIR: configDir,
PUB_MAPPING_PATH: mappingPath,
},
},
);
const pageUrl = publishResult.stdout.trim();

const htmlResponse = await fetch(pageUrl);
const html = await htmlResponse.text();
expect(html).toContain("data:image/svg+xml;base64,");

const rawResponse = await fetch(`${pageUrl}?raw=1`);
const rawMarkdown = await rawResponse.text();
expect(rawMarkdown).toContain("![[landscape.excalidraw.md]]");
});
});

async function runCli(
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/publish-markdown.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,35 @@ describe("prepareMarkdownBodyForPublish", () => {

expect(prepared).toContain("![Team photo](data:image/svg+xml;base64,");
});

it("resolves Excalidraw embeds to sibling exported images", async () => {
const root = await mkdtemp(path.join(os.tmpdir(), "pubmd-excalidraw-"));
const notePath = path.join(root, "note.md");
const drawingPath = path.join(root, "diagram.excalidraw.md");
const exportPath = path.join(root, "diagram.svg");

await writeFile(
drawingPath,
`---
excalidraw-plugin: parsed
---
`,
"utf8",
);
await writeFile(
exportPath,
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10"><path d="M0 0h10v10H0z" fill="orange"/></svg>',
"utf8",
);

const prepared = await prepareMarkdownBodyForPublish(
"Here:\n\n![[diagram.excalidraw.md]]",
{
sourcePath: notePath,
},
);

expect(prepared).toContain("data:image/svg+xml;base64,");
expect(prepared).not.toContain("![[diagram.excalidraw.md]]");
});
});