Skip to content

Commit

Permalink
✨ feature: add svg formatting with prettier
Browse files Browse the repository at this point in the history
Signed-off-by: Pauline <[email protected]>
  • Loading branch information
pauliesnug committed Aug 25, 2024
1 parent 3e48a4f commit d244419
Show file tree
Hide file tree
Showing 9 changed files with 228 additions and 52 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"eslint": "^9.9.1",
"jiti": "^1.21.6",
"tsup": "^8.2.4",
"tsx": "^4.17.0",
"tsx": "^4.18.0",
"typescript": "^5.5.4",
"unbuild": "^2.0.0",
"vite": "^5.4.2",
Expand Down
53 changes: 53 additions & 0 deletions packages/eslint-config/fixtures/input/svg.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
87 changes: 87 additions & 0 deletions packages/eslint-config/fixtures/output/with-formatters/svg.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion packages/eslint-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@
"@types/eslint-plugin-jsx-a11y": "^6.9.0",
"@types/fs-extra": "^11.0.4",
"@types/prompts": "^2.4.9",
"@unocss/eslint-plugin": "^0.62.2",
"@unocss/eslint-plugin": "^0.62.3",
"astro-eslint-parser": "^1.0.2",
"eslint": "^9.9.1",
"eslint-plugin-astro": "^1.2.3",
Expand Down
28 changes: 25 additions & 3 deletions packages/eslint-config/src/configs/formatters.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { isPackageExists } from 'local-pkg';
import { GLOB_ASTRO, GLOB_ASTRO_TS, GLOB_CSS, GLOB_GRAPHQL, GLOB_HTML, GLOB_LESS, GLOB_MARKDOWN, GLOB_POSTCSS, GLOB_SCSS, GLOB_XML } from '../globs';
import { GLOB_ASTRO, GLOB_ASTRO_TS, GLOB_CSS, GLOB_GRAPHQL, GLOB_HTML, GLOB_LESS, GLOB_MARKDOWN, GLOB_POSTCSS, GLOB_SCSS, GLOB_SVG, GLOB_XML } from '../globs';
import { ensurePackages, interopDefault, isPackageInScope, parserPlain } from '../utils';
import type { OptionsFormatters, StylisticConfig, TypedFlatConfigItem, VendoredPrettierOptions } from '../types';
import { StylisticConfigDefaults } from './stylistic';

type VendoredPrettierXMLOptions = Pick<VendoredPrettierOptions, 'xmlQuoteAttributes' | 'xmlSelfClosingSpace' | 'xmlSortAttributesByKey' | 'xmlWhitespaceSensitivity'>;

export async function formatters(options: OptionsFormatters | true = {}, stylistic: StylisticConfig = {}): Promise<TypedFlatConfigItem[]> {
const isXmlInScope = isPackageInScope('@prettier/plugin-xml');
if (options === true)
options = {
astro: isPackageInScope('prettier-plugin-astro'),
Expand All @@ -15,14 +16,15 @@ export async function formatters(options: OptionsFormatters | true = {}, stylist
html: true,
markdown: true,
slidev: isPackageExists('@slidev/cli'),
xml: isPackageInScope('@prettier/plugin-xml'),
svg: isXmlInScope,
xml: isXmlInScope,
};

await ensurePackages([
'eslint-plugin-format',
options.markdown && options.slidev ? 'prettier-plugin-slidev' : undefined,
options.astro ? 'prettier-plugin-astro' : undefined,
options.xml ? '@prettier/plugin-xml' : undefined,
(options.xml || options.svg) ? '@prettier/plugin-xml' : undefined,
]);

if (options.slidev && options.markdown !== true && options.markdown !== 'prettier')
Expand Down Expand Up @@ -154,6 +156,26 @@ export async function formatters(options: OptionsFormatters | true = {}, stylist
},
});

if (options.svg)
configs.push({
files: [GLOB_SVG],
languageOptions: {
parser: parserPlain,
},
name: 'petal/formatter/svg',
rules: {
'format/prettier': [
'error',
{
...prettierXmlOptions,
...prettierOptions,
parser: 'xml',
plugins: ['@prettier/plugin-xml'],
},
],
},
});

if (options.markdown) {
const formatter = options.markdown === true
? 'prettier'
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-config/src/globs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const GLOB_GRAPHQL = '**/*.{g,graph}ql';
export const GLOB_YAML = '**/*.y?(a)ml';
export const GLOB_TOML = '**/*.toml';
export const GLOB_XML = '**/*.xml';
export const GLOB_SVG = '**/*.svg';
export const GLOB_HTML = '**/*.htm?(l)';
export const GLOB_BIN = '**/bin/**/*';
export const GLOB_BIN_SRC = `**/bin.${GLOB_SRC_EXT}`;
Expand Down
14 changes: 13 additions & 1 deletion packages/eslint-config/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,24 @@ export interface OptionsFormatters {
* Requires installing:
* - `@prettier/plugin-xml`
*
* Currently only support Prettier.
* Currently only supports Prettier.
*
* @default auto-detect based on the dependencies
*/
xml?: 'prettier' | boolean;

/**
* Enable formatting support for SVG.
*
* Requires installing:
* - `@prettier/plugin-xml`
*
* Currently only supports Prettier.
*
* @default auto-detect based on the dependencies
*/
svg?: 'prettier' | boolean;

/**
* Enable formatting support for Markdown.
*
Expand Down
41 changes: 21 additions & 20 deletions packages/utils/src/core/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import * as std from '@std/encoding';
import { decodeHex, encodeHex } from '@std/encoding/hex';
import { decodeBase32, encodeBase32 } from '@std/encoding/base32';
import { decodeBase58, encodeBase58 } from '@std/encoding/base58';
import { decodeBase64, encodeBase64 } from '@std/encoding/base64';
import { decodeBase64Url, encodeBase64Url } from '@std/encoding/base64url';
import { decodeAscii85, encodeAscii85 } from '@std/encoding/ascii85';
import nacl from 'tweetnacl';

export type BufferLike = Uint8Array | ArrayBuffer | string;
Expand All @@ -13,35 +18,35 @@ const te = new TextDecoder();
type Ascii85Standard = 'Adobe' | 'btoa' | 'RFC 1924' | 'Z85';
type Base64Standard = 'btoa' | 'url' | 'url_padded';

const encodeBase64UrlPadded = (data: Encodable): string => std.encodeBase64(data).replace(/\+/g, '-').replace(/\//g, '_');
const decodeBase64UrlPadded = (src: string): Uint8Array => std.decodeBase64(src.replace(/-/g, '+').replace(/_/g, '/'));
const encodeBase64UrlPadded = (data: Encodable): string => encodeBase64(data).replace(/\+/g, '-').replace(/\//g, '_');
const decodeBase64UrlPadded = (src: string): Uint8Array => decodeBase64(src.replace(/-/g, '+').replace(/_/g, '/'));

const enc = {
16: std.encodeHex,
32: std.encodeBase32,
58: std.encodeBase58,
16: encodeHex,
32: encodeBase32,
58: encodeBase58,
64: (data: Encodable, standard: Base64Standard = 'btoa') =>
({
btoa: std.encodeBase64,
url: std.encodeBase64Url,
btoa: encodeBase64,
url: encodeBase64Url,
url_padded: encodeBase64UrlPadded,
})[standard](data),
85: (data: Encodable, standard?: Ascii85Standard) =>
std.encodeAscii85(data, { standard }),
encodeAscii85(data, { standard }),
};

const dec = {
16: std.decodeHex,
32: std.decodeBase32,
58: std.decodeBase58,
16: decodeHex,
32: decodeBase32,
58: decodeBase58,
64: (src: string, standard: Base64Standard = 'btoa') =>
({
btoa: std.decodeBase64,
url: std.decodeBase64Url,
btoa: decodeBase64,
url: decodeBase64Url,
url_padded: decodeBase64UrlPadded,
})[standard](src),
85: (src: string, standard?: Ascii85Standard) =>
std.decodeAscii85(src, { standard }),
decodeAscii85(src, { standard }),
};

export type Base = Extract<keyof typeof enc, number>;
Expand All @@ -65,11 +70,7 @@ export default function base<T extends Base>(b: T, v?: Variation<T>): BaseProvid
export const rand: (n: number) => Uint8Array = nacl.randomBytes;
export const x25519: (n: Uint8Array) => Uint8Array = nacl.scalarMult.base;
export const sha512 = (msg: BufferLike): Uint8Array => nacl.hash(toUint8s(msg));
export function sha256(msg: BufferLike): Promise<Uint8Array> {
return crypto.subtle
.digest('SHA-256', toUint8s(msg))
.then(x => toUint8s(x));
}
export const sha256 = async (msg: BufferLike): Promise<Uint8Array> => toUint8s(await crypto.subtle.digest('SHA-256', toUint8s(msg)));
export function pair(pk?: Uint8Array): readonly [Uint8Array, Uint8Array] {
const priv = pk ?? rand(32);
const pub = x25519(priv);
Expand Down
Loading

0 comments on commit d244419

Please sign in to comment.