mume-with-litvis
Version:
Fork of mume with added http://litvis.org/
49 lines • 2.29 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = require("fs");
const path_1 = require("path");
const utility_1 = require("../utility");
/**
* Load local svg files and embed them into html directly.
* @param $
*/
function enhance($, options, resolveFilePath) {
return __awaiter(this, void 0, void 0, function* () {
const asyncFunctions = [];
$("img").each((i, img) => {
const $img = $(img);
let src = resolveFilePath($img.attr("src"), false);
const fileProtocolMatch = src.match(/^(file|vscode\-resource):\/\/+/);
if (fileProtocolMatch) {
src = (0, utility_1.removeFileProtocol)(src);
src = src.replace(/\?(\.|\d)+$/, ""); // remove cache
const imageType = (0, path_1.extname)(src).slice(1);
if (imageType !== "svg") {
return;
}
asyncFunctions.push(new Promise((resolve, reject) => {
(0, fs_1.readFile)(decodeURI(src), (error, data) => {
if (error) {
return resolve(null);
}
const base64 = new Buffer(data).toString("base64");
$img.attr("src", `data:image/svg+xml;charset=utf-8;base64,${base64}`);
return resolve(base64);
});
}));
}
});
yield Promise.all(asyncFunctions);
});
}
exports.default = enhance;
//# sourceMappingURL=embedded-svgs.js.map