Skip to content

Commit 12718a4

Browse files
yunsiiantfu
andauthored
feat: formatters support SVG (#591)
Co-authored-by: Anthony Fu <github@antfu.me>
1 parent 8f08fb5 commit 12718a4

File tree

5 files changed

+60
-3
lines changed

5 files changed

+60
-3
lines changed

fixtures/input/svg.svg

+8
Loading
+17
Loading

src/configs/formatters.ts

+27-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { isPackageExists } from 'local-pkg'
2-
import { GLOB_ASTRO, GLOB_ASTRO_TS, GLOB_CSS, GLOB_GRAPHQL, GLOB_HTML, GLOB_LESS, GLOB_MARKDOWN, GLOB_POSTCSS, GLOB_SCSS, GLOB_XML } from '../globs'
2+
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'
33
import type { VendoredPrettierOptions } from '../vender/prettier-types'
44
import { ensurePackages, interopDefault, isPackageInScope, parserPlain } from '../utils'
55
import type { OptionsFormatters, StylisticConfig, TypedFlatConfigItem } from '../types'
@@ -10,22 +10,24 @@ export async function formatters(
1010
stylistic: StylisticConfig = {},
1111
): Promise<TypedFlatConfigItem[]> {
1212
if (options === true) {
13+
const isPrettierPluginXmlInScope = isPackageInScope('@prettier/plugin-xml')
1314
options = {
1415
astro: isPackageInScope('prettier-plugin-astro'),
1516
css: true,
1617
graphql: true,
1718
html: true,
1819
markdown: true,
1920
slidev: isPackageExists('@slidev/cli'),
20-
xml: isPackageInScope('@prettier/plugin-xml'),
21+
svg: isPrettierPluginXmlInScope,
22+
xml: isPrettierPluginXmlInScope,
2123
}
2224
}
2325

2426
await ensurePackages([
2527
'eslint-plugin-format',
2628
options.markdown && options.slidev ? 'prettier-plugin-slidev' : undefined,
2729
options.astro ? 'prettier-plugin-astro' : undefined,
28-
options.xml ? '@prettier/plugin-xml' : undefined,
30+
(options.xml || options.svg) ? '@prettier/plugin-xml' : undefined,
2931
])
3032

3133
if (options.slidev && options.markdown !== true && options.markdown !== 'prettier')
@@ -174,6 +176,28 @@ export async function formatters(
174176
},
175177
})
176178
}
179+
if (options.svg) {
180+
configs.push({
181+
files: [GLOB_SVG],
182+
languageOptions: {
183+
parser: parserPlain,
184+
},
185+
name: 'antfu/formatter/svg',
186+
rules: {
187+
'format/prettier': [
188+
'error',
189+
{
190+
...prettierXmlOptions,
191+
...prettierOptions,
192+
parser: 'xml',
193+
plugins: [
194+
'@prettier/plugin-xml',
195+
],
196+
},
197+
],
198+
},
199+
})
200+
}
177201

178202
if (options.markdown) {
179203
const formater = options.markdown === true

src/globs.ts

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export const GLOB_VUE = '**/*.vue'
2424
export const GLOB_YAML = '**/*.y?(a)ml'
2525
export const GLOB_TOML = '**/*.toml'
2626
export const GLOB_XML = '**/*.xml'
27+
export const GLOB_SVG = '**/*.svg'
2728
export const GLOB_HTML = '**/*.htm?(l)'
2829
export const GLOB_ASTRO = '**/*.astro'
2930
export const GLOB_ASTRO_TS = '**/*.astro/*.ts'

src/types.ts

+7
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ export interface OptionsFormatters {
7272
*/
7373
xml?: 'prettier' | boolean
7474

75+
/**
76+
* Enable formatting support for SVG.
77+
*
78+
* Currently only support Prettier.
79+
*/
80+
svg?: 'prettier' | boolean
81+
7582
/**
7683
* Enable formatting support for Markdown.
7784
*

0 commit comments

Comments
 (0)