Skip to content

Commit f3e91f9

Browse files
authored
feat: integrate slidev prettier plugin (#412)
1 parent 5c628ff commit f3e91f9

File tree

5 files changed

+91
-9
lines changed

5 files changed

+91
-9
lines changed

package.json

+5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"eslint-plugin-react-hooks": "^4.6.0",
4545
"eslint-plugin-react-refresh": "^0.4.4",
4646
"eslint-plugin-svelte": "^2.34.1",
47+
"prettier-plugin-slidev": "^1.0.5",
4748
"svelte-eslint-parser": "^0.33.1"
4849
},
4950
"peerDependenciesMeta": {
@@ -65,6 +66,9 @@
6566
"eslint-plugin-svelte": {
6667
"optional": true
6768
},
69+
"prettier-plugin-slidev": {
70+
"optional": true
71+
},
6872
"svelte-eslint-parser": {
6973
"optional": true
7074
}
@@ -132,6 +136,7 @@
132136
"fast-glob": "^3.3.2",
133137
"fs-extra": "^11.2.0",
134138
"lint-staged": "^15.2.1",
139+
"prettier-plugin-slidev": "^1.0.5",
135140
"rimraf": "^5.0.5",
136141
"simple-git-hooks": "^2.9.0",
137142
"svelte": "^4.2.9",

pnpm-lock.yaml

+34-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/configs/formatters.ts

+41-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { isPackageExists } from 'local-pkg'
12
import { GLOB_CSS, GLOB_LESS, GLOB_MARKDOWN, GLOB_POSTCSS, GLOB_SCSS } from '../globs'
23
import type { VendoredPrettierOptions } from '../vender/prettier-types'
34
import { ensurePackages, interopDefault, parserPlain } from '../utils'
@@ -8,19 +9,24 @@ export async function formatters(
89
options: OptionsFormatters | true = {},
910
stylistic: StylisticConfig = {},
1011
): Promise<FlatConfigItem[]> {
11-
await ensurePackages([
12-
'eslint-plugin-format',
13-
])
14-
1512
if (options === true) {
1613
options = {
1714
css: true,
1815
graphql: true,
1916
html: true,
2017
markdown: true,
18+
slidev: isPackageExists('@slidev/cli'),
2119
}
2220
}
2321

22+
await ensurePackages([
23+
'eslint-plugin-format',
24+
options.markdown && options.slidev ? 'prettier-plugin-slidev' : undefined,
25+
])
26+
27+
if (options.slidev && options.markdown !== true && options.markdown !== 'prettier')
28+
throw new Error('`slidev` option only works when `markdown` is enabled with `prettier`')
29+
2430
const {
2531
indent,
2632
quotes,
@@ -139,8 +145,15 @@ export async function formatters(
139145
? 'prettier'
140146
: options.markdown
141147

148+
const GLOB_SLIDEV = !options.slidev
149+
? []
150+
: options.slidev === true
151+
? ['**/slides.md']
152+
: options.slidev.files
153+
142154
configs.push({
143155
files: [GLOB_MARKDOWN],
156+
ignores: GLOB_SLIDEV,
144157
languageOptions: {
145158
parser: parserPlain,
146159
},
@@ -162,6 +175,30 @@ export async function formatters(
162175
],
163176
},
164177
})
178+
179+
if (options.slidev) {
180+
configs.push({
181+
files: GLOB_SLIDEV,
182+
languageOptions: {
183+
parser: parserPlain,
184+
},
185+
name: 'antfu:formatter:slidev',
186+
rules: {
187+
'format/prettier': [
188+
'error',
189+
{
190+
printWidth: 120,
191+
...prettierOptions,
192+
embeddedLanguageFormatting: 'off',
193+
parser: 'slidev',
194+
plugins: [
195+
'prettier-plugin-slidev',
196+
],
197+
},
198+
],
199+
},
200+
})
201+
}
165202
}
166203

167204
if (options.graphql) {

src/types.ts

+9
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ export interface OptionsFormatters {
143143
* By default it's controlled by our own config.
144144
*/
145145
dprintOptions?: boolean
146+
147+
/**
148+
* Install the prettier plugin for handle Slidev markdown
149+
*
150+
* Only works when `markdown` is enabled with `prettier`.
151+
*/
152+
slidev?: boolean | {
153+
files?: string[]
154+
}
146155
}
147156

148157
export interface OptionsComponentExts {

src/utils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ export async function interopDefault<T>(m: Awaitable<T>): Promise<T extends { de
5151
return (resolved as any).default || resolved
5252
}
5353

54-
export async function ensurePackages(packages: string[]) {
54+
export async function ensurePackages(packages: (string | undefined)[]) {
5555
if (process.env.CI || process.stdout.isTTY === false)
5656
return
5757

58-
const nonExistingPackages = packages.filter(i => !isPackageExists(i))
58+
const nonExistingPackages = packages.filter(i => i && !isPackageExists(i)) as string[]
5959
if (nonExistingPackages.length === 0)
6060
return
6161

0 commit comments

Comments
 (0)