From 0d89303bc2431ed6f385d4680b179d6babecb7c4 Mon Sep 17 00:00:00 2001 From: Christoph Werner Date: Thu, 15 Jul 2021 19:37:56 +0200 Subject: [PATCH 1/2] fix: dont inject styles without code --- src/remarkPlugin.js | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/remarkPlugin.js b/src/remarkPlugin.js index 84dac6d..d05fe17 100644 --- a/src/remarkPlugin.js +++ b/src/remarkPlugin.js @@ -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; + codeNodeRegistry.forEachCodeBlock((codeBlock, node) => { + numCodeOccurrences++; + const graphQLNode = getCodeBlockGraphQLDataFromRegistry( codeNodeRegistry, node, @@ -182,6 +186,8 @@ function remarkPlugin(options = {}) { }); codeNodeRegistry.forEachCodeSpan((codeSpan, node) => { + numCodeOccurrences++; + const graphQLNode = getCodeSpanGraphQLDataFromRegistry(codeNodeRegistry, node, codeSpan, getClassName); // Update Markdown node @@ -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) }); + } } }; } From ee6851a6b682d4c6c83c0d9705fee457e62b4339 Mon Sep 17 00:00:00 2001 From: Christoph Werner Date: Fri, 16 Jul 2021 07:54:24 +0200 Subject: [PATCH 2/2] fix: formatting --- src/remarkPlugin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/remarkPlugin.js b/src/remarkPlugin.js index d05fe17..b5ec7f9 100644 --- a/src/remarkPlugin.js +++ b/src/remarkPlugin.js @@ -156,7 +156,7 @@ function remarkPlugin(options = {}) { // its value to the HTML rendering contained in the GraphQL node. let numCodeOccurrences = 0; - + codeNodeRegistry.forEachCodeBlock((codeBlock, node) => { numCodeOccurrences++;