diff --git a/docs/api.md b/docs/api.md index 57e8689..947c7f0 100644 --- a/docs/api.md +++ b/docs/api.md @@ -97,7 +97,7 @@ Read and write vault files. - `const dataurl = await $.io.load_data_url( path, mimetype? )` - Encode the contents of a file into a dataurl for use in a template - Optionaly specify the mime type. - - Auto mime type mappings are available for the following file extensions: `.jpg`, `.png`, `.gif`, `.svg`, `.css` + - Auto mime type mappings are available for the following common file extensions: `.jpeg`, `.jpg`, `.png`, `.gif`, `.svg`, `.css` - `await $.io.output( file, content, open? )` - (Over)Writes the `file` with given `content` - Tries to open the `file` if `open` is true (false by default) diff --git a/src/constants.ts b/src/constants.ts index 5bb20dd..451103d 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,2 +1,12 @@ export const CODE_BLOCK_LANG_TOOLBAR = 'meld-build-toolbar'; -export const URL_HELP = 'https://github.com/meld-cp/obsidian-build/blob/master/docs/user-guide.md'; \ No newline at end of file + +export const URL_HELP = 'https://github.com/meld-cp/obsidian-build/blob/master/docs/user-guide.md'; + +export const EXTENSION_MIMETYPE_MAP = new Map([ + ['jpeg', 'image/jpeg'], + ['jpg', 'image/jpeg'], + ['png', 'image/png'], + ['gif', 'image/gif'], + ['svg', 'image/svg+xml'], + ['css', 'text/css'], +]); diff --git a/src/rci-io.ts b/src/rci-io.ts index e3ae38f..8cefc00 100644 --- a/src/rci-io.ts +++ b/src/rci-io.ts @@ -1,4 +1,5 @@ import { normalizePath, TFile } from "obsidian"; +import { EXTENSION_MIMETYPE_MAP } from "./constants"; import { DataSet, IDataSetCollection } from "./data-set"; import { NamedCodeBlock } from "./named-code-block"; import { Parser } from "./parser"; @@ -103,13 +104,10 @@ export class IoRunContextImplemention implements TIoRunContext { return Promise.resolve(undefined); } - const finalMimeType = mimetype ?? { - 'jpg':'image/jpeg', - 'png':'image/png', - 'gif':'image/gif', - 'svg':'image/svg+xml', - 'css':'text/css' - }[af.extension] ?? ''; + const finalMimeType = mimetype + ?? EXTENSION_MIMETYPE_MAP.get(af.extension) + ?? '' + ; const base64Data = Utils.toBase64( await app.vault.readBinary(af) );