Search code examples
prismgatsbytheme-ui

Theme-ui/prism won't work for gatsbyjs markdown files


I'm using @theme-ui/prism to format code block text in markdown files. The markdown plugin im using is gatsby-plugin-mdx. It doesn't seem to be working because I don't get the dark background in the code block.

Here's what I did:

yarn add @theme-ui/prism

Create src/gatsby-plugin-theme-ui/components.ts:

import Prism from '@theme-ui/prism'
const components = {
  pre: props => props.children,
  code: Prism,
}
export default components

In src/gatsby-plugin-theme-ui/index.ts I have defined a style for pre:

import nightOwl from '@theme-ui/prism/presets/night-owl.json';
import colors from "./colors";
import headings from "./headings";

const systemFonts =
  '-apple-system, BlinkMacSystemFont, San Francisco, Helvetica Neue, Helvetica, Ubuntu, Roboto, Noto, Segoe UI, Arial, sans-serif';
const transition = '0.2s ease-out';

export default {
  useColorSchemeMediaQuery: true,
  colors,
  initialColorMode: `dark`,
  fonts: {
    body: systemFonts,
    heading: "Avenir Next",
    monospace: 'Menlo, monospace'
  },
  fontSizes: [12, 14, 16, 24, 28, 36, 48, 64],
  fontWeights: {
    body: 400,
    heading: 500,
    bold: 600,
  },
  lineHeights: {
    body: 1.7,
    heading: 1.1275,
  },
  letterSpacings: {
    body: 'normal',
    caps: '0.2em'
  },
  breakpoints: [
    '320px', '376px', '540px', '735px', '1070px', '1280px', '1640px', '1880px'
  ],
  transition,
  styles: {
    root: {
      fontFamily: 'body',
      lineHeight: 'body',
      fontWeight: 'body',
      ...headings
    },
    ...headings,
    p: {
      my: 4,
    },
    a: {
      color: 'secondary',
      transition: `color ${transition}`,
      ':hover,:focus': {
        color: 'text'
      }
    },
    pre: {
      ...nightOwl,
      fontFamily: `"Operator Mono", monospace`,
      fontSize: '0.9rem',
      tabSize: 4,
      hyphens: `none`,
      overflow: `auto`,
      borderRadius: 6,
      p: 3,
      my: 4
    },
    inlineCode: {
      color: `primary`,
      background: `rgba(233, 218, 172, 0.15)`,
      borderRadius: 3,
      px: `0.4rem`,
      py: `0.2rem`
    },
    table: {
      width: '100%',
      borderCollapse: 'separate',
      borderSpacing: 0
    },
    th: {
      textAlign: 'left',
      borderBottomStyle: 'solid'
    },
    td: {
      textAlign: 'left',
      borderBottomStyle: 'solid'
    }
  }
}

Solution

  • The prism feature seems to actually be targeted for a future version of theme-ui, so the default (v0.3.x at the time of writing) doesn't support it.

    To solve this you can change your theme-ui* and gatsby-plugin-theme-ui package versions to ^0.4.0-rc.1 (rc.3 is actually out but it's broken atm so don't use it).

    // package.json
    {
      ...
      "dependencies": {
        "gatsby-plugin-theme-ui": "^0.4.0-rc.1",
        "@theme-ui/prism": "^0.4.0-rc.1",
        "theme-ui": "^0.4.0-rc.1",
      }
    }