Skip to content

Commit

Permalink
fix: support new fontawesome6 css glyph approach
Browse files Browse the repository at this point in the history
  • Loading branch information
johnf committed Nov 25, 2024
1 parent 925d107 commit dca0985
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const extractGlyphMapFromCss = (fileName: string, selectorPrefix: string) => {
const css = fs.readFileSync(fileName, 'utf-8');
const glyphMap: Record<string, number> = {};

const selectorPattern = `${escapeRegExp(selectorPrefix)}([A-Za-z0-9_-]+):?before`;
const selectorPattern = `${escapeRegExp(selectorPrefix)}([A-Za-z0-9_-]+)::?before`;
postcss.parse(css).walkRules((rule) => {
const iconNames: string[] = [];
const transform = (selectors: parser.Root) => {
Expand All @@ -42,6 +42,38 @@ const extractGlyphMapFromCss = (fileName: string, selectorPrefix: string) => {
contents.push(content);
});

const content = contents[0];
if (!content || content === 'var(--fa)') {
return;
}
const codePoint = Number.parseInt(content.slice(1), 16);

iconNames.forEach((iconName) => {
glyphMap[iconName] = codePoint;
});
});

// TODO: Quick hack for fontawesome - refactor this to be more general
const selectorPatternFA = `${escapeRegExp(selectorPrefix)}([A-Za-z0-9_-]+)$`;
postcss.parse(css).walkRules((rule) => {
const iconNames: string[] = [];
const transform = (selectors: parser.Root) => {
selectors.walk((selector) => {
const md = selector.toString().match(selectorPatternFA);
if (md?.[1]) {
iconNames.push(md[1]);
}
});
};

parser(transform).processSync(rule.selector);

const contents: string[] = [];
rule.walkDecls('--fa', (decl) => {
const content = decl.value.replace(/['"]/g, ''); // Remove quotes
contents.push(content);
});

const content = contents[0];
if (!content) {
return;
Expand Down
30 changes: 30 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12784,6 +12784,8 @@ __metadata:
npm-registry-fetch: "npm:^18.0.2"
onchange: "npm:^7.1.0"
oslllo-svg-fixer: "npm:^5.0.0"
postcss: "npm:^8.4.49"
postcss-selector-parser: "npm:^7.0.0"
registry-auth-token: "npm:^5.0.2"
semver: "npm:^7.6.3"
typescript: "npm:^5.6.3"
Expand Down Expand Up @@ -21091,6 +21093,16 @@ __metadata:
languageName: node
linkType: hard

"postcss-selector-parser@npm:^7.0.0":
version: 7.0.0
resolution: "postcss-selector-parser@npm:7.0.0"
dependencies:
cssesc: "npm:^3.0.0"
util-deprecate: "npm:^1.0.2"
checksum: 10c0/e96e096afcce70bf5c97789f5ea09d7415ae5eb701d82b05b5e8532885d31363b484fcb1ca9488c9a331f30508d9e5bb6c3109eb2eb5067ef3d3919f9928cd9d
languageName: node
linkType: hard

"postcss-svgo@npm:^5.1.0":
version: 5.1.0
resolution: "postcss-svgo@npm:5.1.0"
Expand Down Expand Up @@ -21142,6 +21154,17 @@ __metadata:
languageName: node
linkType: hard

"postcss@npm:^8.4.49":
version: 8.4.49
resolution: "postcss@npm:8.4.49"
dependencies:
nanoid: "npm:^3.3.7"
picocolors: "npm:^1.1.1"
source-map-js: "npm:^1.2.1"
checksum: 10c0/f1b3f17aaf36d136f59ec373459f18129908235e65dbdc3aee5eef8eba0756106f52de5ec4682e29a2eab53eb25170e7e871b3e4b52a8f1de3d344a514306be3
languageName: node
linkType: hard

"prelude-ls@npm:^1.2.1":
version: 1.2.1
resolution: "prelude-ls@npm:1.2.1"
Expand Down Expand Up @@ -23367,6 +23390,13 @@ __metadata:
languageName: node
linkType: hard

"source-map-js@npm:^1.2.1":
version: 1.2.1
resolution: "source-map-js@npm:1.2.1"
checksum: 10c0/7bda1fc4c197e3c6ff17de1b8b2c20e60af81b63a52cb32ec5a5d67a20a7d42651e2cb34ebe93833c5a2a084377e17455854fee3e21e7925c64a51b6a52b0faf
languageName: node
linkType: hard

"source-map-loader@npm:^3.0.0":
version: 3.0.2
resolution: "source-map-loader@npm:3.0.2"
Expand Down

0 comments on commit dca0985

Please sign in to comment.