Skip to content

Commit

Permalink
Update Prettier configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
remcohaszing committed May 6, 2023
1 parent 773910a commit 79df18a
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 87 deletions.
3 changes: 2 additions & 1 deletion .prettierrc.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
proseWrap: always
semi: false
singleQuote: true
trailingComma: all
trailingComma: none
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,18 @@ information.
This plugin takes all code blocks marked as `mermaid` and renders them as an inline SVG.

```js
import { readFile } from 'node:fs/promises';
import { readFile } from 'node:fs/promises'

import { remark } from 'remark';
import remarkMermaid from 'remark-mermaidjs';
import { remark } from 'remark'
import remarkMermaid from 'remark-mermaidjs'

const { value } = await remark()
.use(remarkMermaid, {
/* Options */
})
.process(await readFile('readme.md'));
.process(await readFile('readme.md'))

console.log(value);
console.log(value)
```

## API
Expand Down
60 changes: 30 additions & 30 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { fromHtmlIsomorphic } from 'hast-util-from-html-isomorphic';
import { type BlockContent, type Code, type Parent, type Root } from 'mdast';
import { fromHtmlIsomorphic } from 'hast-util-from-html-isomorphic'
import { type BlockContent, type Code, type Parent, type Root } from 'mdast'
import {
createMermaidRenderer,
type CreateMermaidRendererOptions,
type RenderOptions,
} from 'mermaid-isomorphic';
import { type Plugin } from 'unified';
import { visit } from 'unist-util-visit';
import { type VFile } from 'vfile';
type RenderOptions
} from 'mermaid-isomorphic'
import { type Plugin } from 'unified'
import { visit } from 'unist-util-visit'
import { type VFile } from 'vfile'

type CodeInstance = [Code, Parent];
type CodeInstance = [Code, Parent]

export interface RemarkMermaidOptions
extends CreateMermaidRendererOptions,
Expand All @@ -24,58 +24,58 @@ export interface RemarkMermaidOptions
* code block is removed
*/
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
errorFallback?: (node: Code, error: string, file: VFile) => BlockContent | undefined | void;
errorFallback?: (node: Code, error: string, file: VFile) => BlockContent | undefined | void
}

export type RemarkMermaid = Plugin<[RemarkMermaidOptions?], Root>;
export type RemarkMermaid = Plugin<[RemarkMermaidOptions?], Root>

/**
* @param options Options that may be used to tweak the output.
*/
const remarkMermaid: RemarkMermaid = (options) => {
const render = createMermaidRenderer(options);
const render = createMermaidRenderer(options)

return async function transformer(ast, file) {
const instances: CodeInstance[] = [];
const instances: CodeInstance[] = []

visit(ast, { type: 'code', lang: 'mermaid' }, (node: Code, index, parent: Parent) => {
instances.push([node, parent]);
});
instances.push([node, parent])
})

// Nothing to do. No need to start a browser in this case.
if (!instances.length) {
return;
return
}

const results = await render(
instances.map((instance) => instance[0].value),
options,
);
options
)

for (const [i, [node, parent]] of instances.entries()) {
const result = results[i];
const nodeIndex = parent.children.indexOf(node);
const result = results[i]
const nodeIndex = parent.children.indexOf(node)

if (result.status === 'fulfilled') {
const { svg } = result.value;
const hChildren = fromHtmlIsomorphic(svg, { fragment: true }).children;
const { svg } = result.value
const hChildren = fromHtmlIsomorphic(svg, { fragment: true }).children
parent.children[nodeIndex] = {
type: 'paragraph',
children: [{ type: 'html', value: svg }],
data: { hChildren },
};
data: { hChildren }
}
} else if (options?.errorFallback) {
const fallback = options.errorFallback(node, result.reason, file);
const fallback = options.errorFallback(node, result.reason, file)
if (fallback) {
parent.children[nodeIndex] = fallback;
parent.children[nodeIndex] = fallback
} else {
parent.children.splice(nodeIndex, 1);
parent.children.splice(nodeIndex, 1)
}
} else {
file.fail(result.reason, node, 'remark-mermaidjs:remark-mermaidjs');
file.fail(result.reason, node, 'remark-mermaidjs:remark-mermaidjs')
}
}
};
};
}
}

export default remarkMermaid;
export default remarkMermaid
8 changes: 4 additions & 4 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { type PlaywrightTestConfig } from '@playwright/test';
import { type PlaywrightTestConfig } from '@playwright/test'

const config: PlaywrightTestConfig = {
testMatch: /test\/test\.ts$/,
metadata: {},
};
metadata: {}
}

export default config;
export default config
10 changes: 5 additions & 5 deletions test/fixtures/error/options.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { type RemarkMermaidOptions } from 'remark-mermaidjs';
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)}`,
};
},
};
value: `${vfile.basename}\n\n${error}\n\n${JSON.stringify(node, undefined, 2)}`
}
}
}
6 changes: 3 additions & 3 deletions test/fixtures/errorEmpty/options.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type RemarkMermaidOptions } from 'remark-mermaidjs';
import { type RemarkMermaidOptions } from 'remark-mermaidjs'

export const options: RemarkMermaidOptions = {
// eslint-disable-next-line @typescript-eslint/no-empty-function
errorFallback() {},
};
errorFallback() {}
}
8 changes: 4 additions & 4 deletions test/fixtures/forest/options.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type RemarkMermaidOptions } from 'remark-mermaidjs';
import { type RemarkMermaidOptions } from 'remark-mermaidjs'

export const options: RemarkMermaidOptions = {
mermaidConfig: {
theme: 'forest',
},
};
theme: 'forest'
}
}
2 changes: 1 addition & 1 deletion test/fixtures/no-mermaid/input.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# No medmaid

```js
console.log('This is a JavaScript code block');
console.log('This is a JavaScript code block')
```
68 changes: 34 additions & 34 deletions test/test.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,56 @@
import { readdir, readFile } from 'node:fs/promises';
import { readdir, readFile } from 'node:fs/promises'

import { expect, test } from '@playwright/test';
import rehypeStringify from 'rehype-stringify';
import { remark } from 'remark';
import remarkRehype from 'remark-rehype';
import { VFile } from 'vfile';
import { expect, test } from '@playwright/test'
import rehypeStringify from 'rehype-stringify'
import { remark } from 'remark'
import remarkRehype from 'remark-rehype'
import { VFile } from 'vfile'

import remarkMermaid, { type RemarkMermaidOptions } from '../index.js';
import remarkMermaid, { type RemarkMermaidOptions } from '../index.js'

const fixtures = new URL('fixtures/', import.meta.url);
const fixtureNames = await readdir(fixtures);
const fixtures = new URL('fixtures/', import.meta.url)
const fixtureNames = await readdir(fixtures)

test.describe.parallel('node', () => {
for (const name of fixtureNames) {
// eslint-disable-next-line no-empty-pattern
test(name, async ({}, testInfo) => {
testInfo.snapshotSuffix = 'node';
const fixture = new URL(`${name}/`, fixtures);
const value = await readFile(new URL('input.md', fixture));
let options: RemarkMermaidOptions | undefined;
testInfo.snapshotSuffix = 'node'
const fixture = new URL(`${name}/`, fixtures)
const value = await readFile(new URL('input.md', fixture))
let options: RemarkMermaidOptions | undefined
try {
({ options } = await import(String(new URL('options.ts', fixture))));
;({ options } = await import(String(new URL('options.ts', fixture))))
} catch {
// This test case uses default options.
}
const processor = remark().use(remarkMermaid, options);
const asMarkdown = await processor.process({ path: `${name}/input.md`, value });
const processor = remark().use(remarkMermaid, options)
const asMarkdown = await processor.process({ path: `${name}/input.md`, value })
const asHTML = await processor()
.use(remarkRehype)
.use(rehypeStringify)
.process({ path: `${name}/input.md`, value });
expect(asMarkdown.value).toMatchSnapshot({ name: `${name}.md` });
expect(asHTML.value).toMatchSnapshot({ name: `${name}.html` });
});
.process({ path: `${name}/input.md`, value })
expect(asMarkdown.value).toMatchSnapshot({ name: `${name}.md` })
expect(asHTML.value).toMatchSnapshot({ name: `${name}.html` })
})
}

test('it should throw a vfile error if a diagram is invalid without error fallback', async () => {
const processor = remark().use(remarkMermaid);
const file = new VFile('```mermaid\ninvalid\n```\n');
const processor = remark().use(remarkMermaid)
const file = new VFile('```mermaid\ninvalid\n```\n')
await expect(() => processor.process(file)).rejects.toThrowError(
'No diagram type detected matching given configuration for text: invalid',
);
expect(file.messages).toHaveLength(1);
'No diagram type detected matching given configuration for text: invalid'
)
expect(file.messages).toHaveLength(1)
expect(file.messages[0].message).toBe(
'No diagram type detected matching given configuration for text: invalid',
);
expect(file.messages[0].source).toBe('remark-mermaidjs');
expect(file.messages[0].ruleId).toBe('remark-mermaidjs');
expect(file.messages[0].fatal).toBe(true);
'No diagram type detected matching given configuration for text: invalid'
)
expect(file.messages[0].source).toBe('remark-mermaidjs')
expect(file.messages[0].ruleId).toBe('remark-mermaidjs')
expect(file.messages[0].fatal).toBe(true)
expect(file.messages[0].position).toStrictEqual({
start: { offset: 0, line: 1, column: 1 },
end: { offset: 22, line: 3, column: 4 },
});
});
});
end: { offset: 22, line: 3, column: 4 }
})
})
})

0 comments on commit 79df18a

Please sign in to comment.