Skip to content

Commit

Permalink
Provide complete CSS by leveraging fixture classes (#39)
Browse files Browse the repository at this point in the history
Co-authored-by: hyrious <[email protected]>
  • Loading branch information
earlAchromatic and hyrious authored Nov 15, 2024
1 parent 6b67a6e commit 73bb29f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
7 changes: 7 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const cli = meow(
--only-style Only output the styles, forces --preserve-variables on
--only-variables Only output the variables for the specified themes
--root-selector Specify the root selector when outputting styles, default '.markdown-body'
--no-use-fixture Exclude generated classes that come from GitHub Markdown API rendered fixture.md
Examples
$ github-markdown-css --list
Expand Down Expand Up @@ -74,6 +75,10 @@ const cli = meow(
rootSelector: {
type: 'string',
},
useFixture: {
type: 'boolean',
default: true,
},
},
},
);
Expand All @@ -85,6 +90,7 @@ const {
onlyStyle,
onlyVariables,
rootSelector,
useFixture,
} = cli.flags;

let {light, dark} = cli.flags;
Expand Down Expand Up @@ -140,5 +146,6 @@ console.log(
onlyStyles: onlyStyle,
onlyVariables,
rootSelector,
useFixture,
}),
);
28 changes: 20 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import postcss from 'postcss';
import {cachedFetch, reverseUnique, unique, zip} from './utilities.js';
import {cachedFetch, getUniqueClasses, renderMarkdown, reverseUnique, unique, zip} from './utilities.js';
import {ALLOW_CLASS, ALLOW_TAGS, manuallyAddedStyle} from './constants.js';

function extractStyles(rules, cssText) {
function extractStyles(rules, cssText, {extraAllowableClasses = []} = {}) {
const allowableClassList = new Set([...ALLOW_CLASS, ...extraAllowableClasses]);

function select(selector) {
if (selector.startsWith('.markdown-body')) {
return true;
Expand All @@ -23,7 +25,7 @@ function extractStyles(rules, cssText) {
}

const klass = selector.match(/\.[-\w]+/);
if (klass && !ALLOW_CLASS.has(klass[0])) {
if (klass && !allowableClassList.has(klass[0])) {
return false;
}

Expand All @@ -32,7 +34,7 @@ function extractStyles(rules, cssText) {

const klass = selector.match(/^\.[-\w]+/);
if (klass) {
return ALLOW_CLASS.has(klass[0]);
return allowableClassList.has(klass[0]);
}

return false;
Expand Down Expand Up @@ -79,7 +81,7 @@ function extractStyles(rules, cssText) {
return;
}

if (rule.some(decl => decl.value.includes('prettylights'))) {
if (rule.some(node => node.type === 'decl' && node.value.includes('prettylights'))) {
if (!rule.selector.includes('.QueryBuilder')) {
rules.push(rule);
}
Expand Down Expand Up @@ -135,6 +137,10 @@ function classifyRules(rules) {
return undefined;
}

function isEmpty(rule) {
return !rule.first;
}

function mergeRules(rules) {
const result = [];
const selectorIndexMap = {};
Expand Down Expand Up @@ -163,7 +169,7 @@ function classifyRules(rules) {
});
}

return result;
return result.filter(rule => !isEmpty(rule));
}

const result = {rules: [], light: [], dark: []};
Expand Down Expand Up @@ -233,6 +239,7 @@ export default async function getCSS({
onlyVariables = false,
onlyStyles = false,
rootSelector = '.markdown-body',
useFixture = true,
} = {}) {
if (onlyVariables && onlyStyles) {
// Would result in an empty output
Expand All @@ -250,7 +257,12 @@ export default async function getCSS({
const body = await cachedFetch('https://github.com');
// Get a list of all css links on the page
const links = unique(body.match(/(?<=href=").+?\.css/g));
const contents = await Promise.all(links.map(url => cachedFetch(url)));
const renderMarkdownPromise = useFixture ? renderMarkdown() : Promise.resolve();
const [fixtureHtml, ...contents] = await Promise.all([
renderMarkdownPromise,
...links.map(url => cachedFetch(url)),
]);
const fixtureClasses = getUniqueClasses(fixtureHtml);

let rules = [];
const colors = [];
Expand All @@ -277,7 +289,7 @@ export default async function getCSS({
extractVariables(colors, 'shared', cssText);
}

extractStyles(rules, cssText);
extractStyles(rules, cssText, {extraAllowableClasses: fixtureClasses});
}
}

Expand Down
12 changes: 11 additions & 1 deletion utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ export async function renderMarkdown() {

const response = await fetch('https://api.github.com/markdown', {
method: 'POST',
body: JSON.stringify({text}),
body: JSON.stringify({
text,
mode: 'gfm',
context: 'sindresorhus/generate-github-markdown-css',
}),
headers: {
Accept: 'application/vnd.github.v3+json',
'User-Agent': 'Node.js',
Expand All @@ -91,3 +95,9 @@ export async function renderMarkdown() {

throw new Error(`Failed to render markdown: ${body}`);
}

export function getUniqueClasses(html = '') {
const classNames = [...html.matchAll(/class\s*=\s*["']([^"']+)["']/g)]
.flatMap(match => match[1].split(/\s+/).map(c => `.${c}`));
return new Set(classNames);
}

0 comments on commit 73bb29f

Please sign in to comment.