Skip to content

Commit 0ec1fe6

Browse files
karlllJichouP
authored andcommitted
Correctly render SVG images in export
Resolves #58
1 parent 17bb6cc commit 0ec1fe6

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/engine.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// To properly export SVGs as data URLs, see https://github.com/orgs/marp-team/discussions/425
2+
const marpEngineJs = `
3+
module.exports = ({ marp }) => marp.use((md) => {
4+
// https://github.com/markdown-it/markdown-it/issues/447#issuecomment-373408654
5+
const defaultValidateLink = md.validateLink;
6+
md.validateLink = url => /^data:image\\/.*?;/.test(url) || defaultValidateLink(url);
7+
})`;
8+
9+
export function getEngine(): string {
10+
return marpEngineJs;
11+
}

src/export.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Notice, TFile } from 'obsidian';
44
import { convertToBase64 } from './convertImage';
55
import { join, normalize } from 'path';
66
import fixPath from 'fix-path';
7+
import { getEngine } from './engine';
78

89
const imgPathReg = /!\[[^\]]*\]\(([^)]+)\)/g;
910

@@ -21,6 +22,7 @@ export async function exportSlide(
2122
if (!file) return;
2223
const filePath = normalize(join(basePath, file.path));
2324
const tmpPath = join(exportDir, `${file.basename}.tmp`);
25+
const tmpEnginePath = join(exportDir, 'engine.js');
2426

2527
let fileContent = await readFile(filePath, 'utf-8');
2628

@@ -43,6 +45,7 @@ export async function exportSlide(
4345
await mkdir(exportDir, { recursive: true });
4446
try {
4547
await writeFile(tmpPath, fileContent);
48+
await writeFile(tmpEnginePath, getEngine());
4649
} catch (e) {
4750
console.error(e);
4851
}
@@ -53,18 +56,19 @@ export async function exportSlide(
5356
cmd = `npx -y @marp-team/marp-cli@latest --bespoke.transition --stdin false --allow-local-files --theme-set "${themeDir}" -o "${join(
5457
exportDir,
5558
file.basename,
56-
)}.${ext}" -- "${tmpPath}"`;
59+
)}.${ext}" --engine ${tmpEnginePath} -- "${tmpPath}"`;
5760
} catch (e) {
5861
cmd = `npx -y @marp-team/marp-cli@latest --stdin false --allow-local-files --bespoke.transition -o "${join(
5962
exportDir,
6063
file.basename,
61-
)}.${ext}" -- "${tmpPath}"`;
64+
)}.${ext}" --engine ${tmpEnginePath} -- "${tmpPath}"`;
6265
}
6366

6467
fixPath();
6568
new Notice(`Exporting "${file.basename}.${ext}" to "${exportDir}"`, 20000);
6669
exec(cmd, () => {
6770
new Notice('Exported successfully', 20000);
6871
rm(tmpPath);
72+
rm(tmpEnginePath);
6973
});
7074
}

0 commit comments

Comments
 (0)