|
1 | 1 | "use strict";
|
2 | 2 |
|
3 |
| -const valueParser = require("postcss-value-parser"); |
| 3 | +import valueParser from "postcss-value-parser"; |
4 | 4 |
|
5 |
| -//const { gettxt } = require("./tools.mjs"); |
| 5 | +import { gettxt } from "./tools.mjs"; |
6 | 6 |
|
7 |
| -const DEFAULT_OPTS = { |
| 7 | +const OPTIONS = { |
8 | 8 | rootFontSize: "16px",
|
9 | 9 | fontSize: "16px",
|
10 | 10 | fontWeight: 400,
|
11 | 11 | usage: 4,
|
12 | 12 | };
|
13 | 13 |
|
| 14 | +function plugin(options = {}) { |
| 15 | + const { rootFontSize, fontSize, fontWeight, usage, } = Object.assign(OPTIONS, options); |
| 16 | + |
| 17 | + let curM = OPTIONS.rootM = getFontSizePX(rootFontSize); |
| 18 | + |
| 19 | + return { |
| 20 | + postcssPlugin: "postcss-accessibility", |
| 21 | + |
| 22 | + Declaration(decl) { |
| 23 | + const values = decl.value.split(/(?!\(.*)\s(?![^(]*?\))/g); |
| 24 | + |
| 25 | + for (const valueIndex in values) { |
| 26 | + const value = values[valueIndex]; |
| 27 | + if (value.startsWith("a11y-txt(")) { |
| 28 | + const params = valueParser(value).nodes[0].nodes |
| 29 | + .filter(i => i.type !== "div") |
| 30 | + .map(i => { |
| 31 | + switch (i.type) { |
| 32 | + case "word": return i.value; |
| 33 | + case "function": return `${i.value}(${i.nodes.filter(i => i.type !== "div").map(i => i.value).join("")})`; |
| 34 | + } |
| 35 | + }); |
| 36 | + decl.value = gettxt(params[0], "txt", `${getFontSizePX(params[1] ?? fontSize, curM)}px`, params[2] ?? fontWeight, params[3] ?? usage); |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | +plugin.postcss = true; |
| 43 | + |
14 | 44 | /**
|
15 | 45 | * @type {import('postcss').PluginCreator}
|
16 | 46 | */
|
17 |
| -module.exports = (opts = {}) => { |
18 |
| - const { rootFontSize, fontSize, fontWeight, usage, } = Object.assign(DEFAULT_OPTS, opts); |
| 47 | +export default plugin; |
| 48 | + |
| 49 | +function getFontSizePX(fontSize, M = OPTIONS.rootM) { |
| 50 | + if (typeof fontSize !== "string" && !(fontSize instanceof String)) return; |
19 | 51 |
|
20 |
| - if (typeof rootFontSize !== "string" && !(rootFontSize instanceof String)) return; |
| 52 | + let [, value, unit] = fontSize.match(/^(\d*\.?\d+)(px|cm|mm|Q|in|pc|pt|rem|em)$/) ?? []; |
21 | 53 |
|
22 |
| - let [, rootFontSizeValue, rootFontSizeUnit] = rootFontSize.match(/^(\d*\.?\d+)(px|cm|mm|Q|in|pc|pt)$/) ?? []; |
| 54 | + if (unit.includes("em") && !OPTIONS.rootM) return; |
23 | 55 |
|
24 |
| - if (rootFontSizeValue == null || !rootFontSizeUnit == null) return; |
| 56 | + if (value == null || !unit == null) return; |
25 | 57 |
|
26 |
| - if (rootFontSizeUnit.startsWith(".")) rootFontSizeUnit = 0 + rootFontSizeUnit; |
| 58 | + if (unit.startsWith(".")) unit = 0 + unit; |
27 | 59 |
|
28 |
| - switch (rootFontSizeUnit) { |
29 |
| - case "px": break; |
30 |
| - case "cm": rootFontSizeValue *= 96/2.54; break; |
31 |
| - case "mm": rootFontSizeValue *= 96/2.54/10; break; |
32 |
| - case "Q": rootFontSizeValue *= 96/2.54/10*4; break; |
33 |
| - case "in": rootFontSizeValue *= 96; break; |
34 |
| - case "pc": rootFontSizeValue *= 96/6; break; |
35 |
| - case "pt": rootFontSizeValue *= 96/72; break; |
| 60 | + switch (unit) { |
| 61 | + case "em": return value * M; |
| 62 | + case "rem": return value * OPTIONS.rootM; |
| 63 | + case "px": return value; |
| 64 | + case "cm": return value * 96/2.54; |
| 65 | + case "mm": return value * 96/2.54/10; |
| 66 | + case "Q": return value * 96/2.54/10*4; |
| 67 | + case "in": return value * 96; |
| 68 | + case "pc": return value * 96/6; |
| 69 | + case "pt": return value * 96/72; |
36 | 70 | default: return;
|
37 | 71 | }
|
38 |
| - |
39 |
| - console.log("fs: ", rootFontSizeValue); |
40 |
| - |
41 |
| - // Work with options here |
42 |
| - return { |
43 |
| - postcssPlugin: 'postcss-accessibility', |
44 |
| - //Rule: rule => { |
45 |
| - // console.log(rule); |
46 |
| - //}, |
47 |
| - Declaration: decl => { |
48 |
| - if (decl.value.startsWith("a11y-txt(")) { |
49 |
| - const params = valueParser(decl.value).nodes[0].nodes; |
50 |
| - //console.log(/*gettxt(*/params[0].value, "txt", ...Object.values(opts)/*)*/); |
51 |
| - } |
52 |
| - }, |
53 |
| - }; |
54 | 72 | }
|
55 |
| - |
56 |
| -module.exports.postcss = true; |
|
0 commit comments