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
feat(preset-tagify): new preset #1010
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d5ed819
feat: preset-tagify
zojize e6cc608
chore: fix build
zojize 1d67db8
Update packages/preset-tagify/src/extractor.ts
zojize a2679c8
chore: test tagify variants
zojize 7afa100
feat: append default extractor
antfu 94fe22f
feat: defaultExtractor option
antfu 7a9d0ca
perf: filter prefix on extractor
antfu 8f06b6c
docs: add license
antfu e7524ae
feat: include in `unocss`
antfu 98abe69
chore: update docs
antfu File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
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
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,22 @@ | ||
MIT License | ||
|
||
Copyright (c) 2022-PRESENT Jeff Zou <https://github.com/zojize> | ||
Copyright (c) 2022-PRESENT Anthony Fu <https://github.com/antfu> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,77 @@ | ||
# @unocss/preset-tagify | ||
|
||
Tagify Mode for [UnoCSS](https://github.com/unocss/unocss). | ||
|
||
## Installation | ||
|
||
```bash | ||
npm i -D @unocss/preset-tagify | ||
``` | ||
|
||
```ts | ||
import preseTagify from '@unocss/preset-tagify' | ||
|
||
Unocss({ | ||
presets: [ | ||
presetTagify({ /* options */ }), | ||
// ...other presets | ||
], | ||
}) | ||
``` | ||
|
||
## Tagify Mode | ||
|
||
This preset can come in handy when you only need a single unocss rule to be apply on an element. | ||
|
||
```html | ||
<span class="text-red"> red text </span> | ||
<div class="flex"> flexbox </div> | ||
I'm feeling <span class="i-line-md-emoji-grin"></span> today! | ||
``` | ||
|
||
With tagify mode, you can embed CSS styles into HTML tags: | ||
|
||
```html | ||
<text-red> red text </text-red> | ||
<flex> flexbox </flex> | ||
I'm feeling <i-line-md-emoji-grin> </i-line-md-emoji-grin> today! | ||
``` | ||
|
||
The HTML above works exactly as you would expect. | ||
|
||
## With Prefix | ||
|
||
```js | ||
presetTagify({ | ||
prefix: 'un-' | ||
}) | ||
``` | ||
|
||
```html | ||
<!-- this will be matched --> | ||
<un-flex> </un-flex> | ||
<!-- this will not be matched --> | ||
<flex> </flex> | ||
``` | ||
|
||
## Extra Properties | ||
|
||
You can inject extra properties to the matched rules: | ||
|
||
```js | ||
presetTagify({ | ||
// adds display: inline-block to matched icons | ||
extraProperties: matched => matched.startsWith('i-') | ||
? { display: 'inline-block' } | ||
: { } | ||
}) | ||
presetTagify({ | ||
// extraProperties can also be a plain object | ||
extraProperties: { display: 'block' } | ||
}) | ||
``` | ||
|
||
## License | ||
|
||
MIT License © 2022-PRESENT [Jeff Zou](https://github.com/zojize) | ||
MIT License © 2022-PRESENT [Anthony Fu](https://github.com/antfu) |
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,12 @@ | ||
import { defineBuildConfig } from 'unbuild' | ||
|
||
export default defineBuildConfig({ | ||
entries: [ | ||
'src/index', | ||
], | ||
clean: true, | ||
declaration: true, | ||
rollup: { | ||
emitCJS: true, | ||
}, | ||
}) |
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,42 @@ | ||
{ | ||
"name": "@unocss/preset-tagify", | ||
"version": "0.34.1", | ||
"description": "Tagify preset for UnoCSS", | ||
"author": "Anthony Fu <anthonyfu117@hotmail.com>", | ||
"license": "MIT", | ||
"funding": "https://github.com/sponsors/antfu", | ||
"homepage": "https://github.com/unocss/unocss/tree/main/packages/transformer-variant-group#readme", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/unocss/unocss.git", | ||
"directory": "packages/transformer-variant-group" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/unocss/unocss/issues" | ||
}, | ||
"keywords": [ | ||
"unocss", | ||
"unocss-preset" | ||
], | ||
"sideEffects": false, | ||
"exports": { | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"import": "./dist/index.mjs", | ||
"require": "./dist/index.cjs" | ||
} | ||
}, | ||
"main": "./dist/index.cjs", | ||
"module": "./dist/index.mjs", | ||
"types": "./dist/index.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "unbuild", | ||
"stub": "unbuild --stub" | ||
}, | ||
"dependencies": { | ||
"@unocss/core": "workspace:*" | ||
} | ||
} |
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,22 @@ | ||
import type { Extractor } from '@unocss/core' | ||
import type { TagifyOptions } from './types' | ||
|
||
export const MARKER = '__TAGIFY__' | ||
export const htmlTagRE = /<([\w\d-:]+)/g | ||
|
||
export const extractorTagify = (options: TagifyOptions): Extractor => { | ||
const { | ||
prefix = '', | ||
} = options | ||
|
||
return { | ||
name: 'tagify', | ||
extract({ code }) { | ||
return new Set( | ||
Array.from(code.matchAll(htmlTagRE)) | ||
.filter(({ 1: match }) => match.startsWith(prefix)) | ||
.map(([, matched]) => `${MARKER}${matched}`), | ||
) | ||
}, | ||
} | ||
} |
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,33 @@ | ||
import type { Preset } from '@unocss/core' | ||
import { extractorSplit } from '@unocss/core' | ||
import type { TagifyOptions } from './types' | ||
import { extractorTagify } from './extractor' | ||
import { variantTagify } from './variant' | ||
|
||
export * from './extractor' | ||
export * from './types' | ||
export * from './variant' | ||
|
||
const preset = (options: TagifyOptions = {}): Preset => { | ||
const { | ||
defaultExtractor = true, | ||
} = options | ||
|
||
const variants = [ | ||
variantTagify(options), | ||
] | ||
const extractors = [ | ||
extractorTagify(options), | ||
] | ||
|
||
if (defaultExtractor) | ||
extractors.push(extractorSplit) | ||
|
||
return { | ||
name: '@unocss/preset-tagify', | ||
variants, | ||
extractors, | ||
} | ||
} | ||
|
||
export default preset |
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,19 @@ | ||
export interface TagifyOptions { | ||
/** | ||
* The prefix to use for the tagify variant. | ||
*/ | ||
prefix?: string | ||
|
||
/** | ||
* Extra CSS properties to apply to matched rules | ||
*/ | ||
extraProperties?: | ||
| Record<string, string> | ||
| ((matched: string) => Partial<Record<string, string>>) | ||
|
||
/** | ||
* Enable default extractor | ||
* @default true | ||
*/ | ||
defaultExtractor?: boolean | ||
} |
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,31 @@ | ||
import type { VariantHandler, VariantObject } from '@unocss/core' | ||
import type { TagifyOptions } from './types' | ||
import { MARKER } from './extractor' | ||
|
||
export const variantTagify = (options: TagifyOptions): VariantObject => { | ||
const { extraProperties } = options | ||
const prefix = `${MARKER}${options.prefix ?? ''}` | ||
|
||
return { | ||
name: 'tagify', | ||
match(input) { | ||
if (!input.startsWith(prefix)) | ||
return | ||
|
||
const matcher = input.slice(prefix.length) | ||
const handler: VariantHandler = { | ||
matcher, | ||
selector: i => i.slice(MARKER.length + 1), | ||
} | ||
|
||
if (extraProperties) { | ||
if (typeof extraProperties === 'function') | ||
handler.body = entries => [...entries, ...Object.entries(extraProperties(matcher) ?? {})] | ||
else | ||
handler.body = entries => [...entries, ...Object.entries(extraProperties)] | ||
} | ||
|
||
return handler | ||
}, | ||
} | ||
} |
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
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 @@ | ||
export * from '@unocss/preset-attributify' |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zojize I have added your credit here. I will merge this for now, feel free to send another PR if you'd like to adjust this part. Thanks!