Skip to content

Commit 5b804ad

Browse files
author
LB
authored
Merge pull request #879 from system-ui/prism-options
Add Prism preset to options
2 parents c973516 + cd2b429 commit 5b804ad

File tree

7 files changed

+60
-8
lines changed

7 files changed

+60
-8
lines changed

packages/gatsby-plugin-theme-ui/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ prevent a flash of unstyled colors when using color modes.
2020

2121
| Key | Default value | Description |
2222
| ------------------------ | ---------------- | -------------------------------------------------------------------------------- |
23+
| `prismPreset` | `null` | The name of the preset you'd like to use to style code blocks inside your markdown files. The available presets can be found in the [theme-ui docs](https://theme-ui.com/packages/prism/). You can also use a package string of your own choosing. |
2324
| `preset` | `null` | This can be a JSON theme object or a string package name. Make sure the package you're requiring is installed in your dependencies. |
2425

2526
> Note that this plugin assumes the theme object is exported as `default`.
@@ -34,8 +35,8 @@ module.exports = {
3435
plugins: [
3536
{ resolve: 'gatsby-plugin-theme-ui',
3637
options: {
38+
prismPreset: 'night-owl'
3739
preset: '@theme-ui/preset-funk'
38-
// or require('@theme-ui/preset-funk')
3940
}
4041
}],
4142
}

packages/gatsby-plugin-theme-ui/gatsby-node.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
exports.onPreInit = (__, options) => {
2-
let { preset } = options
1+
const prismPresetDictionary = require(`./utils/preset-dictionary`)
2+
3+
exports.onPreInit = ({ reporter }, options) => {
4+
let { preset, prismPreset } = options
35

46
if (typeof preset === 'string') {
57
try {
@@ -10,6 +12,18 @@ exports.onPreInit = (__, options) => {
1012
)
1113
}
1214
}
15+
16+
if (prismPreset in prismPresetDictionary) {
17+
prismPreset = prismPresetDictionary[prismPreset]
18+
}
19+
20+
if (prismPreset) {
21+
try {
22+
options.prismPreset = require(prismPreset)
23+
} catch {
24+
reporter.warn(`It appears the prism dependency is not installed.`)
25+
}
26+
}
1327
}
1428

1529
exports.createSchemaCustomization = ({ actions }) => {
@@ -18,15 +32,20 @@ exports.createSchemaCustomization = ({ actions }) => {
1832
createTypes(`
1933
type ThemeUiConfig implements Node {
2034
preset: JSON,
35+
prismPreset: JSON,
2136
}
2237
`)
2338
}
2439

25-
exports.sourceNodes = ({ actions, createContentDigest }, { preset = {} }) => {
40+
exports.sourceNodes = (
41+
{ actions, createContentDigest },
42+
{ preset = {}, prismPreset = {} }
43+
) => {
2644
const { createNode } = actions
2745

2846
const themeUiConfig = {
2947
preset,
48+
prismPreset,
3049
}
3150

3251
createNode({

packages/gatsby-plugin-theme-ui/src/hooks/configOptions.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const useThemeUiConfig = () => {
55
query {
66
themeUiConfig(id: { eq: "gatsby-plugin-theme-ui-config" }) {
77
preset
8+
prismPreset
89
}
910
}
1011
`)

packages/gatsby-plugin-theme-ui/src/provider.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@ import useThemeUiConfig from './hooks/configOptions'
66

77
const Root = ({ children }) => {
88
const themeUiConfig = useThemeUiConfig()
9-
const { preset } = themeUiConfig
9+
const { preset, prismPreset } = themeUiConfig
1010

1111
const theme = preset.default || preset
1212

13-
const fullTheme = merge(theme, localTheme)
13+
const themeWithPrism = merge(theme, {
14+
styles: {
15+
pre: prismPreset,
16+
},
17+
})
18+
19+
const fullTheme = merge(themeWithPrism, localTheme)
1420

1521
return (
1622
<ThemeProvider theme={fullTheme} components={components}>

packages/gatsby-plugin-theme-ui/test/__mocks__/gatsby.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module.exports = {
77
useStaticQuery: jest.fn(() => ({
88
themeUiConfig: {
99
preset: {},
10+
prismPreset: {},
1011
},
1112
})),
1213
}

packages/gatsby-plugin-theme-ui/test/provider.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ test('renders with theme context', () => {
2323
const root = render(wrapRootElement({ element: <Consumer /> }, {}))
2424
expect(context.theme).toEqual({
2525
colors: {},
26+
styles: {
27+
pre: {},
28+
},
2629
})
2730
})
2831

@@ -32,8 +35,8 @@ test.skip('renders with ColorMode component', () => {
3235
modes: {
3336
dark: {
3437
primary: 'magenta',
35-
}
36-
}
38+
},
39+
},
3740
}
3841
const root = renderer.create(
3942
wrapRootElement(
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module.exports = {
2+
dracula: '@theme-ui/prism/presets/dracula.json',
3+
'duotone-dark': '@theme-ui/prism/presets/duotone-dark.json',
4+
'duotone-light': '@theme-ui/prism/presets/duotone-light.json',
5+
github: '@theme-ui/prism/presets/github.json',
6+
'night-owl-light': '@theme-ui/prism/presets/night-owl-light.json',
7+
'night-owl': '@theme-ui/prism/presets/night-owl.json',
8+
'oceanic-next': '@theme-ui/prism/presets/oceanic-next.json',
9+
'prism-coy': '@theme-ui/prism/presets/prism-coy.json',
10+
'prism-dark': '@theme-ui/prism/presets/prism-dark.json',
11+
'prism-funky': '@theme-ui/prism/presets/prism-funky.json',
12+
'prism-okaidia': '@theme-ui/prism/presets/prism-okaidia.json',
13+
'prism-solarizedlight': '@theme-ui/prism/presets/prism-solarizedlight.json',
14+
'prism-tomorrow': '@theme-ui/prism/presets/prism-tomorrow.json',
15+
'prism-twilight': '@theme-ui/prism/presets/prism-twilight.json',
16+
prism: '@theme-ui/prism/presets/prism.json',
17+
'shades-of-purple': '@theme-ui/prism/presets/shades-of-purple.json',
18+
'theme-ui': '@theme-ui/prism/presets/theme-ui.json',
19+
ultramin: '@theme-ui/prism/presets/ultramin.json',
20+
'vs-dark': '@theme-ui/prism/presets/vs-dark.json',
21+
}

0 commit comments

Comments
 (0)