-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #399 from marp-team/css-nesting
Support for CSS nesting
- Loading branch information
Showing
11 changed files
with
352 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** @module */ | ||
import postcssIsPseudoClass from '@csstools/postcss-is-pseudo-class' | ||
import postcssNesting from 'postcss-nesting' | ||
import postcssPlugin from '../helpers/postcss_plugin' | ||
|
||
const { Rule: applyPostCSSNesting } = postcssNesting() | ||
|
||
const matcher = /:is\((?:section|:root)\b/ | ||
|
||
export const nesting = postcssPlugin( | ||
'marpit-postcss-nesting', | ||
() => (root, helpers) => { | ||
const rules = [] | ||
|
||
// Note: Use walk instead of walkRules to include nested rules | ||
root.walk((node) => { | ||
if (node.type !== 'rule') return | ||
|
||
rules.push(node) | ||
node.__marpitNestingOriginalSelector = node.selector | ||
}) | ||
|
||
// Apply postcss-nesting | ||
root.walkRules((rule) => applyPostCSSNesting(rule, helpers)) | ||
|
||
const { Rule: applyPostCSSIsPseudoClass } = postcssIsPseudoClass({ | ||
onComplexSelector: 'warning', | ||
}).prepare() | ||
|
||
for (const rule of rules) { | ||
if ( | ||
rule.__marpitNestingOriginalSelector !== rule.selector && | ||
matcher.test(rule.selector) | ||
) { | ||
// Apply postcss-is-pseudo-class only to transformed rules that is | ||
// including `:is() selector starting from `section` element or `:root` | ||
// pseudo-class | ||
applyPostCSSIsPseudoClass(rule, helpers) | ||
} | ||
|
||
delete rule.__marpitNestingOriginalSelector | ||
} | ||
}, | ||
) | ||
|
||
export default nesting |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.