Skip to content

Commit

Permalink
feat: export postcssIsolateStyles for vp-raw
Browse files Browse the repository at this point in the history
  • Loading branch information
brc-dd committed Aug 19, 2023
1 parent 877f643 commit 3c736c1
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 14 deletions.
4 changes: 2 additions & 2 deletions docs/guide/getting-started.md
Expand Up @@ -18,11 +18,11 @@ VitePress can be used on its own, or be installed into an existing project. In b
::: code-group

```sh [npm]
$ npm install -D vitepress
$ npm add -D vitepress
```

```sh [pnpm]
$ pnpm add -D vitepress@latest
$ pnpm add -D vitepress
```

```sh [yarn]
Expand Down
27 changes: 15 additions & 12 deletions docs/guide/markdown.md
Expand Up @@ -222,29 +222,32 @@ Wraps in a <div class="vp-raw">

::: details

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

```sh
$ npm install -D postcss postcss-prefix-selector
$ npm add -D postcss
```

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

```js
module.exports = {
import { postcssIsolateStyles } from 'vitepress'

export default {
plugins: {
'postcss-prefix-selector': {
prefix: ':not(:where(.vp-raw *))',
includeFiles: [/vp-doc\.css/],
transform(prefix, _selector) {
const [selector, pseudo = ''] = _selector.split(/(:\S*)$/)
return selector + prefix + pseudo
}
}
postcssIsolateStyles()
}
}
```

It uses [`postcss-prefix-selector`](https://github.com/postcss/postcss-load-config) under the hood. You can pass its options like this:

```js
postcssIsolateStyles({
includeFiles: [/vp-doc\.css/] // defaults to /base\.css/
})
```

:::

## Syntax Highlighting in Code Blocks
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -163,6 +163,7 @@
"pkg-dir": "^7.0.0",
"playwright-chromium": "^1.37.1",
"polka": "1.0.0-next.22",
"postcss-prefix-selector": "^1.16.0",
"prettier": "^3.0.2",
"prompts": "^2.4.2",
"punycode": "^2.3.0",
Expand Down
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

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

1 change: 1 addition & 0 deletions src/node/index.ts
Expand Up @@ -5,6 +5,7 @@ export * from './build/build'
export * from './serve/serve'
export * from './init/init'
export * from './contentLoader'
export * from './postcss'
export { defineLoader, type LoaderModule } from './plugins/staticDataPlugin'
export { loadEnv } from 'vite'

Expand Down
29 changes: 29 additions & 0 deletions src/node/postcss/index.ts
@@ -0,0 +1,29 @@
// @ts-ignore
import postcssPrefixSelector from 'postcss-prefix-selector'

export function postcssIsolateStyles(options: Options = {}) {
if (options.enable === false) return false
return postcssPrefixSelector({
prefix: ':not(:where(.vp-raw, .vp-raw *))',
includeFiles: [/base\.css/],
transform(prefix, _selector) {
const [selector, pseudo = ''] = _selector.split(/:\S*$/)
return selector + prefix + pseudo
},
...options
} satisfies Omit<Options, 'enable'>) as (root: any) => void
}

interface Options {
enable?: boolean
prefix?: string
exclude?: (string | RegExp)[]
ignoreFiles?: (string | RegExp)[]
includeFiles?: (string | RegExp)[]
transform?: (
prefix: string,
selector: string,
prefixedSelector: string,
file: string
) => string
}

0 comments on commit 3c736c1

Please sign in to comment.