Skip to content

Commit a5bf1a8

Browse files
Laurie BarthLaurie Barth
authored andcommitted
refactor prism code
1 parent 58cece8 commit a5bf1a8

File tree

3 files changed

+54
-82
lines changed

3 files changed

+54
-82
lines changed
Lines changed: 43 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const prismPresetDictionary = {
2-
'dracula': '@theme-ui/prism/presets/dracula.json',
2+
dracula: '@theme-ui/prism/presets/dracula.json',
33
'duotone-dark': '@theme-ui/prism/presets/duotone-dark.json',
44
'duotone-light': '@theme-ui/prism/presets/duotone-light.json',
5-
'github': '@theme-ui/prism/presets/github.json',
5+
github: '@theme-ui/prism/presets/github.json',
66
'night-owl-light': '@theme-ui/prism/presets/night-owl-light.json',
77
'night-owl': '@theme-ui/prism/presets/night-owl.json',
88
'oceanic-next': '@theme-ui/prism/presets/oceanic-next.json',
@@ -12,30 +12,27 @@ const prismPresetDictionary = {
1212
'prism-solarizedlight': '@theme-ui/prism/presets/prism-solarizedlight.json',
1313
'prism-tomorrow': '@theme-ui/prism/presets/prism-tomorrow.json',
1414
'prism-twilight': '@theme-ui/prism/presets/prism-twilight.json',
15-
'prism': '@theme-ui/prism/presets/prism.json',
16-
'shades-or-purple': '@theme-ui/prism/presets/shades-or-purple.json',
15+
prism: '@theme-ui/prism/presets/prism.json',
16+
'shades-of-purple': '@theme-ui/prism/presets/shades-of-purple.json',
1717
'theme-ui': '@theme-ui/prism/presets/theme-ui.json',
18-
'ultramin': '@theme-ui/prism/presets/ultramin.json',
18+
ultramin: '@theme-ui/prism/presets/ultramin.json',
1919
'vs-dark': '@theme-ui/prism/presets/vs-dark.json',
2020
}
2121

2222
exports.onPreInit = (__, options) => {
23-
let {themeModulePath} = options
24-
if(themeModulePath) {
25-
options.themeModulePath = require(themeModulePath)
26-
}
27-
28-
if(prismPreset in prismPresetDictionary) {
29-
prismPreset = prismPresetDictionary[prismPreset]
30-
}
23+
if (prismPreset in prismPresetDictionary) {
24+
prismPreset = prismPresetDictionary[prismPreset]
25+
}
3126

32-
if(prismPreset) {
33-
try {
34-
options.prismPreset = require(prismPreset)
35-
} catch {
36-
reporter.error(`It appears the prism dependency is not installed. Try running \`${generateInstallInstructions()}\n\n${prismPreset}\``)
37-
}
27+
if (prismPreset) {
28+
try {
29+
options.prismPreset = require(prismPreset)
30+
} catch {
31+
reporter.error(
32+
`It appears the prism dependency is not installed. Try running \`${generateInstallInstructions()}\n\n${prismPreset}\``
33+
)
3834
}
35+
}
3936
}
4037

4138
function generateInstallInstructions() {
@@ -44,48 +41,41 @@ function generateInstallInstructions() {
4441
const packageMangerConfigKey = `cli.packageManager`
4542
const PACKAGE_MANGER = getConfigStore().get(packageMangerConfigKey) || `yarn`
4643

47-
const installKeyWord = PACKAGE_MANGER === `yarn` ? "add" : "install"
44+
const installKeyWord = PACKAGE_MANGER === `yarn` ? 'add' : 'install'
4845

4946
return `${PACKAGE_MANGER} ${installKeyWord}`
5047
}
5148

5249
exports.createSchemaCustomization = ({ actions }) => {
53-
const { createTypes } = actions
54-
55-
createTypes(`
50+
const { createTypes } = actions
51+
52+
createTypes(`
5653
type ThemeUiConfig implements Node {
57-
themeModule: JSON,
58-
themeModulePath: JSON,
59-
moduleExportName: String,
6054
prismPreset: JSON,
6155
}
6256
`)
57+
}
58+
59+
exports.sourceNodes = (
60+
{ actions, createContentDigest },
61+
{ prismPreset = {} }
62+
) => {
63+
const { createNode } = actions
64+
65+
const themeUiConfig = {
66+
prismPreset,
6367
}
64-
65-
exports.sourceNodes = (
66-
{ actions, createContentDigest },
67-
{ prismPreset, moduleExportName = 'default', themeModule, themeModulePath}
68-
) => {
69-
const { createNode } = actions
70-
71-
const themeUiConfig = {
72-
themeModule,
73-
themeModulePath,
74-
moduleExportName,
75-
prismPreset
76-
}
77-
78-
createNode({
79-
...themeUiConfig,
80-
id: `gatsby-plugin-theme-ui-config`,
81-
parent: null,
82-
children: [],
83-
internal: {
84-
type: `ThemeUiConfig`,
85-
contentDigest: createContentDigest(themeUiConfig),
86-
content: JSON.stringify(themeUiConfig),
87-
description: `Options for gatsby-plugin-theme-ui`,
88-
},
89-
})
90-
}
9168

69+
createNode({
70+
...themeUiConfig,
71+
id: `gatsby-plugin-theme-ui-config`,
72+
parent: null,
73+
children: [],
74+
internal: {
75+
type: `ThemeUiConfig`,
76+
contentDigest: createContentDigest(themeUiConfig),
77+
content: JSON.stringify(themeUiConfig),
78+
description: `Options for gatsby-plugin-theme-ui`,
79+
},
80+
})
81+
}
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import { useStaticQuery, graphql } from "gatsby"
1+
import { useStaticQuery, graphql } from 'gatsby'
22

33
const useThemeUiConfig = () => {
44
const data = useStaticQuery(graphql`
55
query {
66
themeUiConfig(id: { eq: "gatsby-plugin-theme-ui-config" }) {
7-
themeModule
8-
themeModulePath
9-
moduleExportName
107
prismPreset
118
}
129
}
@@ -15,4 +12,4 @@ const useThemeUiConfig = () => {
1512
return data.themeUiConfig
1613
}
1714

18-
export default useThemeUiConfig
15+
export default useThemeUiConfig

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

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,19 @@ import useThemeUiConfig from './hooks/configOptions'
66

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

11-
let themeWrapper
12-
if (themeModule) {
13-
themeWrapper = themeModule
14-
}
15-
16-
if (themeModulePath) {
17-
themeWrapper = themeModulePath
18-
}
19-
20-
if (themeWrapper && moduleExportName in themeWrapper) {
21-
themeWrapper = themeWrapper[moduleExportName]
22-
}
23-
24-
if (prismPreset) {
25-
themeWrapper = merge(themeWrapper, {
11+
const themeWithPrism = merge(
12+
{},
13+
{
2614
styles: {
27-
pre: {
28-
...prismPreset,
29-
},
15+
pre: prismPreset,
3016
},
31-
})
32-
}
17+
}
18+
)
19+
20+
themeWrapper = merge(themeWithPrism, theme)
3321

34-
themeWrapper = merge(themeWrapper, {
35-
...theme,
36-
})
3722
return (
3823
<ThemeProvider theme={themeWrapper} components={components}>
3924
{children}

0 commit comments

Comments
 (0)