From 79df18a91627494eafc7edf68f8cc22ffaa18acc Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Sat, 6 May 2023 20:55:51 +0200 Subject: [PATCH] Update Prettier configuration --- .prettierrc.yaml | 3 +- README.md | 10 ++--- index.ts | 60 ++++++++++++------------- playwright.config.ts | 8 ++-- test/fixtures/error/options.ts | 10 ++--- test/fixtures/errorEmpty/options.ts | 6 +-- test/fixtures/forest/options.ts | 8 ++-- test/fixtures/no-mermaid/input.md | 2 +- test/test.ts | 68 ++++++++++++++--------------- 9 files changed, 88 insertions(+), 87 deletions(-) diff --git a/.prettierrc.yaml b/.prettierrc.yaml index 6b0b711..b3732ec 100644 --- a/.prettierrc.yaml +++ b/.prettierrc.yaml @@ -1,3 +1,4 @@ proseWrap: always +semi: false singleQuote: true -trailingComma: all +trailingComma: none diff --git a/README.md b/README.md index 2ff02c6..118003e 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/index.ts b/index.ts index d99e16c..566900b 100644 --- a/index.ts +++ b/index.ts @@ -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, @@ -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 diff --git a/playwright.config.ts b/playwright.config.ts index b380f07..cb76bef 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -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 diff --git a/test/fixtures/error/options.ts b/test/fixtures/error/options.ts index ac14f02..de374d3 100644 --- a/test/fixtures/error/options.ts +++ b/test/fixtures/error/options.ts @@ -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)}` + } + } +} diff --git a/test/fixtures/errorEmpty/options.ts b/test/fixtures/errorEmpty/options.ts index 33dbf21..17cca85 100644 --- a/test/fixtures/errorEmpty/options.ts +++ b/test/fixtures/errorEmpty/options.ts @@ -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() {} +} diff --git a/test/fixtures/forest/options.ts b/test/fixtures/forest/options.ts index 9b7fcbd..6587ba9 100644 --- a/test/fixtures/forest/options.ts +++ b/test/fixtures/forest/options.ts @@ -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' + } +} diff --git a/test/fixtures/no-mermaid/input.md b/test/fixtures/no-mermaid/input.md index fca3f7f..19d2ad9 100644 --- a/test/fixtures/no-mermaid/input.md +++ b/test/fixtures/no-mermaid/input.md @@ -1,5 +1,5 @@ # No medmaid ```js -console.log('This is a JavaScript code block'); +console.log('This is a JavaScript code block') ``` diff --git a/test/test.ts b/test/test.ts index f0b4d4d..537d4f8 100644 --- a/test/test.ts +++ b/test/test.ts @@ -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 } + }) + }) +})