Skip to content

Commit

Permalink
feat: integrate slidev prettier plugin (#412)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu authored Mar 3, 2024
1 parent 5c628ff commit f3e91f9
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 9 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.4",
"eslint-plugin-svelte": "^2.34.1",
"prettier-plugin-slidev": "^1.0.5",
"svelte-eslint-parser": "^0.33.1"
},
"peerDependenciesMeta": {
Expand All @@ -65,6 +66,9 @@
"eslint-plugin-svelte": {
"optional": true
},
"prettier-plugin-slidev": {
"optional": true
},
"svelte-eslint-parser": {
"optional": true
}
Expand Down Expand Up @@ -132,6 +136,7 @@
"fast-glob": "^3.3.2",
"fs-extra": "^11.2.0",
"lint-staged": "^15.2.1",
"prettier-plugin-slidev": "^1.0.5",
"rimraf": "^5.0.5",
"simple-git-hooks": "^2.9.0",
"svelte": "^4.2.9",
Expand Down
37 changes: 34 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 41 additions & 4 deletions src/configs/formatters.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isPackageExists } from 'local-pkg'
import { GLOB_CSS, GLOB_LESS, GLOB_MARKDOWN, GLOB_POSTCSS, GLOB_SCSS } from '../globs'
import type { VendoredPrettierOptions } from '../vender/prettier-types'
import { ensurePackages, interopDefault, parserPlain } from '../utils'
Expand All @@ -8,19 +9,24 @@ export async function formatters(
options: OptionsFormatters | true = {},
stylistic: StylisticConfig = {},
): Promise<FlatConfigItem[]> {
await ensurePackages([
'eslint-plugin-format',
])

if (options === true) {
options = {
css: true,
graphql: true,
html: true,
markdown: true,
slidev: isPackageExists('@slidev/cli'),
}
}

await ensurePackages([
'eslint-plugin-format',
options.markdown && options.slidev ? 'prettier-plugin-slidev' : undefined,
])

if (options.slidev && options.markdown !== true && options.markdown !== 'prettier')
throw new Error('`slidev` option only works when `markdown` is enabled with `prettier`')

const {
indent,
quotes,
Expand Down Expand Up @@ -139,8 +145,15 @@ export async function formatters(
? 'prettier'
: options.markdown

const GLOB_SLIDEV = !options.slidev
? []
: options.slidev === true
? ['**/slides.md']
: options.slidev.files

configs.push({
files: [GLOB_MARKDOWN],
ignores: GLOB_SLIDEV,
languageOptions: {
parser: parserPlain,
},
Expand All @@ -162,6 +175,30 @@ export async function formatters(
],
},
})

if (options.slidev) {
configs.push({
files: GLOB_SLIDEV,
languageOptions: {
parser: parserPlain,
},
name: 'antfu:formatter:slidev',
rules: {
'format/prettier': [
'error',
{
printWidth: 120,
...prettierOptions,
embeddedLanguageFormatting: 'off',
parser: 'slidev',
plugins: [
'prettier-plugin-slidev',
],
},
],
},
})
}
}

if (options.graphql) {
Expand Down
9 changes: 9 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ export interface OptionsFormatters {
* By default it's controlled by our own config.
*/
dprintOptions?: boolean

/**
* Install the prettier plugin for handle Slidev markdown
*
* Only works when `markdown` is enabled with `prettier`.
*/
slidev?: boolean | {
files?: string[]
}
}

export interface OptionsComponentExts {
Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ export async function interopDefault<T>(m: Awaitable<T>): Promise<T extends { de
return (resolved as any).default || resolved
}

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

const nonExistingPackages = packages.filter(i => !isPackageExists(i))
const nonExistingPackages = packages.filter(i => i && !isPackageExists(i)) as string[]
if (nonExistingPackages.length === 0)
return

Expand Down

0 comments on commit f3e91f9

Please sign in to comment.