@argdown/pandoc-filter
Version:
Turns Argdown code fences into svg, png, jpg, webp or web-component argument maps
195 lines • 7.99 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const pandoc_filter_1 = require("pandoc-filter");
const node_1 = require("@argdown/node");
const lodash_defaultsdeep_1 = __importDefault(require("lodash.defaultsdeep"));
const core_1 = require("@argdown/core");
const tryToInstallImageExport_1 = require("./tryToInstallImageExport");
let imageCounter = 1;
let webComponentCount = 0;
let loadedConfig;
let settings;
const getFilterSettings = (meta) => {
if (!settings) {
settings = {};
const argdownMeta = meta["argdown"];
if (argdownMeta && argdownMeta.t === "MetaMap") {
for (var entry of Object.entries(argdownMeta.c)) {
const value = entry[1];
if (value.t === "MetaInlines" &&
Array.isArray(value.c) &&
typeof value.c[0].c === "string") {
settings[entry[0]] = value.c[0].c;
}
}
}
settings = (0, core_1.mergeDefaults)(settings, {
caption: "",
mode: "inline",
format: "svg"
});
}
return settings;
};
const getArgdownConfig = async (configPath) => {
if (!loadedConfig) {
let pathToConfig;
if (configPath) {
pathToConfig = path_1.default.resolve(process.cwd(), configPath);
}
else {
pathToConfig = path_1.default.resolve(process.cwd(), "argdown.config.json");
}
loadedConfig = await node_1.argdown.loadConfig(pathToConfig);
if (!loadedConfig) {
loadedConfig = {};
}
}
return loadedConfig;
};
(0, pandoc_filter_1.stdio)(async (ele, _format, meta) => {
var _a, _b;
if (ele.t === `CodeBlock`) {
const [headers, content] = ele.c;
const [, [language]] = headers;
if (language === "argdown-map") {
const settings = Object.assign({}, getFilterSettings(meta));
headers[2].map(item => {
settings[item[0]] = item[1];
});
const config = await getArgdownConfig(settings.config);
const id = headers[0];
const process = getProcess(settings);
if (settings.format != "svg") {
const imageExportInstalled = await (0, tryToInstallImageExport_1.tryToInstallImageExport)(node_1.argdown);
if (!imageExportInstalled) {
throw new Error(`You are trying to export an Argdown map to ${settings.format}. Please run "npm install -g @argdown/image-export" to install the Ardown image export plugin.`);
}
}
switch (settings.mode) {
case "web-component": {
const request = (0, lodash_defaultsdeep_1.default)({
input: content,
process
}, config, {
webComponent: {
figureCaption: settings.caption,
addGlobalStyles: webComponentCount == 0,
addWebComponentPolyfill: webComponentCount == 0,
addWebComponentScript: webComponentCount == 0
}
});
webComponentCount++;
const response = await node_1.argdown.runAsync(request);
return (0, pandoc_filter_1.RawBlock)("html", response.webComponent || "");
}
case "inline": {
const request = (0, lodash_defaultsdeep_1.default)({
input: content,
process
}, config);
const response = await node_1.argdown.runAsync(request);
settings.caption =
((_a = request.webComponent) === null || _a === void 0 ? void 0 : _a.figureCaption) || settings.caption;
let inlineImage = getInlineImage(response, settings.format);
return getPandocImage(settings, inlineImage, id);
}
case "file": {
let fileName = id;
if (!fileName || fileName == "") {
fileName = `map-${imageCounter}`;
imageCounter++;
}
const request = (0, lodash_defaultsdeep_1.default)({
input: content,
process,
saveAs: {
fileName
}
}, config);
const response = await node_1.argdown.runAsync(request);
settings.caption =
((_b = request.webComponent) === null || _b === void 0 ? void 0 : _b.figureCaption) || settings.caption;
return getPandocImage(settings, response.outputPath, id);
}
}
}
else if (language === "argdown") {
const settings = Object.assign({}, getFilterSettings(meta));
let sourceMode = settings.sourceHighlighter;
headers[2].map(item => {
if (item[0] === "mode") {
sourceMode = item[1];
}
settings[item[0]] = item[1];
});
if (sourceMode === "web-component") {
const config = await getArgdownConfig(settings.config);
const process = getProcess(settings);
const request = (0, lodash_defaultsdeep_1.default)({
input: content,
process
}, config, {
webComponent: {
initialView: "source",
figureCaption: settings.caption,
addGlobalStyles: webComponentCount == 0,
addWebComponentPolyfill: webComponentCount == 0,
addWebComponentScript: webComponentCount == 0
}
});
webComponentCount++;
const response = await node_1.argdown.runAsync(request);
return (0, pandoc_filter_1.RawBlock)("html", response.webComponent || "");
}
}
}
return;
});
const getInlineImage = (response, format) => {
const inlineFormat = format == "svg" ? "svg+xml" : format;
let result = response[format];
if (typeof result === "string" || result instanceof String) {
result = Buffer.from(result);
}
return `data:image/${inlineFormat};base64,${result.toString("base64")}`;
};
const getProcess = (settings) => {
const process = [
"parse-input",
"build-model",
"build-map",
"transform-closed-groups",
"colorize",
"export-dot",
"export-svg"
];
if (settings.format !== "svg") {
process.push(`export-${settings.format}`);
}
if (settings.mode == "file") {
process.push(`save-as-${settings.format}`);
}
else if (settings.mode == "web-component") {
process.push("highlight-source", "export-web-component");
}
return process;
};
const getPandocImage = (settings, imagePath, id) => {
const caption = settings.caption || "";
const attr = [];
if (settings.width) {
attr.push(["width", settings.width.toString()]);
}
if (settings.height) {
attr.push(["height", settings.height.toString()]);
}
const fig = `fig:${settings.caption}`;
return (0, pandoc_filter_1.Para)([(0, pandoc_filter_1.Image)([id, [], attr], [(0, pandoc_filter_1.Str)(caption)], [imagePath, fig])]);
};
//# sourceMappingURL=index.js.map