Skip to content

Commit 3c736c1

Browse files
committedAug 19, 2023
feat: export postcssIsolateStyles for vp-raw
1 parent 877f643 commit 3c736c1

File tree

6 files changed

+59
-14
lines changed

6 files changed

+59
-14
lines changed
 

‎docs/guide/getting-started.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ VitePress can be used on its own, or be installed into an existing project. In b
1818
::: code-group
1919

2020
```sh [npm]
21-
$ npm install -D vitepress
21+
$ npm add -D vitepress
2222
```
2323

2424
```sh [pnpm]
25-
$ pnpm add -D vitepress@latest
25+
$ pnpm add -D vitepress
2626
```
2727

2828
```sh [yarn]

‎docs/guide/markdown.md

+15-12
Original file line numberDiff line numberDiff line change
@@ -222,29 +222,32 @@ Wraps in a <div class="vp-raw">
222222

223223
::: details
224224

225-
- Install required deps with your preferred package manager:
225+
- Install `postcss` with your preferred package manager:
226226

227227
```sh
228-
$ npm install -D postcss postcss-prefix-selector
228+
$ npm add -D postcss
229229
```
230230

231-
- Create a file named `docs/.postcssrc.cjs` and add this to it:
231+
- Create a file named `docs/postcss.config.mjs` and add this to it:
232232

233233
```js
234-
module.exports = {
234+
import { postcssIsolateStyles } from 'vitepress'
235+
236+
export default {
235237
plugins: {
236-
'postcss-prefix-selector': {
237-
prefix: ':not(:where(.vp-raw *))',
238-
includeFiles: [/vp-doc\.css/],
239-
transform(prefix, _selector) {
240-
const [selector, pseudo = ''] = _selector.split(/(:\S*)$/)
241-
return selector + prefix + pseudo
242-
}
243-
}
238+
postcssIsolateStyles()
244239
}
245240
}
246241
```
247242

243+
It uses [`postcss-prefix-selector`](https://github.com/postcss/postcss-load-config) under the hood. You can pass its options like this:
244+
245+
```js
246+
postcssIsolateStyles({
247+
includeFiles: [/vp-doc\.css/] // defaults to /base\.css/
248+
})
249+
```
250+
248251
:::
249252

250253
## Syntax Highlighting in Code Blocks

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@
163163
"pkg-dir": "^7.0.0",
164164
"playwright-chromium": "^1.37.1",
165165
"polka": "1.0.0-next.22",
166+
"postcss-prefix-selector": "^1.16.0",
166167
"prettier": "^3.0.2",
167168
"prompts": "^2.4.2",
168169
"punycode": "^2.3.0",

‎pnpm-lock.yaml

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

‎src/node/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export * from './build/build'
55
export * from './serve/serve'
66
export * from './init/init'
77
export * from './contentLoader'
8+
export * from './postcss'
89
export { defineLoader, type LoaderModule } from './plugins/staticDataPlugin'
910
export { loadEnv } from 'vite'
1011

‎src/node/postcss/index.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// @ts-ignore
2+
import postcssPrefixSelector from 'postcss-prefix-selector'
3+
4+
export function postcssIsolateStyles(options: Options = {}) {
5+
if (options.enable === false) return false
6+
return postcssPrefixSelector({
7+
prefix: ':not(:where(.vp-raw, .vp-raw *))',
8+
includeFiles: [/base\.css/],
9+
transform(prefix, _selector) {
10+
const [selector, pseudo = ''] = _selector.split(/:\S*$/)
11+
return selector + prefix + pseudo
12+
},
13+
...options
14+
} satisfies Omit<Options, 'enable'>) as (root: any) => void
15+
}
16+
17+
interface Options {
18+
enable?: boolean
19+
prefix?: string
20+
exclude?: (string | RegExp)[]
21+
ignoreFiles?: (string | RegExp)[]
22+
includeFiles?: (string | RegExp)[]
23+
transform?: (
24+
prefix: string,
25+
selector: string,
26+
prefixedSelector: string,
27+
file: string
28+
) => string
29+
}

0 commit comments

Comments
 (0)
Please sign in to comment.