Skip to content

Commit abb46a3

Browse files
committed
improve handling of GFM output in mermaid diagrams
1 parent 869844b commit abb46a3

File tree

2 files changed

+77
-23
lines changed

2 files changed

+77
-23
lines changed

src/core/handlers/mermaid.ts

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { formatResourcePath } from "../resources.ts";
1515
import { join } from "path/mod.ts";
1616
import {
1717
isJavascriptCompatible,
18+
isLatexOutput,
1819
isMarkdownOutput,
1920
isRevealjsOutput,
2021
} from "../../config/format.ts";
@@ -36,6 +37,7 @@ import {
3637
import { Element } from "../deno-dom.ts";
3738
import { convertFromYaml } from "../lib/yaml-schema/from-yaml.ts";
3839
import { readYamlFromString } from "../yaml.ts";
40+
import { LocalizedError } from "../lib/error.ts";
3941

4042
const mermaidHandler: LanguageHandler = {
4143
...baseHandler,
@@ -139,14 +141,22 @@ object:
139141
});
140142
}
141143

142-
return this.build(
143-
handlerContext,
144-
cell,
145-
svg,
146-
options,
147-
undefined,
148-
new Set(["fig-width", "fig-height"]),
149-
);
144+
if (isMarkdownOutput(handlerContext.options.format.pandoc, ["gfm"])) {
145+
throw new LocalizedError(
146+
"UnsupportedFormatError",
147+
"`mermaid-format: svg` is not supported in GFM format",
148+
cell.sourceVerbatim,
149+
);
150+
} else {
151+
return this.build(
152+
handlerContext,
153+
cell,
154+
svg,
155+
options,
156+
undefined,
157+
new Set(["fig-width", "fig-height"]),
158+
);
159+
}
150160
};
151161

152162
const makePng = async () => {
@@ -162,21 +172,36 @@ object:
162172
resources,
163173
});
164174

165-
const {
175+
let {
166176
widthInInches,
167177
heightInInches,
168178
} = await resolveSize(svgText, options);
169-
170-
return this.build(
171-
handlerContext,
172-
cell,
173-
mappedConcat([
174-
`\n![](${sourceName}){width="${widthInInches}in" height="${heightInInches}in" fig-pos='H'}\n`,
175-
]),
176-
options,
177-
undefined,
178-
new Set(["fig-width", "fig-height"]),
179-
);
179+
widthInInches = Math.round(widthInInches * 100) / 100;
180+
heightInInches = Math.round(heightInInches * 100) / 100;
181+
182+
const posSpecifier = isLatexOutput(handlerContext.options.format.pandoc)
183+
? " fig-pos='H'"
184+
: "";
185+
const idSpecifier = cell.options?.label ? ` #${cell.options?.label}` : "";
186+
187+
const cellContent = mappedConcat([
188+
`\n![${
189+
cell.options?.["fig-cap"] || ""
190+
}](${sourceName}){width="${widthInInches}in" height="${heightInInches}in"${posSpecifier}${idSpecifier}}\n`,
191+
]);
192+
193+
if (isMarkdownOutput(handlerContext.options.format.pandoc, ["gfm"])) {
194+
return cellContent;
195+
} else {
196+
return this.build(
197+
handlerContext,
198+
cell,
199+
cellContent,
200+
options,
201+
undefined,
202+
new Set(["fig-width", "fig-height"]),
203+
);
204+
}
180205
};
181206

182207
const makeDefault = async () => {
@@ -185,14 +210,15 @@ object:
185210
} else if (
186211
isMarkdownOutput(handlerContext.options.format.pandoc, ["gfm"])
187212
) {
188-
return this.build(
213+
return mappedConcat(["\n``` mermaid\n", cellContent, "\n```\n"]);
214+
215+
/*return this.build(
189216
handlerContext,
190217
cell,
191-
mappedConcat(["\n``` mermaid\n", cellContent, "\n```\n"]),
192218
options,
193219
undefined,
194220
new Set(["fig-width", "fig-height"]),
195-
);
221+
);*/
196222
} else {
197223
return await makePng();
198224
}

src/core/lib/error.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*
66
*/
77

8+
import { mappedIndexToLineCol, MappedString } from "./mapped-text.ts";
9+
810
// deno-lint-ignore-file no-explicit-any
911

1012
export class ErrorEx extends Error {
@@ -24,6 +26,32 @@ export class ErrorEx extends Error {
2426
public readonly printStack: boolean;
2527
}
2628

29+
export class LocalizedError extends Error {
30+
constructor(
31+
name: string,
32+
message: string,
33+
source: MappedString,
34+
position = 0,
35+
printName = true,
36+
printStack = true,
37+
) {
38+
const fileName = source.map(position)?.originalString?.fileName;
39+
if (fileName) {
40+
const { line, column } = mappedIndexToLineCol(source)(position);
41+
message = `In file ${fileName} (${line + 1}:${column + 1}):
42+
${message}`;
43+
}
44+
45+
super(message);
46+
this.name = name;
47+
this.printName = printName;
48+
this.printStack = printStack;
49+
}
50+
51+
public readonly printName: boolean;
52+
public readonly printStack: boolean;
53+
}
54+
2755
export function asErrorEx(e: unknown) {
2856
if (e instanceof ErrorEx) {
2957
return e;

0 commit comments

Comments
 (0)