Skip to content

Commit

Permalink
Fix syntax coloring and light/dark theme switching
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasVanPhan committed Jun 13, 2022
1 parent 9e39cb2 commit 82857ca
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
1 change: 1 addition & 0 deletions gitlab-pages/website/core/CodeExamples.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import Highlight, { defaultProps } from "prism-react-renderer";
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
import Prism from './PrismLigoSyntaxes'
import useThemeContext from '@theme/hooks/useThemeContext';
import defaultTheme from 'prism-react-renderer/themes/palenight';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
Expand Down
17 changes: 17 additions & 0 deletions gitlab-pages/website/core/PrismLigoSyntaxes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
import useThemeContext from '@theme/hooks/useThemeContext';
import defaultTheme from 'prism-react-renderer/themes/palenight';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';

const {Prism} = require("prism-react-renderer");

export const GetTheme = () => {
const {
siteConfig: {
themeConfig: {prism = {}},
},
} = useDocusaurusContext();
const {isDarkTheme} = useThemeContext();
const lightModeTheme = prism.theme || defaultTheme;
const darkModeTheme = prism.darkTheme || lightModeTheme;

return isDarkTheme ? darkModeTheme : lightModeTheme;
}

// TODO : What's a better way to extend the Prism library with these syntaxes ?

Prism.languages = {
Expand Down
7 changes: 2 additions & 5 deletions gitlab-pages/website/src/pages/faq.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ import React from 'react';
import ReactMarkdown from 'react-markdown';
import Layout from '@theme/Layout';
import Highlight, { defaultProps } from "prism-react-renderer";
import defaultTheme from 'prism-react-renderer/themes/palenight';
import Prism from '../../core/PrismLigoSyntaxes'
import Prism, {GetTheme} from '../../core/PrismLigoSyntaxes'

// TODO : Add light / dark theme instead of default one
// TODO : Make Ligo syntaxes highlighting work in Markdown code block
// TODO : Add filtering-by-tag function

const QUESTIONS = [
Expand Down Expand Up @@ -87,7 +84,7 @@ const Question = ({question, answer, tags}) => (
const language = match[1]
const code = children[0]
return (
<Highlight {...defaultProps} code={code} language={language} style={defaultTheme}>
<Highlight {...defaultProps} code={code} language={language} theme={GetTheme()}>
{/* This function was taken directly from the prism-react-renderer Github page README */}
{({ className, style, tokens, getLineProps, getTokenProps }) => (
<pre className={className} style={style}>
Expand Down
1 change: 0 additions & 1 deletion gitlab-pages/website/src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React from 'react';
import Layout from '@theme/Layout';
import useBaseUrl from '@docusaurus/useBaseUrl';
import CodeExamples from '../../core/CodeExamples';
import Prism from '../../core/PrismLigoSyntaxes'

const FEATURES = [
{
Expand Down

0 comments on commit 82857ca

Please sign in to comment.