Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding ignore feature #70

Merged
merged 8 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,17 @@ require('postcss-font-magician')({
By default, 'http/https' protocol is removed from the font url.
Supports any string values, eg - '', 'http:' or 'https:'

## Ignore

If you don't need Font Magician in some part of your CSS, you can use control comments to disable it.

```css

body {
/* font-magician: ignore-next */
font-family: "Alice"; /* will not generate font-face */
}
```

## Future

Expand Down
18 changes: 16 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,18 @@ function getFontFaceRules(family, foundries, options) {
return fontFaceRules;
}

function isRuleIgnored(rule) {
let previous = rule.prev();

if (
previous &&
previous.type === 'comment' &&
/(!\s*)?font-magician:\s*ignore(-|\s+)next/i.test(previous.text)
) {
return true;
}
}

function plugin(initialOptions) {
// get configured option
const options = getConfiguredOptions(initialOptions || {});
Expand Down Expand Up @@ -356,8 +368,10 @@ function plugin(initialOptions) {
// set the font family
const family = getQuoteless(decl.value);

// set the font family as declared
fontFamiliesDeclared[family] = true;
if (!isRuleIgnored(decl)) {
// set the font family as declared
fontFamiliesDeclared[family] = true;
}
});
});

Expand Down
20 changes: 20 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,4 +375,24 @@ describe('postcss-font-magician', function() {
done
);
});

it('supports ignoring a declaration', function (done) {
test(
'a{/* font-magician: ignore next */font-family:"Alice"}b{}',

'a{/* font-magician: ignore next */font-family:"Alice"}b{}',

{},
done
);

test(
'a{/* font-magician: ignore-next */font-family:"Alice"}b{}',

'a{/* font-magician: ignore-next */font-family:"Alice"}b{}',

{},
done
);
});
});