Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dont inject styles without code #162

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions src/remarkPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,11 @@ function remarkPlugin(options = {}) {
// time, change the original code fence Markdown node to an HTML node and set
// its value to the HTML rendering contained in the GraphQL node.

let numCodeOccurrences = 0;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nicer to just be able to ask codeNodeRegistry.isEmpty() or something


codeNodeRegistry.forEachCodeBlock((codeBlock, node) => {
numCodeOccurrences++;

const graphQLNode = getCodeBlockGraphQLDataFromRegistry(
codeNodeRegistry,
node,
Expand All @@ -182,6 +186,8 @@ function remarkPlugin(options = {}) {
});

codeNodeRegistry.forEachCodeSpan((codeSpan, node) => {
numCodeOccurrences++;

const graphQLNode = getCodeSpanGraphQLDataFromRegistry(codeNodeRegistry, node, codeSpan, getClassName);

// Update Markdown node
Expand All @@ -202,15 +208,17 @@ function remarkPlugin(options = {}) {
// 4. Generate CSS rules for each theme used by one or more code blocks in the registry,
// then append that CSS to the Markdown AST in an HTML node.

const styleElement = createStyleElement(
codeNodeRegistry.getAllPossibleThemes(),
codeNodeRegistry.getTokenStylesForTheme,
replaceColor,
injectStyles ? styles : undefined
);
if (numCodeOccurrences > 0) {
const styleElement = createStyleElement(
codeNodeRegistry.getAllPossibleThemes(),
codeNodeRegistry.getTokenStylesForTheme,
replaceColor,
injectStyles ? styles : undefined
);

if (styleElement) {
tree.children.unshift({ type: 'html', value: renderHTML(styleElement) });
if (styleElement) {
tree.children.unshift({ type: 'html', value: renderHTML(styleElement) });
}
}
};
}
Expand Down