@pantoken/inline-styles 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/cli.d.mts +4 -0
- package/dist/cli.mjs +79 -0
- package/dist/html.d.mts +11 -0
- package/dist/html.mjs +2 -0
- package/dist/index.d.mts +3 -0
- package/dist/index.mjs +1 -0
- package/dist/inline-html-Bg3Fqfrb.mjs +96 -0
- package/dist/pantoken-html.d.mts +23 -0
- package/dist/pantoken-html.mjs +71 -0
- package/package.json +59 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Danny Wahl
|
|
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.
|
package/dist/cli.d.mts
ADDED
package/dist/cli.mjs
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { inlinePantokenHtml } from "./pantoken-html.mjs";
|
|
3
|
+
import { readFile, writeFile } from "node:fs/promises";
|
|
4
|
+
import { pathToFileURL } from "node:url";
|
|
5
|
+
import { parseArgs } from "node:util";
|
|
6
|
+
//#region src/cli.ts
|
|
7
|
+
const THEMES = [
|
|
8
|
+
"rebrand",
|
|
9
|
+
"canvas",
|
|
10
|
+
"canvasHighContrast"
|
|
11
|
+
];
|
|
12
|
+
function parseTheme(value) {
|
|
13
|
+
if (value === void 0) return void 0;
|
|
14
|
+
if (THEMES.includes(value)) return value;
|
|
15
|
+
throw new Error(`Invalid theme "${value}". Choose rebrand, canvas, or canvasHighContrast.`);
|
|
16
|
+
}
|
|
17
|
+
function parseMode(value) {
|
|
18
|
+
if (value === void 0 || value === "light" || value === "dark") return value;
|
|
19
|
+
throw new Error(`Invalid mode "${value}". Choose light or dark.`);
|
|
20
|
+
}
|
|
21
|
+
/** Run the HTML inliner CLI. */
|
|
22
|
+
async function runCli(args = process.argv.slice(2)) {
|
|
23
|
+
const parsed = parseArgs({
|
|
24
|
+
args,
|
|
25
|
+
options: {
|
|
26
|
+
input: {
|
|
27
|
+
type: "string",
|
|
28
|
+
short: "i"
|
|
29
|
+
},
|
|
30
|
+
output: {
|
|
31
|
+
type: "string",
|
|
32
|
+
short: "o"
|
|
33
|
+
},
|
|
34
|
+
css: {
|
|
35
|
+
type: "string",
|
|
36
|
+
short: "c"
|
|
37
|
+
},
|
|
38
|
+
theme: { type: "string" },
|
|
39
|
+
mode: { type: "string" },
|
|
40
|
+
prefix: { type: "string" },
|
|
41
|
+
"custom-color": { type: "string" },
|
|
42
|
+
help: {
|
|
43
|
+
type: "boolean",
|
|
44
|
+
short: "h"
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
allowPositionals: false
|
|
48
|
+
});
|
|
49
|
+
const { input, output, css, help, theme, mode, prefix } = parsed.values;
|
|
50
|
+
const customColor = parsed.values["custom-color"];
|
|
51
|
+
if (help) {
|
|
52
|
+
process.stdout.write("Usage: pantoken-inline [--theme <name>] [--mode <light|dark>] [--prefix <name>] [--custom-color <hex>] [--css <file>] [--input <file>] [--output <file>]\n");
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
const selectedTheme = parseTheme(theme);
|
|
56
|
+
const selectedMode = parseMode(mode);
|
|
57
|
+
const html = input ? await readFile(input, "utf8") : await readStdin();
|
|
58
|
+
const extraCss = css ? await readFile(css, "utf8") : void 0;
|
|
59
|
+
const result = inlinePantokenHtml(html, {
|
|
60
|
+
theme: selectedTheme,
|
|
61
|
+
mode: selectedMode,
|
|
62
|
+
prefix,
|
|
63
|
+
customColor,
|
|
64
|
+
extraCss
|
|
65
|
+
});
|
|
66
|
+
if (output) await writeFile(output, result);
|
|
67
|
+
else process.stdout.write(result);
|
|
68
|
+
}
|
|
69
|
+
async function readStdin() {
|
|
70
|
+
const chunks = [];
|
|
71
|
+
for await (const chunk of process.stdin) chunks.push(Buffer.from(chunk));
|
|
72
|
+
return Buffer.concat(chunks).toString("utf8");
|
|
73
|
+
}
|
|
74
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) runCli().catch((error) => {
|
|
75
|
+
process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
|
|
76
|
+
process.exitCode = 1;
|
|
77
|
+
});
|
|
78
|
+
//#endregion
|
|
79
|
+
export { runCli };
|
package/dist/html.d.mts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
//#region src/inline-html.d.ts
|
|
2
|
+
/** Options for inlining a stylesheet into an HTML document. */
|
|
3
|
+
export interface InlineHtmlOptions {
|
|
4
|
+
/** Keep media-query and pseudo-class rules in a style element. */
|
|
5
|
+
preserveFallbacks?: boolean;
|
|
6
|
+
/** Resolve custom-property references against declarations in the document. */
|
|
7
|
+
resolveCSSVariables?: boolean;
|
|
8
|
+
}
|
|
9
|
+
/** Inline CSS rules into matching HTML elements without fetching external resources. */
|
|
10
|
+
export declare function inlineHtml(html: string, css: string, options?: InlineHtmlOptions): string;
|
|
11
|
+
//#endregion
|
package/dist/html.mjs
ADDED
package/dist/index.d.mts
ADDED
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import juice from "juice";
|
|
2
|
+
import postcss from "postcss";
|
|
3
|
+
//#region src/flatten-scopes.ts
|
|
4
|
+
function splitSelectors(selectors) {
|
|
5
|
+
const results = [];
|
|
6
|
+
let start = 0;
|
|
7
|
+
let parentheses = 0;
|
|
8
|
+
let brackets = 0;
|
|
9
|
+
let quote = "";
|
|
10
|
+
for (let index = 0; index < selectors.length; index += 1) {
|
|
11
|
+
const character = selectors[index];
|
|
12
|
+
if (quote) {
|
|
13
|
+
if (character === "\\") index += 1;
|
|
14
|
+
else if (character === quote) quote = "";
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
if (character === "\"" || character === "'") quote = character;
|
|
18
|
+
else if (character === "(") parentheses += 1;
|
|
19
|
+
else if (character === ")") parentheses -= 1;
|
|
20
|
+
else if (character === "[") brackets += 1;
|
|
21
|
+
else if (character === "]") brackets -= 1;
|
|
22
|
+
else if (character === "," && parentheses === 0 && brackets === 0) {
|
|
23
|
+
results.push(selectors.slice(start, index).trim());
|
|
24
|
+
start = index + 1;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
results.push(selectors.slice(start).trim());
|
|
28
|
+
return results.filter(Boolean);
|
|
29
|
+
}
|
|
30
|
+
function scopeSelectors(params) {
|
|
31
|
+
const start = params.indexOf("(");
|
|
32
|
+
if (start < 0) throw new SyntaxError(`Unsupported @scope parameters: ${params}`);
|
|
33
|
+
let depth = 0;
|
|
34
|
+
let end = -1;
|
|
35
|
+
for (let index = start; index < params.length; index += 1) if (params[index] === "(") depth += 1;
|
|
36
|
+
else if (params[index] === ")") {
|
|
37
|
+
depth -= 1;
|
|
38
|
+
if (depth === 0) {
|
|
39
|
+
end = index;
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (end < 0 || params.slice(end + 1).trim()) throw new SyntaxError(`Unsupported @scope parameters: ${params}`);
|
|
44
|
+
const selectors = splitSelectors(params.slice(start + 1, end));
|
|
45
|
+
if (selectors.length === 0) throw new SyntaxError(`Empty @scope root: ${params}`);
|
|
46
|
+
return selectors;
|
|
47
|
+
}
|
|
48
|
+
function combineSelectors(parents, children) {
|
|
49
|
+
return splitSelectors(children).flatMap((child) => parents.map((parent) => child.includes("&") ? child.replaceAll("&", parent) : `${parent} ${child}`)).join(", ");
|
|
50
|
+
}
|
|
51
|
+
function flattenContainer(container, parents) {
|
|
52
|
+
for (const node of container.nodes?.slice() ?? []) {
|
|
53
|
+
if (node.type === "atrule" && node.name.toLowerCase() === "scope") {
|
|
54
|
+
flattenContainer(node, scopeSelectors(node.params));
|
|
55
|
+
const children = [...node.nodes ?? []];
|
|
56
|
+
if (children.length) node.replaceWith(...children);
|
|
57
|
+
else node.remove();
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
if (node.type === "rule") {
|
|
61
|
+
const rule = node;
|
|
62
|
+
if (parents) rule.selector = combineSelectors(parents, rule.selector);
|
|
63
|
+
flattenContainer(rule, splitSelectors(rule.selector));
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (node.type === "atrule" && node.nodes) {
|
|
67
|
+
const atRule = node;
|
|
68
|
+
flattenContainer(atRule, /(?:^|-)(?:webkit-)?keyframes$/iu.test(atRule.name) ? void 0 : parents);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/** Flatten simple CSS `@scope` blocks to selectors Juice can process. */
|
|
73
|
+
function flattenScopes(css) {
|
|
74
|
+
const root = postcss.parse(css);
|
|
75
|
+
flattenContainer(root);
|
|
76
|
+
return root.toString();
|
|
77
|
+
}
|
|
78
|
+
//#endregion
|
|
79
|
+
//#region src/inline-html.ts
|
|
80
|
+
/** Inline CSS rules into matching HTML elements without fetching external resources. */
|
|
81
|
+
function inlineHtml(html, css, options = {}) {
|
|
82
|
+
const { preserveFallbacks = true, resolveCSSVariables = false } = options;
|
|
83
|
+
const safeCss = flattenScopes(css).replace(/</gu, "\\3C ");
|
|
84
|
+
return juice(`<style>${safeCss}</style>${html}`, {
|
|
85
|
+
preserveContainerQueries: preserveFallbacks,
|
|
86
|
+
preserveFontFaces: preserveFallbacks,
|
|
87
|
+
preserveKeyFrames: preserveFallbacks,
|
|
88
|
+
preserveLayers: preserveFallbacks,
|
|
89
|
+
preserveMediaQueries: preserveFallbacks,
|
|
90
|
+
preservePseudos: preserveFallbacks,
|
|
91
|
+
removeStyleTags: true,
|
|
92
|
+
resolveCSSVariables
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
//#endregion
|
|
96
|
+
export { inlineHtml as t };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { PantokenColorNamespace } from "@pantoken/plugin-custom-theme-colors";
|
|
2
|
+
import { Theme } from "@pantoken/model";
|
|
3
|
+
//#region src/pantoken-html.d.ts
|
|
4
|
+
/** Options for inlining pantoken component styles. */
|
|
5
|
+
export interface PantokenHtmlOptions {
|
|
6
|
+
/** Token theme to use. Defaults to `rebrand`. */
|
|
7
|
+
theme?: Theme;
|
|
8
|
+
/** Color scheme to resolve. Defaults to `light`. */
|
|
9
|
+
mode?: "light" | "dark";
|
|
10
|
+
/** Component class prefix. Defaults to `instui`. */
|
|
11
|
+
prefix?: string | null;
|
|
12
|
+
/** Add generated rules for this custom brand color. */
|
|
13
|
+
customColor?: string;
|
|
14
|
+
/** Apply one of the plugin's shipped or custom color scales. */
|
|
15
|
+
color?: PantokenColorNamespace;
|
|
16
|
+
/** Retain pseudo-class and media-query rules in a style element. */
|
|
17
|
+
preserveFallbacks?: boolean;
|
|
18
|
+
/** Additional caller-supplied CSS. */
|
|
19
|
+
extraCss?: string;
|
|
20
|
+
}
|
|
21
|
+
/** Inline pantoken tokens and component CSS into matching HTML elements. */
|
|
22
|
+
export declare function inlinePantokenHtml(html: string, options?: PantokenHtmlOptions): string;
|
|
23
|
+
//#endregion
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { t as inlineHtml } from "./inline-html-Bg3Fqfrb.mjs";
|
|
2
|
+
import { componentsCss } from "@pantoken/components";
|
|
3
|
+
import { load } from "cheerio";
|
|
4
|
+
import { COLOR_KEYS, CUSTOM_COLOR_KEY, customThemeColorsCss } from "@pantoken/plugin-custom-theme-colors";
|
|
5
|
+
import { byTheme } from "@pantoken/tokens";
|
|
6
|
+
import { resolveTokens } from "@pantoken/utils";
|
|
7
|
+
//#region src/pantoken-html.ts
|
|
8
|
+
function modeValue(value, mode) {
|
|
9
|
+
const start = value.indexOf("light-dark(");
|
|
10
|
+
if (start < 0) return value;
|
|
11
|
+
let depth = 1;
|
|
12
|
+
let comma = -1;
|
|
13
|
+
let end = -1;
|
|
14
|
+
for (let index = start + 11; index < value.length; index += 1) if (value[index] === "(") depth += 1;
|
|
15
|
+
else if (value[index] === ")") {
|
|
16
|
+
depth -= 1;
|
|
17
|
+
if (depth === 0) {
|
|
18
|
+
end = index;
|
|
19
|
+
break;
|
|
20
|
+
}
|
|
21
|
+
} else if (value[index] === "," && depth === 1 && comma < 0) comma = index;
|
|
22
|
+
if (comma < 0 || end < 0) return value;
|
|
23
|
+
const selected = mode === "light" ? value.slice(start + 11, comma) : value.slice(comma + 1, end);
|
|
24
|
+
return `${value.slice(0, start)}${modeValue(selected.trim(), mode)}${value.slice(end + 1)}`;
|
|
25
|
+
}
|
|
26
|
+
function resolvedThemeTokens(theme, mode) {
|
|
27
|
+
const tokens = byTheme(theme).map((token) => ({
|
|
28
|
+
...token,
|
|
29
|
+
value: modeValue(token.value, mode),
|
|
30
|
+
flatValue: token.flatValue ? modeValue(token.flatValue, mode) : void 0
|
|
31
|
+
}));
|
|
32
|
+
return {
|
|
33
|
+
tokens,
|
|
34
|
+
resolved: resolveTokens(tokens)
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function tokenStylesheet(tokens, resolved) {
|
|
38
|
+
return `:root {\n${tokens.map((token) => ` ${token.name}: ${resolved.get(token.name) ?? token.value};`).join("\n")}\n}`;
|
|
39
|
+
}
|
|
40
|
+
function markColorScope(html, color) {
|
|
41
|
+
const $ = load(html, {}, false);
|
|
42
|
+
const documentRoot = $("html").first();
|
|
43
|
+
(documentRoot.length ? documentRoot : $.root().children().filter((_, node) => node.type === "tag")).attr("data-pantoken-color", color);
|
|
44
|
+
return $.html();
|
|
45
|
+
}
|
|
46
|
+
/** Inline pantoken tokens and component CSS into matching HTML elements. */
|
|
47
|
+
function inlinePantokenHtml(html, options = {}) {
|
|
48
|
+
const { theme = "rebrand", mode = "light", prefix = "instui", customColor, color, preserveFallbacks = false, extraCss = "" } = options;
|
|
49
|
+
const activeColor = color ?? (customColor ? CUSTOM_COLOR_KEY : void 0);
|
|
50
|
+
if (activeColor && activeColor !== CUSTOM_COLOR_KEY && !COLOR_KEYS.includes(activeColor)) throw new Error(`Unknown pantoken color scale: ${activeColor}`);
|
|
51
|
+
if (activeColor === CUSTOM_COLOR_KEY && !customColor) throw new Error("The \"custom\" color scale requires a customColor hex value.");
|
|
52
|
+
const { tokens, resolved } = resolvedThemeTokens(theme, mode);
|
|
53
|
+
const css = [
|
|
54
|
+
tokenStylesheet(tokens, resolved),
|
|
55
|
+
customThemeColorsCss(resolved, {
|
|
56
|
+
custom: customColor,
|
|
57
|
+
selector: ""
|
|
58
|
+
}),
|
|
59
|
+
componentsCss({
|
|
60
|
+
theme,
|
|
61
|
+
prefix
|
|
62
|
+
}),
|
|
63
|
+
extraCss
|
|
64
|
+
].join("\n");
|
|
65
|
+
return inlineHtml(activeColor ? markColorScope(html, activeColor) : html, css, {
|
|
66
|
+
resolveCSSVariables: true,
|
|
67
|
+
preserveFallbacks
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
//#endregion
|
|
71
|
+
export { inlinePantokenHtml };
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pantoken/inline-styles",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Inline CSS into HTML with pantoken styles.",
|
|
5
|
+
"homepage": "https://pantoken.app",
|
|
6
|
+
"bugs": "https://github.com/thedannywahl/pantoken/issues",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/thedannywahl/pantoken.git",
|
|
11
|
+
"directory": "formats/inline-styles"
|
|
12
|
+
},
|
|
13
|
+
"bin": {
|
|
14
|
+
"pantoken-inline": "./dist/cli.mjs"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"type": "module",
|
|
20
|
+
"sideEffects": false,
|
|
21
|
+
"exports": {
|
|
22
|
+
".": "./dist/index.mjs",
|
|
23
|
+
"./cli": "./dist/cli.mjs",
|
|
24
|
+
"./html": "./dist/html.mjs",
|
|
25
|
+
"./pantoken-html": "./dist/pantoken-html.mjs",
|
|
26
|
+
"./package.json": "./package.json"
|
|
27
|
+
},
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public",
|
|
30
|
+
"provenance": true
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@pantoken/components": "2.0.0",
|
|
34
|
+
"@pantoken/model": "0.5.0",
|
|
35
|
+
"@pantoken/plugin-custom-theme-colors": "0.3.1",
|
|
36
|
+
"@pantoken/tokens": "0.7.0",
|
|
37
|
+
"@pantoken/utils": "1.2.0",
|
|
38
|
+
"cheerio": "^1.2.0",
|
|
39
|
+
"juice": "^12.2.0",
|
|
40
|
+
"postcss": "^8.5.28"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@types/node": "^26.6.3",
|
|
44
|
+
"typescript": "^7.0.2",
|
|
45
|
+
"vite": "npm:@voidzero-dev/vite-plus-core@1.0.0-rc.0",
|
|
46
|
+
"vite-plus": "1.0.0-rc.0"
|
|
47
|
+
},
|
|
48
|
+
"engines": {
|
|
49
|
+
"node": ">=22.18.0"
|
|
50
|
+
},
|
|
51
|
+
"pantoken": {
|
|
52
|
+
"key": "inlineStyles",
|
|
53
|
+
"kind": "namespace"
|
|
54
|
+
},
|
|
55
|
+
"scripts": {
|
|
56
|
+
"test": "vp test",
|
|
57
|
+
"check": "vp check"
|
|
58
|
+
}
|
|
59
|
+
}
|