Skip to content

Commit

Permalink
Update breaking dependencies
Browse files Browse the repository at this point in the history
- Update to `puppeteer-core@19`.
- Update to `svgo@3`.
- Remove `@types/svgo`, since `svgo` now ship their own type definitions.
- Remove `@types/mermaid`, since `mermaid` now ship their own type definitions.
- Use default SVGO options.
- Instead of `null`, use `false` to disable SVG minification.
  • Loading branch information
remcohaszing committed Nov 21, 2022
1 parent 529e44b commit b6189d3
Show file tree
Hide file tree
Showing 47 changed files with 569 additions and 2,783 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
**/*-snapshots/*
coverage/
dist/
test-results/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ information, see the Puppeteer
This plugin takes all code blocks marked as `mermaid` and renders them as an inline SVG.

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

import { remark } from 'remark';
import remarkMermaid from 'remark-mermaidjs';
Expand Down
80 changes: 11 additions & 69 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,78 +1,20 @@
import { createRequire } from 'module';
import { createRequire } from 'node:module';

import { fromParse5 } from 'hast-util-from-parse5';
import { type Code, type Parent, type Root } from 'mdast';
import { type Mermaid } from 'mermaid';
import { type MermaidConfig } from 'mermaid';
import { parseFragment } from 'parse5';
import puppeteer, { type Browser, type Page, type PuppeteerLaunchOptions } from 'puppeteer-core';
import { optimize, type OptimizedSvg, type OptimizeOptions } from 'svgo';
import { type Config, optimize } from 'svgo';
import { type Plugin } from 'unified';
import { visit } from 'unist-util-visit';

const mermaidScript = {
path: createRequire(import.meta.url).resolve('mermaid/dist/mermaid.min.js'),
};

declare const mermaid: Mermaid;

export const defaultSVGOOptions: OptimizeOptions = {
js2svg: {
indent: 2,
pretty: true,
},
multipass: false,
plugins: [
{ name: 'addAttributesToSVGElement', active: false },
{ name: 'addClassesToSVGElement', active: false },
{ name: 'cleanupAttrs', active: true },
{ name: 'cleanupEnableBackground', active: false },
{ name: 'cleanupListOfValues', active: false },
{ name: 'cleanupNumericValues', active: true },
{ name: 'convertColors', active: true },
{ name: 'convertEllipseToCircle', active: true },
{ name: 'convertPathData', active: true },
{ name: 'convertShapeToPath', active: false },
{ name: 'convertTransform', active: true },
{ name: 'minifyStyles', active: true },
{ name: 'inlineStyles', active: true, params: { onlyMatchedOnce: false } },
{ name: 'convertStyleToAttrs', active: true },
{ name: 'mergePaths', active: true },
{ name: 'moveElemsAttrsToGroup', active: false },
{ name: 'moveGroupAttrsToElems', active: false },
{ name: 'prefixIds', active: false },
{ name: 'removeAttributesBySelector', active: false },
{ name: 'removeComments', active: true },
{ name: 'removeDesc', active: true },
{ name: 'removeDimensions', active: false },
{ name: 'removeDoctype', active: true },
{ name: 'removeEditorsNSData', active: true },
{ name: 'removeElementsByAttr', active: false },
{ name: 'removeEmptyAttrs', active: false },
{ name: 'removeEmptyContainers', active: true },
{ name: 'removeEmptyText', active: true },
{ name: 'removeHiddenElems', active: true },
{ name: 'removeMetadata', active: true },
{ name: 'removeNonInheritableGroupAttrs', active: true },
{ name: 'removeOffCanvasPaths', active: true },
{ name: 'removeRasterImages', active: true },
{ name: 'removeScriptElement', active: true },
{ name: 'removeStyleElement', active: true },
{ name: 'removeTitle', active: true },
{ name: 'removeUnknownsAndDefaults', active: true },
{ name: 'removeUnusedNS', active: true },
{ name: 'removeUselessDefs', active: true },
{ name: 'removeUselessStrokeAndFill', active: true, params: { removeNone: true } },
{ name: 'removeViewBox', active: true },
{ name: 'removeXMLNS', active: true },
{ name: 'removeXMLProcInst', active: true },
{ name: 'reusePaths', active: true },
{ name: 'removeAttrs', active: true, params: { attrs: ['class'] } },
{ name: 'cleanupIDs', active: true },
{ name: 'sortAttrs', active: true },
{ name: 'sortDefsChildren', active: true },
{ name: 'collapseGroups', active: true },
],
};
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
declare const mermaid: typeof import('mermaid').default;

export interface RemarkMermaidOptions {
/**
Expand All @@ -85,21 +27,21 @@ export interface RemarkMermaidOptions {
/**
* SVGO options used to minify the SVO output.
*
* Set to `null` explicitly to disable this.
* Set to `false` explicitly to disable this.
*
* **Note**: This options is only supported in Node.js. In the browser this option is unused.
*
* @default defaultSVGOOptions
*/
svgo?: OptimizeOptions | null;
svgo?: Config | false;

/**
* The mermaid options to use.
*
* **Note**: This options is only supported in Node.js. In the browser this option is unused. If
* you use this in a browser, call `mermaid.initialize()` manually.
*/
mermaidOptions?: Parameters<typeof mermaid['initialize']>[0];
mermaidOptions?: MermaidConfig;
}

/**
Expand All @@ -110,7 +52,7 @@ const remarkMermaid: Plugin<[RemarkMermaidOptions?], Root> = (options) => {
throw new Error('The option `launchOptions.executablePath` is required when using Node.js');
}

const { launchOptions, mermaidOptions, svgo = defaultSVGOOptions } = options;
const { launchOptions, mermaidOptions, svgo } = options;

let browserPromise: Promise<Browser> | undefined;
let count = 0;
Expand Down Expand Up @@ -166,8 +108,8 @@ const remarkMermaid: Plugin<[RemarkMermaidOptions?], Root> = (options) => {

for (const [i, [, index, parent]] of instances.entries()) {
let value = results[i];
if (svgo) {
value = (optimize(value, svgo) as OptimizedSvg).data;
if (svgo !== false) {
value = optimize(value, svgo).data;
}
parent.children.splice(index, 1, {
type: 'paragraph',
Expand Down
Loading

0 comments on commit b6189d3

Please sign in to comment.