Skip to content

Commit 4113bbb

Browse files
committed
feat: integrate slidev prettier plugin
1 parent 1b3bae8 commit 4113bbb

File tree

5 files changed

+82
-6
lines changed

5 files changed

+82
-6
lines changed

package.json

+5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"eslint-plugin-react-hooks": "^4.6.0",
4646
"eslint-plugin-react-refresh": "^0.4.4",
4747
"eslint-plugin-svelte": "^2.34.1",
48+
"prettier-plugin-slidev": "^1.0.5",
4849
"svelte-eslint-parser": "^0.33.1"
4950
},
5051
"peerDependenciesMeta": {
@@ -66,6 +67,9 @@
6667
"eslint-plugin-svelte": {
6768
"optional": true
6869
},
70+
"prettier-plugin-slidev": {
71+
"optional": true
72+
},
6973
"svelte-eslint-parser": {
7074
"optional": true
7175
}
@@ -133,6 +137,7 @@
133137
"fast-glob": "^3.3.2",
134138
"fs-extra": "^11.2.0",
135139
"lint-staged": "^15.2.2",
140+
"prettier-plugin-slidev": "^1.0.5",
136141
"rimraf": "^5.0.5",
137142
"simple-git-hooks": "^2.9.0",
138143
"svelte": "^4.2.10",

pnpm-lock.yaml

+25
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,25 @@ 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+
}
30+
2431
const {
2532
indent,
2633
quotes,
@@ -139,8 +146,15 @@ export async function formatters(
139146
? "prettier"
140147
: options.markdown;
141148

149+
const GLOB_SLIDEV = !options.slidev
150+
? []
151+
: options.slidev === true
152+
? ["**/slides.md"]
153+
: options.slidev.files;
154+
142155
configs.push({
143156
files: [GLOB_MARKDOWN],
157+
ignores: GLOB_SLIDEV,
144158
languageOptions: {
145159
parser: parserPlain,
146160
},
@@ -162,6 +176,29 @@ export async function formatters(
162176
],
163177
},
164178
});
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
@@ -52,12 +52,12 @@ export async function interopDefault<T>(m: Awaitable<T>): Promise<T extends { de
5252
return (resolved as any).default || resolved;
5353
}
5454

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

60-
const nonExistingPackages = packages.filter(i => !isPackageExists(i));
60+
const nonExistingPackages = packages.filter(i => i && !isPackageExists(i)) as string[];
6161
if (nonExistingPackages.length === 0) {
6262
return;
6363
}

0 commit comments

Comments
 (0)