diff --git a/package.json b/package.json index 6f14fc1f1a..f2b0fdd05b 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -65,6 +66,9 @@ "eslint-plugin-svelte": { "optional": true }, + "prettier-plugin-slidev": { + "optional": true + }, "svelte-eslint-parser": { "optional": true } @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 051e453826..07cdcdfa80 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -122,7 +122,7 @@ importers: version: 'link:' '@antfu/eslint-plugin-prettier': specifier: ^5.0.1-1 - version: 5.0.1-1(@types/eslint@8.56.2)(eslint-ts-patch@8.56.0-0)(prettier@3.1.0) + version: 5.0.1-1(@types/eslint@8.56.2)(eslint-ts-patch@8.56.0-0)(prettier@3.2.5) '@antfu/ni': specifier: ^0.21.12 version: 0.21.12 @@ -189,6 +189,9 @@ importers: lint-staged: specifier: ^15.2.1 version: 15.2.1 + prettier-plugin-slidev: + specifier: ^1.0.5 + version: 1.0.5(prettier@3.2.5) rimraf: specifier: ^5.0.5 version: 5.0.5 @@ -232,7 +235,7 @@ packages: engines: {node: '>=18.0.0', npm: '>=9.0.0', pnpm: '>= 8.6.0'} dev: false - /@antfu/eslint-plugin-prettier@5.0.1-1(@types/eslint@8.56.2)(eslint-ts-patch@8.56.0-0)(prettier@3.1.0): + /@antfu/eslint-plugin-prettier@5.0.1-1(@types/eslint@8.56.2)(eslint-ts-patch@8.56.0-0)(prettier@3.2.5): resolution: {integrity: sha512-Bmmb7SXmdrsPT/hPProT6UvPV0vvojq0kTA5KsKbFbFnMazrjYOT9rHffpSLHWlM6uCzF6OfolI0Cay5T+I6fg==} engines: {node: ^14.18.0 || >=16.0.0} peerDependencies: @@ -248,7 +251,7 @@ packages: dependencies: '@types/eslint': 8.56.2 eslint: /eslint-ts-patch@8.56.0-0 - prettier: 3.1.0 + prettier: 3.2.5 prettier-linter-helpers: 1.0.0 synckit: 0.8.8 dev: true @@ -949,6 +952,19 @@ packages: /@sinclair/typebox@0.27.8: resolution: {integrity: sha512-+Fj43pSMwJs4KRrH/938Uf+uAELIgVBmQzg/q1YG10djyfA3TnrU8N8XzqCh/okZdszqBQTZf96idMfE5lnwTA==} + /@slidev/parser@0.47.5: + resolution: {integrity: sha512-KvqOEhIFuMDu8CjAsehMNKlAnnmAn2TRHiEymc3CMadid05oWa5zgTxqEyMxUl2rjWpYs3A1yDiendZsGYs3HA==} + engines: {node: '>=18.0.0'} + dependencies: + '@slidev/types': 0.47.5 + js-yaml: 4.1.0 + dev: true + + /@slidev/types@0.47.5: + resolution: {integrity: sha512-X67V4cCgM0Sz50bP8GbVzmiL8DHC2IXvdKcsN7DlxHyf+/T4d9GveeGukwha5Fx3MuYeGZWKag7TFL2ZY4w54A==} + engines: {node: '>=18.0.0'} + dev: true + /@stylistic/eslint-plugin-js@1.5.4(eslint-ts-patch@8.56.0-0): resolution: {integrity: sha512-3ctWb3NvJNV1MsrZN91cYp2EGInLPSoZKphXIbIRx/zjZxKwLDr9z4LMOWtqjq14li/OgqUUcMq5pj8fgbLoTw==} engines: {node: ^16.0.0 || >=18.0.0} @@ -4173,12 +4189,27 @@ packages: fast-diff: 1.3.0 dev: true + /prettier-plugin-slidev@1.0.5(prettier@3.2.5): + resolution: {integrity: sha512-1xaKzOAX17QAcn1roUgQIAfeio3HPYYefCgzxh6Bzk+5rJJxq0I8HUhLrUyv9nCpIP6e9CDv/yDQSdEeqqRUuA==} + peerDependencies: + prettier: ^3.2.4 + dependencies: + '@slidev/parser': 0.47.5 + prettier: 3.2.5 + dev: true + /prettier@3.1.0: resolution: {integrity: sha512-TQLvXjq5IAibjh8EpBIkNKxO749UEWABoiIZehEPiY4GNpVdhaFKqSTu+QrlU6D2dPAfubRmtJTi4K4YkQ5eXw==} engines: {node: '>=14'} hasBin: true dev: true + /prettier@3.2.5: + resolution: {integrity: sha512-3/GWa9aOC0YeD7LUfvOG2NiDyhOWRvt1k+rcKhOuYnMY24iiCphgneUfJDyFXd6rZCAnuLBv6UeAULtrhT/F4A==} + engines: {node: '>=14'} + hasBin: true + dev: true + /pretty-format@29.7.0: resolution: {integrity: sha512-Pdlw/oPxN+aXdmM9R00JVC9WVFoCLTKJvDVLgmJ+qAffBMxsV85l/Lu7sNx4zSzPyoL2euImuEwHhOXdEgNFZQ==} engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} diff --git a/src/configs/formatters.ts b/src/configs/formatters.ts index d0c24d5078..c5398402b4 100644 --- a/src/configs/formatters.ts +++ b/src/configs/formatters.ts @@ -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' @@ -8,19 +9,24 @@ export async function formatters( options: OptionsFormatters | true = {}, stylistic: StylisticConfig = {}, ): Promise { - 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, @@ -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, }, @@ -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) { diff --git a/src/types.ts b/src/types.ts index 3b0697649c..7aeac55f9d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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 { diff --git a/src/utils.ts b/src/utils.ts index 5f62d0f477..cdb5a11464 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -51,11 +51,11 @@ export async function interopDefault(m: Awaitable): Promise !isPackageExists(i)) + const nonExistingPackages = packages.filter(i => i && !isPackageExists(i)) as string[] if (nonExistingPackages.length === 0) return