Skip to content

Commit a85b41c

Browse files
authored
Merge pull request #58 from mdluo/master
Change `wrapperClassName` to accept both string and function
2 parents f7b5ede + ba0fdbe commit a85b41c

File tree

6 files changed

+31
-6
lines changed

6 files changed

+31
-6
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Add to your `gatsby-config.js` (all options are optional; defaults shown here):
5757
// All options are optional. Defaults shown here.
5858
options: {
5959
colorTheme: 'Dark+ (default dark)', // Read on for list of included themes. Also accepts object and function forms.
60-
wrapperClassName: '', // Additional class put on 'pre' tag
60+
wrapperClassName: '', // Additional class put on 'pre' tag. Also accepts function to set the class dynamically.
6161
injectStyles: true, // Injects (minimal) additional CSS for layout and scrolling
6262
extensions: [], // Extensions to download from the marketplace to provide more languages and themes
6363
extensionDataDirectory: // Absolute path to the directory where extensions will be downloaded. Defaults to inside node_modules.
@@ -335,7 +335,7 @@ Line numbers and ranges aren’t the only things you can pass as options on your
335335
<Amazing><Stuff /></Amazing>
336336
```
337337
338-
`gatsby-remark-vscode` doesn’t inherently understand these things, but it parses the input and allows you to access it in the `colorTheme` and `getLineClassName` functions:
338+
`gatsby-remark-vscode` doesn’t inherently understand these things, but it parses the input and allows you to access it in the `colorTheme`, `wrapperClassName` and `getLineClassName` functions:
339339
340340
```js
341341
{
@@ -350,7 +350,8 @@ Line numbers and ranges aren’t the only things you can pass as options on your
350350
// nested: { objects: 'yep' }
351351
// }
352352
return parsedOptions.theme || 'Dark+ (default dark)';
353-
}
353+
},
354+
wrapperClassName: ({ parsedOptions, language, markdownNode, codeFenceNode }) => '';
354355
}
355356
```
356357

src/index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,17 @@ function createPlugin() {
177177
);
178178
}
179179

180-
const className = joinClassNames(wrapperClassName, themeClassNames, 'vscode-highlight');
180+
const wrapperClassNameValue =
181+
typeof wrapperClassName === 'function'
182+
? wrapperClassName({
183+
language: languageName,
184+
markdownNode,
185+
codeFenceNode: node,
186+
parsedOptions: options
187+
})
188+
: wrapperClassName;
189+
190+
const className = joinClassNames(wrapperClassNameValue, themeClassNames, 'vscode-highlight');
181191
node.type = 'html';
182192
node.value = renderHTML(
183193
pre(

src/types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type ColorThemeOption = string | ColorThemeSettings | ((data: CodeFenceData) =>
4141

4242
interface PluginOptions {
4343
colorTheme?: ColorThemeOption;
44-
wrapperClassName?: string;
44+
wrapperClassName?: string | ((data: CodeFenceData) => string);
4545
languageAliases?: Record<string, string>;
4646
extensions?: ExtensionDemand[];
4747
getLineClassName?: (line: LineData) => string;

test/integration/cases/code-fence-meta.expected.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<pre class="default-dark vscode-highlight" data-language="js"><code class="vscode-highlight-code"><span class="vscode-highlight-line vscode-highlight-line-highlighted"><span class="mtk3">// should be highlighted</span></span></code></pre>
1+
<pre class="test-wrapper default-dark vscode-highlight" data-language="js"><code class="vscode-highlight-code"><span class="vscode-highlight-line vscode-highlight-line-highlighted"><span class="mtk3">// should be highlighted</span></span></code></pre>
2+
<pre class="test-wrapper js-test default-dark vscode-highlight" data-language="js"><code class="vscode-highlight-code"><span class="vscode-highlight-line"><span class="mtk3">// should be highlighted</span></span></code></pre>
23
<style class="vscode-highlight-styles">
34
:root {
45
--vscode-highlight-padding-v: 1rem;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
wrapperClassName: ({ language, parsedOptions }) => {
3+
const { wrapperClass } = parsedOptions;
4+
if (wrapperClass) {
5+
return `test-wrapper ${language}-${wrapperClass}`;
6+
}
7+
return 'test-wrapper';
8+
},
9+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
```js {1}
22
// should be highlighted
3+
```
4+
5+
```js {wrapperClass: 'test'}
6+
// should be highlighted
37
```

0 commit comments

Comments
 (0)