-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
If the user provides `errorFallback`, it will be called when mermaid throws an error. It will receive the faulty code node, the mermaid error message, and the vfile as arguments, and may return a node to replace the code. If nothing is returned from the fallback, the code will be removed instead. To keep the code as-is, simply return the it from the fallback. Also if no error fallback is provided, errors are now handled gracefully using `file.fail()`. Closes #10
- Loading branch information
1 parent
c7206ed
commit cefeaf4
Showing
19 changed files
with
343 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,37 @@ | ||
import { fromDom } from 'hast-util-from-dom'; | ||
import { type Code, type Parent, type Root } from 'mdast'; | ||
import mermaid from 'mermaid'; | ||
import { type Plugin } from 'unified'; | ||
import { visit } from 'unist-util-visit'; | ||
import { type RemarkMermaid } from 'remark-mermaidjs'; | ||
|
||
// eslint-disable-next-line jsdoc/require-jsdoc | ||
function transformer(ast: Root): void { | ||
const instances: [string, number, Parent][] = []; | ||
import { extractCodeBlocks, replaceCodeBlocks } from './shared.js'; | ||
|
||
visit(ast, { type: 'code', lang: 'mermaid' }, (node: Code, index, parent: Parent) => { | ||
instances.push([node.value, index, parent]); | ||
}); | ||
const remarkMermaid: RemarkMermaid = (options) => (ast, file) => { | ||
const instances = extractCodeBlocks(ast); | ||
|
||
// Nothing to do. No need to start puppeteer in this case. | ||
// Nothing to do. No need to do further processing. | ||
if (!instances.length) { | ||
return; | ||
} | ||
|
||
const results = instances.map(([code], index) => | ||
// @ts-expect-error The mermaid types are wrong. | ||
mermaid.render(`remark-mermaid-${index}`, code), | ||
); | ||
const results = instances.map(([node], index) => { | ||
try { | ||
return { | ||
success: true, | ||
// @ts-expect-error The mermaid types are wrong. | ||
result: mermaid.render(`remark-mermaid-${index}`, node.value), | ||
}; | ||
} catch (error) { | ||
return { | ||
success: false, | ||
result: error instanceof Error ? error.message : String(error), | ||
}; | ||
} | ||
}); | ||
|
||
const wrapper = document.createElement('div'); | ||
for (const [i, [, index, parent]] of instances.entries()) { | ||
const value = results[i]; | ||
replaceCodeBlocks(instances, results, options, file, (value) => { | ||
wrapper.innerHTML = value; | ||
parent.children.splice(index, 1, { | ||
type: 'paragraph', | ||
children: [{ type: 'html', value }], | ||
data: { hChildren: [fromDom(wrapper.firstChild!)] }, | ||
}); | ||
} | ||
} | ||
|
||
const remarkMermaid: Plugin<[], Root> = () => transformer; | ||
return [value, fromDom(wrapper.firstChild!)]; | ||
}); | ||
}; | ||
|
||
export default remarkMermaid; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { type Node } from 'hast'; | ||
import { type Code, type Parent, type Root } from 'mdast'; | ||
import { visit } from 'unist-util-visit'; | ||
import { type VFile } from 'vfile'; | ||
|
||
import { type RemarkMermaidOptions } from './index.js'; | ||
|
||
type CodeInstance = [Code, number, Parent]; | ||
|
||
/** | ||
* Extract Mermaid code blocks from the AST. | ||
* | ||
* @param ast The markdown AST to extract code blocks from. | ||
* @returns A list of tuples that represent the code blocks. | ||
*/ | ||
export function extractCodeBlocks(ast: Root): CodeInstance[] { | ||
const instances: CodeInstance[] = []; | ||
|
||
visit(ast, { type: 'code', lang: 'mermaid' }, (node: Code, index, parent: Parent) => { | ||
instances.push([node, index, parent]); | ||
}); | ||
|
||
return instances; | ||
} | ||
|
||
export interface Result { | ||
/** | ||
* This indicates diagram was rendered succesfully. | ||
*/ | ||
success: boolean; | ||
|
||
/** | ||
* Either the resulting SVG code or the error message depending on the success status. | ||
*/ | ||
result: string; | ||
} | ||
|
||
/** | ||
* Replace the code blocks with rendered diagrams. | ||
* | ||
* @param instances The code block instances to replace. | ||
* @param results The diagram rendering results. | ||
* @param options The `remark-mermaidjs` options as given by the user. | ||
* @param file The file to report errors on. | ||
* @param processDiagram Postprocess a diagram. | ||
*/ | ||
export function replaceCodeBlocks( | ||
instances: CodeInstance[], | ||
results: Result[], | ||
options: RemarkMermaidOptions | undefined, | ||
file: VFile, | ||
processDiagram: (diagram: string) => [string, Node], | ||
): void { | ||
for (const [i, [node, index, parent]] of instances.entries()) { | ||
const result = results[i]; | ||
if (result.success) { | ||
const [value, hChild] = processDiagram(result.result); | ||
parent.children[index] = { | ||
type: 'paragraph', | ||
children: [{ type: 'html', value }], | ||
data: { hChildren: [hChild] }, | ||
}; | ||
} else if (options?.errorFallback) { | ||
const fallback = options.errorFallback(node, result.result, file); | ||
if (fallback) { | ||
parent.children[index] = fallback; | ||
} else { | ||
parent.children.splice(index, 1); | ||
} | ||
} else { | ||
file.fail(result.result, node, 'remark-mermaidjs:remark-mermaidjs'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Error | ||
|
||
This is an invalid diagram | ||
|
||
```mermaid | ||
invalid | ||
``` | ||
|
||
More content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { RemarkMermaidOptions } from 'remark-mermaidjs'; | ||
|
||
export const options: RemarkMermaidOptions = { | ||
errorFallback(node, error, vfile) { | ||
return { | ||
type: 'code', | ||
value: `${vfile.basename}\n\n${error}\n\n${JSON.stringify(node, undefined, 2)}`, | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Error | ||
|
||
This is an invalid diagram | ||
|
||
```mermaid | ||
invalid | ||
``` | ||
|
||
More content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import type { RemarkMermaidOptions } from 'remark-mermaidjs'; | ||
|
||
export const options: RemarkMermaidOptions = { | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
errorFallback() {}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.