Skip to content

Commit 39d0ce6

Browse files
committed
first commit
0 parents  commit 39d0ce6

File tree

11 files changed

+352
-0
lines changed

11 files changed

+352
-0
lines changed

.github/FUNDING.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
open_collective: css-doodle

.github/workflows/ci.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: CI
2+
3+
on:
4+
- push
5+
- pull_request
6+
7+
jobs:
8+
test:
9+
10+
runs-on: ${{ matrix.os }}
11+
12+
strategy:
13+
fail-fast: false
14+
matrix:
15+
os: [macos-latest, ubuntu-latest, windows-latest]
16+
node-version: [19.x]
17+
18+
steps:
19+
- uses: actions/checkout@v2
20+
- name: Use Node.js ${{ matrix.node-version }}
21+
uses: actions/setup-node@v2
22+
with:
23+
node-version: ${{ matrix.node-version }}
24+
- run: npm install
25+
- run: npm run build --if-present
26+
- run: npm test

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Yuan Chuan
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# postcss-doodle
2+
3+
![Build Status](https://github.com/css-doodle/postcss-doodle/actions/workflows/ci.yml/badge.svg)
4+
![license](https://img.shields.io/github/license/mashape/apistatus.svg)

index.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { svg, shape } from 'css-doodle/generator';
2+
import valueParser from 'postcss-value-parser';
3+
4+
export default function plugin() {
5+
return {
6+
postcssPlugin: "postcss-doodle",
7+
Declaration(decl) {
8+
let parsed = valueParser(decl.value);
9+
for (let node of parsed.nodes) {
10+
if (node.type === 'function' && isKeyword(node.value)) {
11+
let { result, args } = getInput(node.nodes);
12+
node.type = 'word'
13+
if (node.value === '@svg') {
14+
node.value = wrapSVG(svg(result));
15+
}
16+
if (node.value === '@shape') {
17+
node.value = shape(...args);
18+
}
19+
}
20+
}
21+
decl.value = parsed.toString();
22+
}
23+
}
24+
}
25+
26+
plugin.postcss = true;
27+
28+
function getInput(nodes) {
29+
let result = '', argResult = '';
30+
let args = [];
31+
for (let node of nodes) {
32+
let nested = (node.type === 'function' && Array.isArray(node.nodes));
33+
if (nested) {
34+
let value = `${node.value}(${getInput(node.nodes).result})`;
35+
result += value;
36+
argResult += value;
37+
} else {
38+
if (node.type === 'div' && node.value === ',') {
39+
args.push(argResult);
40+
argResult = '';
41+
} else {
42+
argResult += node.value;
43+
}
44+
result += node.value;
45+
}
46+
}
47+
if (argResult.length) {
48+
args.push(argResult);
49+
}
50+
return { result, args };
51+
}
52+
53+
function isKeyword(name) {
54+
return name === '@svg' || name === '@shape';
55+
}
56+
57+
function wrapSVG(input) {
58+
return `url(data:image/svg+xml;utf8,${ encodeURIComponent(input) })`;
59+
}

package-lock.json

Lines changed: 128 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "postcss-doodle",
3+
"type": "module",
4+
"scripts": {
5+
"test": "node --test test/*.test.js"
6+
},
7+
"devDependencies": {
8+
"css-doodle": "^0.32.0",
9+
"postcss": "^8.4.20",
10+
"postcss-value-parser": "^4.2.0"
11+
},
12+
"version": "1.0.0",
13+
"main": "index.js",
14+
"directories": {
15+
"test": "test"
16+
},
17+
"author": "",
18+
"license": "MIT",
19+
"description": ""
20+
}

test/compare.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import assert from 'assert';
2+
import postcss from 'postcss';
3+
import plugin from '../index.js';
4+
5+
async function render(input) {
6+
const { css } = await postcss()
7+
.use(plugin)
8+
.process(input, { from: null })
9+
return css;
10+
}
11+
12+
export default async function compare(code, result) {
13+
const input = await render(code);
14+
assert.equal(input.trim(), result.trim());
15+
}

test/shape.test.js

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)