diff --git a/src/components/Highlight.js b/src/components/Highlight.js index 26bcd0e..788c38b 100755 --- a/src/components/Highlight.js +++ b/src/components/Highlight.js @@ -1,144 +1,144 @@ -// @flow - -import React, { Component, type Node } from "react"; -import normalizeTokens from "../utils/normalizeTokens"; -import themeToDict, { type ThemeDict } from "../utils/themeToDict"; - -import type { - Language, - Token, - LineInputProps, - LineOutputProps, - TokenInputProps, - TokenOutputProps, - RenderProps, - PrismLib, - PrismTheme -} from "../types"; - -type Props = { - Prism: PrismLib, - theme?: PrismTheme, - language: Language, - code: string, - children: (props: RenderProps) => Node -}; - -class Highlight extends Component { - prevTheme: PrismTheme | void; - prevLanguage: Language | void; - themeDict: ThemeDict | void; - - getThemeDict = (props: Props) => { - if ( - this.themeDict !== undefined && - props.theme === this.prevTheme && - props.language === this.prevLanguage - ) { - return this.themeDict; - } - - this.prevTheme = props.theme; - this.prevLanguage = props.language; - - const themeDict = props.theme - ? themeToDict(props.theme, props.language) - : undefined; - return (this.themeDict = themeDict); - }; - - getLineProps = ({ - key, - className, - style, - line, - ...rest - }: LineInputProps): LineOutputProps => { - const output: LineOutputProps = { - ...rest, - className: "token-line", - style: undefined, - key: undefined - }; - - const themeDict = this.getThemeDict(this.props); - if (themeDict !== undefined) { - output.style = themeDict.plain; - } - - if (style !== undefined) { - output.style = - output.style !== undefined ? { ...output.style, ...style } : style; - } - - if (key !== undefined) output.key = key; - if (className) output.className += ` ${className}`; - - return output; - }; - - getStyleForToken = ({ types, empty }: Token) => { - const typesSize = types.length; - const themeDict = this.getThemeDict(this.props); - - if (themeDict === undefined) { - return undefined; - } else if (typesSize === 1 && types[0] === "plain") { - return empty ? { display: "inline-block" } : undefined; - } else if (typesSize === 1 && !empty) { - return themeDict[types[0]]; - } - - const baseStyle = empty ? { display: "inline-block" } : {}; - // $FlowFixMe - const typeStyles = types.map(type => themeDict[type]); - return Object.assign(baseStyle, ...typeStyles); - }; - - getTokenProps = ({ - key, - className, - style, - token, - ...rest - }: TokenInputProps): TokenOutputProps => { - const output: TokenOutputProps = { - ...rest, - className: `token ${token.types.join(" ")}`, - children: token.content, - style: this.getStyleForToken(token), - key: undefined - }; - - if (style !== undefined) { - output.style = - output.style !== undefined ? { ...output.style, ...style } : style; - } - - if (key !== undefined) output.key = key; - if (className) output.className += ` ${className}`; - - return output; - }; - - render() { - const { Prism, language, code, children } = this.props; - - const themeDict = this.getThemeDict(this.props); - - const grammar = Prism.languages[language]; - const mixedTokens = - grammar !== undefined ? Prism.tokenize(code, grammar, language) : [code]; - const tokens = normalizeTokens(mixedTokens); - - return children({ - tokens, - className: `prism-code language-${language}`, - style: themeDict !== undefined ? themeDict.root : {}, - getLineProps: this.getLineProps, - getTokenProps: this.getTokenProps - }); - } -} - -export default Highlight; +// @flow + +import React, { Component, type Node } from "react"; +import normalizeTokens from "../utils/normalizeTokens"; +import themeToDict, { type ThemeDict } from "../utils/themeToDict"; + +import type { + Language, + Token, + LineInputProps, + LineOutputProps, + TokenInputProps, + TokenOutputProps, + RenderProps, + PrismLib, + PrismTheme, +} from "../types"; + +type Props = { + Prism: PrismLib, + theme?: PrismTheme, + language: Language, + code: string, + children: (props: RenderProps) => Node, +}; + +class Highlight extends Component { + prevTheme: PrismTheme | void; + prevLanguage: Language | void; + themeDict: ThemeDict | void; + + getThemeDict = (props: Props) => { + if ( + this.themeDict !== undefined && + props.theme === this.prevTheme && + props.language === this.prevLanguage + ) { + return this.themeDict; + } + + this.prevTheme = props.theme; + this.prevLanguage = props.language; + + const themeDict = props.theme + ? themeToDict(props.theme, props.language) + : undefined; + return (this.themeDict = themeDict); + }; + + getLineProps = ({ + key, + className, + style, + line, + ...rest + }: LineInputProps): LineOutputProps => { + const output: LineOutputProps = { + ...rest, + className: "token-line", + style: undefined, + key: undefined, + }; + + const themeDict = this.getThemeDict(this.props); + if (themeDict !== undefined) { + output.style = themeDict.plain; + } + + if (style !== undefined) { + output.style = + output.style !== undefined ? { ...output.style, ...style } : style; + } + + if (key !== undefined) output.key = key; + if (className) output.className += ` ${className}`; + + return output; + }; + + getStyleForToken = ({ types, empty }: Token) => { + const typesSize = types.length; + const themeDict = this.getThemeDict(this.props); + + if (themeDict === undefined) { + return undefined; + } else if (typesSize === 1 && types[0] === "plain") { + return empty ? { display: "inline-block" } : undefined; + } else if (typesSize === 1 && !empty) { + return themeDict[types[0]]; + } + + const baseStyle = empty ? { display: "inline-block" } : {}; + // $FlowFixMe + const typeStyles = types.map((type) => themeDict[type]); + return Object.assign(baseStyle, ...typeStyles); + }; + + getTokenProps = ({ + key, + className, + style, + token, + ...rest + }: TokenInputProps): TokenOutputProps => { + const output: TokenOutputProps = { + ...rest, + className: `token ${token.types.join(" ")}`, + children: token.content, + style: this.getStyleForToken(token), + key: undefined, + }; + + if (style !== undefined) { + output.style = + output.style !== undefined ? { ...output.style, ...style } : style; + } + + if (key !== undefined) output.key = key; + if (className) output.className += ` ${className}`; + + return output; + }; + + render() { + const { Prism, language, code, children } = this.props; + + const themeDict = this.getThemeDict(this.props); + + const grammar = Prism.languages[language]; + const mixedTokens = + grammar !== undefined ? Prism.tokenize(code, grammar, language) : [code]; + const tokens = normalizeTokens(mixedTokens); + + return children({ + tokens, + className: `prism-code language-${language}`, + style: themeDict !== undefined ? themeDict.root : {}, + getLineProps: this.getLineProps, + getTokenProps: this.getTokenProps, + }); + } +} + +export default Highlight; diff --git a/src/themes/dracula.js b/src/themes/dracula.js index 5be63fc..0e75bab 100755 --- a/src/themes/dracula.js +++ b/src/themes/dracula.js @@ -7,65 +7,65 @@ import type { PrismTheme } from "../types"; var theme: PrismTheme = { plain: { color: "#F8F8F2", - backgroundColor: "#282A36" + backgroundColor: "#282A36", }, styles: [ { types: ["prolog", "constant", "builtin"], style: { - color: "rgb(189, 147, 249)" - } + color: "rgb(189, 147, 249)", + }, }, { types: ["inserted", "function"], style: { - color: "rgb(80, 250, 123)" - } + color: "rgb(80, 250, 123)", + }, }, { types: ["deleted"], style: { - color: "rgb(255, 85, 85)" - } + color: "rgb(255, 85, 85)", + }, }, { types: ["changed"], style: { - color: "rgb(255, 184, 108)" - } + color: "rgb(255, 184, 108)", + }, }, { types: ["punctuation", "symbol"], style: { - color: "rgb(248, 248, 242)" - } + color: "rgb(248, 248, 242)", + }, }, { types: ["string", "char", "tag", "selector"], style: { - color: "rgb(255, 121, 198)" - } + color: "rgb(255, 121, 198)", + }, }, { types: ["keyword", "variable"], style: { color: "rgb(189, 147, 249)", - fontStyle: "italic" - } + fontStyle: "italic", + }, }, { types: ["comment"], style: { - color: "rgb(98, 114, 164)" - } + color: "rgb(98, 114, 164)", + }, }, { types: ["attr-name"], style: { - color: "rgb(241, 250, 140)" - } - } - ] + color: "rgb(241, 250, 140)", + }, + }, + ], }; export default theme; diff --git a/src/themes/duotoneDark.js b/src/themes/duotoneDark.js index 76e0e5f..54441e1 100755 --- a/src/themes/duotoneDark.js +++ b/src/themes/duotoneDark.js @@ -1,105 +1,105 @@ -// @flow -// Duotone Dark -// Author: Simurai, adapted from DuoTone themes for Atom (http://simurai.com/projects/2016/01/01/duotone-themes) -// Conversion: Bram de Haan (http://atelierbram.github.io/Base2Tone-prism/output/prism/prism-base2tone-evening-dark.css) -// Generated with Base16 Builder (https://github.com/base16-builder/base16-builder) - -import type { PrismTheme } from "../types"; - -var theme: PrismTheme = { - plain: { - backgroundColor: "#2a2734", - color: "#9a86fd" - }, - styles: [ - { - types: ["comment", "prolog", "doctype", "cdata", "punctuation"], - style: { - color: "#6c6783" - } - }, - { - types: ["namespace"], - style: { - opacity: 0.7 - } - }, - { - types: ["tag", "operator", "number"], - style: { - color: "#e09142" - } - }, - { - types: ["property", "function"], - style: { - color: "#9a86fd" - } - }, - { - types: ["tag-id", "selector", "atrule-id"], - style: { - color: "#eeebff" - } - }, - { - types: ["attr-name"], - style: { - color: "#c4b9fe" - } - }, - { - types: [ - "boolean", - "string", - "entity", - "url", - "attr-value", - "keyword", - "control", - "directive", - "unit", - "statement", - "regex", - "at-rule", - "placeholder", - "variable" - ], - style: { - color: "#ffcc99" - } - }, - { - types: ["deleted"], - style: { - textDecorationLine: "line-through" - } - }, - { - types: ["inserted"], - style: { - textDecorationLine: "underline" - } - }, - { - types: ["italic"], - style: { - fontStyle: "italic" - } - }, - { - types: ["important", "bold"], - style: { - fontWeight: "bold" - } - }, - { - types: ["important"], - style: { - color: "#c4b9fe" - } - } - ] -}; - -export default theme; +// @flow +// Duotone Dark +// Author: Simurai, adapted from DuoTone themes for Atom (http://simurai.com/projects/2016/01/01/duotone-themes) +// Conversion: Bram de Haan (http://atelierbram.github.io/Base2Tone-prism/output/prism/prism-base2tone-evening-dark.css) +// Generated with Base16 Builder (https://github.com/base16-builder/base16-builder) + +import type { PrismTheme } from "../types"; + +var theme: PrismTheme = { + plain: { + backgroundColor: "#2a2734", + color: "#9a86fd", + }, + styles: [ + { + types: ["comment", "prolog", "doctype", "cdata", "punctuation"], + style: { + color: "#6c6783", + }, + }, + { + types: ["namespace"], + style: { + opacity: 0.7, + }, + }, + { + types: ["tag", "operator", "number"], + style: { + color: "#e09142", + }, + }, + { + types: ["property", "function"], + style: { + color: "#9a86fd", + }, + }, + { + types: ["tag-id", "selector", "atrule-id"], + style: { + color: "#eeebff", + }, + }, + { + types: ["attr-name"], + style: { + color: "#c4b9fe", + }, + }, + { + types: [ + "boolean", + "string", + "entity", + "url", + "attr-value", + "keyword", + "control", + "directive", + "unit", + "statement", + "regex", + "at-rule", + "placeholder", + "variable", + ], + style: { + color: "#ffcc99", + }, + }, + { + types: ["deleted"], + style: { + textDecorationLine: "line-through", + }, + }, + { + types: ["inserted"], + style: { + textDecorationLine: "underline", + }, + }, + { + types: ["italic"], + style: { + fontStyle: "italic", + }, + }, + { + types: ["important", "bold"], + style: { + fontWeight: "bold", + }, + }, + { + types: ["important"], + style: { + color: "#c4b9fe", + }, + }, + ], +}; + +export default theme; diff --git a/src/themes/duotoneLight.js b/src/themes/duotoneLight.js index 974d8a2..8dcbdd2 100755 --- a/src/themes/duotoneLight.js +++ b/src/themes/duotoneLight.js @@ -1,109 +1,109 @@ -// @flow -// Duotone Light -// Author: Simurai, adapted from DuoTone themes for Atom (http://simurai.com/projects/2016/01/01/duotone-themes) -// Conversion: Bram de Haan (http://atelierbram.github.io/Base2Tone-prism/output/prism/prism-base2tone-evening-dark.css) -// Generated with Base16 Builder (https://github.com/base16-builder/base16-builder) - -import type { PrismTheme } from "../types"; - -var theme: PrismTheme = { - plain: { - backgroundColor: "#faf8f5", - color: "#728fcb" - }, - styles: [ - { - types: ["comment", "prolog", "doctype", "cdata", "punctuation"], - style: { - color: "#b6ad9a" - } - }, - { - types: ["namespace"], - style: { - opacity: 0.7 - } - }, - { - types: ["tag", "operator", "number"], - style: { - color: "#063289" - } - }, - { - types: ["property", "function"], - style: { - color: "#b29762" - } - }, - { - types: ["tag-id", "selector", "atrule-id"], - style: { - color: "#2d2006" - } - }, - { - types: ["attr-name"], - style: { - color: "#896724" - } - }, - { - types: [ - "boolean", - "string", - "entity", - "url", - "attr-value", - "keyword", - "control", - "directive", - "unit", - "statement", - "regex", - "at-rule" - ], - style: { - color: "#728fcb" - } - }, - { - types: ["placeholder", "variable"], - style: { - color: "#93abdc" - } - }, - { - types: ["deleted"], - style: { - textDecorationLine: "line-through" - } - }, - { - types: ["inserted"], - style: { - textDecorationLine: "underline" - } - }, - { - types: ["italic"], - style: { - fontStyle: "italic" - } - }, - { - types: ["important", "bold"], - style: { - fontWeight: "bold" - } - }, - { - types: ["important"], - style: { - color: "#896724" - } - } - ] -}; - -export default theme; +// @flow +// Duotone Light +// Author: Simurai, adapted from DuoTone themes for Atom (http://simurai.com/projects/2016/01/01/duotone-themes) +// Conversion: Bram de Haan (http://atelierbram.github.io/Base2Tone-prism/output/prism/prism-base2tone-evening-dark.css) +// Generated with Base16 Builder (https://github.com/base16-builder/base16-builder) + +import type { PrismTheme } from "../types"; + +var theme: PrismTheme = { + plain: { + backgroundColor: "#faf8f5", + color: "#728fcb", + }, + styles: [ + { + types: ["comment", "prolog", "doctype", "cdata", "punctuation"], + style: { + color: "#b6ad9a", + }, + }, + { + types: ["namespace"], + style: { + opacity: 0.7, + }, + }, + { + types: ["tag", "operator", "number"], + style: { + color: "#063289", + }, + }, + { + types: ["property", "function"], + style: { + color: "#b29762", + }, + }, + { + types: ["tag-id", "selector", "atrule-id"], + style: { + color: "#2d2006", + }, + }, + { + types: ["attr-name"], + style: { + color: "#896724", + }, + }, + { + types: [ + "boolean", + "string", + "entity", + "url", + "attr-value", + "keyword", + "control", + "directive", + "unit", + "statement", + "regex", + "at-rule", + ], + style: { + color: "#728fcb", + }, + }, + { + types: ["placeholder", "variable"], + style: { + color: "#93abdc", + }, + }, + { + types: ["deleted"], + style: { + textDecorationLine: "line-through", + }, + }, + { + types: ["inserted"], + style: { + textDecorationLine: "underline", + }, + }, + { + types: ["italic"], + style: { + fontStyle: "italic", + }, + }, + { + types: ["important", "bold"], + style: { + fontWeight: "bold", + }, + }, + { + types: ["important"], + style: { + color: "#896724", + }, + }, + ], +}; + +export default theme; diff --git a/src/themes/github.js b/src/themes/github.js index 5b455d2..fff27f2 100644 --- a/src/themes/github.js +++ b/src/themes/github.js @@ -6,33 +6,33 @@ import type { PrismTheme } from "../types"; var theme: PrismTheme = { plain: { color: "#393A34", - backgroundColor: "#f6f8fa" + backgroundColor: "#f6f8fa", }, styles: [ { types: ["comment", "prolog", "doctype", "cdata"], style: { color: "#999988", - fontStyle: "italic" - } + fontStyle: "italic", + }, }, { types: ["namespace"], style: { - opacity: 0.7 - } + opacity: 0.7, + }, }, { types: ["string", "attr-value"], style: { - color: "#e3116c" - } + color: "#e3116c", + }, }, { types: ["punctuation", "operator"], style: { - color: "#393A34" - } + color: "#393A34", + }, }, { types: [ @@ -45,37 +45,37 @@ var theme: PrismTheme = { "constant", "property", "regex", - "inserted" + "inserted", ], style: { - color: "#36acaa" - } + color: "#36acaa", + }, }, { types: ["atrule", "keyword", "attr-name", "selector"], style: { - color: "#00a4db" - } + color: "#00a4db", + }, }, { types: ["function", "deleted", "tag"], style: { - color: "#d73a49" - } + color: "#d73a49", + }, }, { types: ["function-variable"], style: { - color: "#6f42c1" - } + color: "#6f42c1", + }, }, { types: ["tag", "selector", "keyword"], style: { - color: "#00009f" - } - } - ] + color: "#00009f", + }, + }, + ], }; export default theme; diff --git a/src/themes/nightOwl.js b/src/themes/nightOwl.js index 6d9eb75..2aa84e1 100755 --- a/src/themes/nightOwl.js +++ b/src/themes/nightOwl.js @@ -7,107 +7,107 @@ import type { PrismTheme } from "../types"; var theme: PrismTheme = { plain: { color: "#d6deeb", - backgroundColor: "#011627" + backgroundColor: "#011627", }, styles: [ { types: ["changed"], style: { color: "rgb(162, 191, 252)", - fontStyle: "italic" - } + fontStyle: "italic", + }, }, { types: ["deleted"], style: { color: "rgba(239, 83, 80, 0.56)", - fontStyle: "italic" - } + fontStyle: "italic", + }, }, { types: ["inserted", "attr-name"], style: { color: "rgb(173, 219, 103)", - fontStyle: "italic" - } + fontStyle: "italic", + }, }, { types: ["comment"], style: { color: "rgb(99, 119, 119)", - fontStyle: "italic" - } + fontStyle: "italic", + }, }, { types: ["string", "url"], style: { - color: "rgb(173, 219, 103)" - } + color: "rgb(173, 219, 103)", + }, }, { types: ["variable"], style: { - color: "rgb(214, 222, 235)" - } + color: "rgb(214, 222, 235)", + }, }, { types: ["number"], style: { - color: "rgb(247, 140, 108)" - } + color: "rgb(247, 140, 108)", + }, }, { types: ["builtin", "char", "constant", "function"], style: { - color: "rgb(130, 170, 255)" - } + color: "rgb(130, 170, 255)", + }, }, { // This was manually added after the auto-generation // so that punctuations are not italicised types: ["punctuation"], style: { - color: "rgb(199, 146, 234)" - } + color: "rgb(199, 146, 234)", + }, }, { types: ["selector", "doctype"], style: { color: "rgb(199, 146, 234)", - fontStyle: "italic" - } + fontStyle: "italic", + }, }, { types: ["class-name"], style: { - color: "rgb(255, 203, 139)" - } + color: "rgb(255, 203, 139)", + }, }, { types: ["tag", "operator", "keyword"], style: { - color: "rgb(127, 219, 202)" - } + color: "rgb(127, 219, 202)", + }, }, { types: ["boolean"], style: { - color: "rgb(255, 88, 116)" - } + color: "rgb(255, 88, 116)", + }, }, { types: ["property"], style: { - color: "rgb(128, 203, 196)" - } + color: "rgb(128, 203, 196)", + }, }, { types: ["namespace"], style: { - color: "rgb(178, 204, 214)" - } - } - ] + color: "rgb(178, 204, 214)", + }, + }, + ], }; export default theme; diff --git a/src/themes/nightOwlLight.js b/src/themes/nightOwlLight.js index 2ad909c..f6bc4de 100644 --- a/src/themes/nightOwlLight.js +++ b/src/themes/nightOwlLight.js @@ -7,95 +7,95 @@ import type { PrismTheme } from "../types"; var theme: PrismTheme = { plain: { color: "#403f53", - backgroundColor: "#FBFBFB" + backgroundColor: "#FBFBFB", }, styles: [ { types: ["changed"], style: { color: "rgb(162, 191, 252)", - fontStyle: "italic" - } + fontStyle: "italic", + }, }, { types: ["deleted"], style: { color: "rgba(239, 83, 80, 0.56)", - fontStyle: "italic" - } + fontStyle: "italic", + }, }, { types: ["inserted", "attr-name"], style: { color: "rgb(72, 118, 214)", - fontStyle: "italic" - } + fontStyle: "italic", + }, }, { types: ["comment"], style: { color: "rgb(152, 159, 177)", - fontStyle: "italic" - } + fontStyle: "italic", + }, }, { types: ["string", "builtin", "char", "constant", "url"], style: { - color: "rgb(72, 118, 214)" - } + color: "rgb(72, 118, 214)", + }, }, { types: ["variable"], style: { - color: "rgb(201, 103, 101)" - } + color: "rgb(201, 103, 101)", + }, }, { types: ["number"], style: { - color: "rgb(170, 9, 130)" - } + color: "rgb(170, 9, 130)", + }, }, { // This was manually added after the auto-generation // so that punctuations are not italicised types: ["punctuation"], style: { - color: "rgb(153, 76, 195)" - } + color: "rgb(153, 76, 195)", + }, }, { types: ["function", "selector", "doctype"], style: { color: "rgb(153, 76, 195)", - fontStyle: "italic" - } + fontStyle: "italic", + }, }, { types: ["class-name"], style: { - color: "rgb(17, 17, 17)" - } + color: "rgb(17, 17, 17)", + }, }, { types: ["tag"], style: { - color: "rgb(153, 76, 195)" - } + color: "rgb(153, 76, 195)", + }, }, { types: ["operator", "property", "keyword", "namespace"], style: { - color: "rgb(12, 150, 155)" - } + color: "rgb(12, 150, 155)", + }, }, { types: ["boolean"], style: { - color: "rgb(188, 84, 84)" - } - } - ] + color: "rgb(188, 84, 84)", + }, + }, + ], }; export default theme; diff --git a/src/themes/oceanicNext.js b/src/themes/oceanicNext.js index 08865b4..492969e 100755 --- a/src/themes/oceanicNext.js +++ b/src/themes/oceanicNext.js @@ -1,143 +1,150 @@ -// @flow -// Oceanic Next -// Author: Dmitri Voronianski (https://github.com/voronianski) -// https://github.com/voronianski/oceanic-next-color-scheme -// Adapted from: https://github.com/reactjs/reactjs.org/blob/428d52b/src/prism-styles.js - -import type { PrismTheme } from "../types"; - -var colors = { - char: "#D8DEE9", - comment: "#999999", - keyword: "#c5a5c5", - primitive: "#5a9bcf", - string: "#8dc891", - variable: "#d7deea", - boolean: "#ff8b50", - punctuation: "#5FB3B3", - tag: "#fc929e", - function: "#79b6f2", - className: "#FAC863", - method: "#6699CC", - operator: "#fc929e" -}; - -var theme: PrismTheme = { - plain: { - backgroundColor: "#282c34", - color: "#ffffff" - }, - styles: [ - { - types: ["attr-name"], - style: { - color: colors.keyword - } - }, - { - types: ["attr-value"], - style: { - color: colors.string - } - }, - { - types: ["comment", "block-comment", "prolog", "doctype", "cdata", "shebang"], - style: { - color: colors.comment - } - }, - { - types: [ - "property", - "number", - "function-name", - "constant", - "symbol", - "deleted" - ], - style: { - color: colors.primitive - } - }, - { - types: ["boolean"], - style: { - color: colors.boolean - } - }, - { - types: ["tag"], - style: { - color: colors.tag - } - }, - { - types: ["string"], - style: { - color: colors.string - } - }, - { - types: ["punctuation"], - style: { - color: colors.string - } - }, - { - types: ["selector", "char", "builtin", "inserted"], - style: { - color: colors.char - } - }, - { - types: ["function"], - style: { - color: colors.function - } - }, - { - types: ["operator", "entity", "url", "variable"], - style: { - color: colors.variable - } - }, - { - types: ["keyword"], - style: { - color: colors.keyword - } - }, - { - types: ["at-rule", "class-name"], - style: { - color: colors.className - } - }, - { - types: ["important"], - style: { - fontWeight: "400" - } - }, - { - types: ["bold"], - style: { - fontWeight: "bold" - } - }, - { - types: ["italic"], - style: { - fontStyle: "italic" - } - }, - { - types: ["namespace"], - style: { - opacity: 0.7 - } - } - ] -}; - -export default theme; +// @flow +// Oceanic Next +// Author: Dmitri Voronianski (https://github.com/voronianski) +// https://github.com/voronianski/oceanic-next-color-scheme +// Adapted from: https://github.com/reactjs/reactjs.org/blob/428d52b/src/prism-styles.js + +import type { PrismTheme } from "../types"; + +var colors = { + char: "#D8DEE9", + comment: "#999999", + keyword: "#c5a5c5", + primitive: "#5a9bcf", + string: "#8dc891", + variable: "#d7deea", + boolean: "#ff8b50", + punctuation: "#5FB3B3", + tag: "#fc929e", + function: "#79b6f2", + className: "#FAC863", + method: "#6699CC", + operator: "#fc929e", +}; + +var theme: PrismTheme = { + plain: { + backgroundColor: "#282c34", + color: "#ffffff", + }, + styles: [ + { + types: ["attr-name"], + style: { + color: colors.keyword, + }, + }, + { + types: ["attr-value"], + style: { + color: colors.string, + }, + }, + { + types: [ + "comment", + "block-comment", + "prolog", + "doctype", + "cdata", + "shebang", + ], + style: { + color: colors.comment, + }, + }, + { + types: [ + "property", + "number", + "function-name", + "constant", + "symbol", + "deleted", + ], + style: { + color: colors.primitive, + }, + }, + { + types: ["boolean"], + style: { + color: colors.boolean, + }, + }, + { + types: ["tag"], + style: { + color: colors.tag, + }, + }, + { + types: ["string"], + style: { + color: colors.string, + }, + }, + { + types: ["punctuation"], + style: { + color: colors.string, + }, + }, + { + types: ["selector", "char", "builtin", "inserted"], + style: { + color: colors.char, + }, + }, + { + types: ["function"], + style: { + color: colors.function, + }, + }, + { + types: ["operator", "entity", "url", "variable"], + style: { + color: colors.variable, + }, + }, + { + types: ["keyword"], + style: { + color: colors.keyword, + }, + }, + { + types: ["at-rule", "class-name"], + style: { + color: colors.className, + }, + }, + { + types: ["important"], + style: { + fontWeight: "400", + }, + }, + { + types: ["bold"], + style: { + fontWeight: "bold", + }, + }, + { + types: ["italic"], + style: { + fontStyle: "italic", + }, + }, + { + types: ["namespace"], + style: { + opacity: 0.7, + }, + }, + ], +}; + +export default theme; diff --git a/src/themes/palenight.js b/src/themes/palenight.js index 0c26cb9..caa8323 100644 --- a/src/themes/palenight.js +++ b/src/themes/palenight.js @@ -6,96 +6,96 @@ import type { PrismTheme } from "../types"; var theme: PrismTheme = { plain: { color: "#bfc7d5", - backgroundColor: "#292d3e" + backgroundColor: "#292d3e", }, styles: [ { types: ["comment"], style: { color: "rgb(105, 112, 152)", - fontStyle: "italic" - } + fontStyle: "italic", + }, }, { types: ["string", "inserted"], style: { - color: "rgb(195, 232, 141)" - } + color: "rgb(195, 232, 141)", + }, }, { types: ["number"], style: { - color: "rgb(247, 140, 108)" - } + color: "rgb(247, 140, 108)", + }, }, { types: ["builtin", "char", "constant", "function"], style: { - color: "rgb(130, 170, 255)" - } + color: "rgb(130, 170, 255)", + }, }, { types: ["punctuation", "selector"], style: { - color: "rgb(199, 146, 234)" - } + color: "rgb(199, 146, 234)", + }, }, { types: ["variable"], style: { - color: "rgb(191, 199, 213)" - } + color: "rgb(191, 199, 213)", + }, }, { types: ["class-name", "attr-name"], style: { - color: "rgb(255, 203, 107)" - } + color: "rgb(255, 203, 107)", + }, }, { types: ["tag", "deleted"], style: { - color: "rgb(255, 85, 114)" - } + color: "rgb(255, 85, 114)", + }, }, { types: ["operator"], style: { - color: "rgb(137, 221, 255)" - } + color: "rgb(137, 221, 255)", + }, }, { types: ["boolean"], style: { - color: "rgb(255, 88, 116)" - } + color: "rgb(255, 88, 116)", + }, }, { types: ["keyword"], style: { - fontStyle: "italic" - } + fontStyle: "italic", + }, }, { types: ["doctype"], style: { color: "rgb(199, 146, 234)", - fontStyle: "italic" - } + fontStyle: "italic", + }, }, { types: ["namespace"], style: { - color: "rgb(178, 204, 214)" - } + color: "rgb(178, 204, 214)", + }, }, { types: ["url"], style: { - color: "rgb(221, 221, 221)" - } - } - ] + color: "rgb(221, 221, 221)", + }, + }, + ], }; export default theme; diff --git a/src/themes/shadesOfPurple.js b/src/themes/shadesOfPurple.js index 3ffb68c..0fde229 100644 --- a/src/themes/shadesOfPurple.js +++ b/src/themes/shadesOfPurple.js @@ -9,69 +9,69 @@ import type { PrismTheme } from "../types"; var theme: PrismTheme = { plain: { color: "#9EFEFF", - backgroundColor: "#2D2A55" + backgroundColor: "#2D2A55", }, styles: [ { types: ["changed"], style: { - color: "rgb(255, 238, 128)" - } + color: "rgb(255, 238, 128)", + }, }, { types: ["deleted"], style: { - color: "rgba(239, 83, 80, 0.56)" - } + color: "rgba(239, 83, 80, 0.56)", + }, }, { types: ["inserted"], style: { - color: "rgb(173, 219, 103)" - } + color: "rgb(173, 219, 103)", + }, }, { types: ["comment"], style: { color: "rgb(179, 98, 255)", - fontStyle: "italic" - } + fontStyle: "italic", + }, }, { types: ["punctuation"], style: { - color: "rgb(255, 255, 255)" - } + color: "rgb(255, 255, 255)", + }, }, { types: ["constant"], style: { - color: "rgb(255, 98, 140)" - } + color: "rgb(255, 98, 140)", + }, }, { types: ["string", "url"], style: { - color: "rgb(165, 255, 144)" - } + color: "rgb(165, 255, 144)", + }, }, { types: ["variable"], style: { - color: "rgb(255, 238, 128)" - } + color: "rgb(255, 238, 128)", + }, }, { types: ["number", "boolean"], style: { - color: "rgb(255, 98, 140)" - } + color: "rgb(255, 98, 140)", + }, }, { types: ["attr-name"], style: { - color: "rgb(255, 180, 84)" - } + color: "rgb(255, 180, 84)", + }, }, { types: [ @@ -81,19 +81,19 @@ var theme: PrismTheme = { "namespace", "tag", "selector", - "doctype" + "doctype", ], style: { - color: "rgb(255, 157, 0)" - } + color: "rgb(255, 157, 0)", + }, }, { types: ["builtin", "char", "constant", "function", "class-name"], style: { - color: "rgb(250, 208, 0)" - } - } - ] + color: "rgb(250, 208, 0)", + }, + }, + ], }; export default theme; diff --git a/src/themes/synthwave84.js b/src/themes/synthwave84.js index 147b3a4..b583b11 100644 --- a/src/themes/synthwave84.js +++ b/src/themes/synthwave84.js @@ -16,113 +16,124 @@ var theme: PrismTheme = { textShadow: "0 0 2px #100c0f, 0 0 5px #dc078e33, 0 0 10px #fff3", }, - styles: [{ + styles: [ + { types: ["comment", "block-comment", "prolog", "doctype", "cdata"], style: { color: "#495495", - fontStyle: "italic" - } + fontStyle: "italic", + }, }, { types: ["punctuation"], style: { - color: "#ccc" - } + color: "#ccc", + }, }, { - types: ["tag", "attr-name", "namespace", "number", "unit", "hexcode", "deleted"], + types: [ + "tag", + "attr-name", + "namespace", + "number", + "unit", + "hexcode", + "deleted", + ], style: { - color: "#e2777a" - } + color: "#e2777a", + }, }, { types: ["property", "selector"], style: { color: "#72f1b8", - textShadow: "0 0 2px #100c0f, 0 0 10px #257c5575, 0 0 35px #21272475" - } + textShadow: "0 0 2px #100c0f, 0 0 10px #257c5575, 0 0 35px #21272475", + }, }, { types: ["function-name"], style: { - color: "#6196cc" - } + color: "#6196cc", + }, }, { types: ["boolean", "selector-id", "function"], style: { color: "#fdfdfd", - textShadow: "0 0 2px #001716, 0 0 3px #03edf975, 0 0 5px #03edf975, 0 0 8px #03edf975" - } + textShadow: + "0 0 2px #001716, 0 0 3px #03edf975, 0 0 5px #03edf975, 0 0 8px #03edf975", + }, }, { types: ["class-name", "maybe-class-name", "builtin"], style: { color: "#fff5f6", - textShadow: "0 0 2px #000, 0 0 10px #fc1f2c75, 0 0 5px #fc1f2c75, 0 0 25px #fc1f2c75" - } + textShadow: + "0 0 2px #000, 0 0 10px #fc1f2c75, 0 0 5px #fc1f2c75, 0 0 25px #fc1f2c75", + }, }, { types: ["constant", "symbol"], style: { color: "#f92aad", - textShadow: "0 0 2px #100c0f, 0 0 5px #dc078e33, 0 0 10px #fff3" - } + textShadow: "0 0 2px #100c0f, 0 0 5px #dc078e33, 0 0 10px #fff3", + }, }, { types: ["important", "atrule", "keyword", "selector-class"], style: { color: "#f4eee4", - textShadow: "0 0 2px #393a33, 0 0 8px #f39f0575, 0 0 2px #f39f0575" - } + textShadow: "0 0 2px #393a33, 0 0 8px #f39f0575, 0 0 2px #f39f0575", + }, }, { types: ["string", "char", "attr-value", "regex", "variable"], style: { - color: "#f87c32" - } + color: "#f87c32", + }, }, { types: ["parameter"], style: { - fontStyle: "italic" - } + fontStyle: "italic", + }, }, { types: ["entity", "url"], style: { - color: "#67cdcc" - } + color: "#67cdcc", + }, }, { types: ["operator"], style: { - color: "ffffffee" - } + color: "ffffffee", + }, }, { types: ["important", "bold"], style: { - fontWeight: "bold" - } + fontWeight: "bold", + }, }, { types: ["italic"], style: { - fontStyle: "italic" - } + fontStyle: "italic", + }, }, { types: ["entity"], style: { - cursor: "help" - } + cursor: "help", + }, }, { types: ["inserted"], style: { - color: "green" - } + color: "green", + }, }, ], }; diff --git a/src/themes/ultramin.js b/src/themes/ultramin.js index d5f3cfc..5d124f5 100755 --- a/src/themes/ultramin.js +++ b/src/themes/ultramin.js @@ -7,28 +7,28 @@ import type { PrismTheme } from "../types"; var theme: PrismTheme = { plain: { color: "#282a2e", - backgroundColor: "#ffffff" + backgroundColor: "#ffffff", }, styles: [ { types: ["comment"], style: { - color: "rgb(197, 200, 198)" - } + color: "rgb(197, 200, 198)", + }, }, { types: ["string", "number", "builtin", "variable"], style: { - color: "rgb(150, 152, 150)" - } + color: "rgb(150, 152, 150)", + }, }, { types: ["class-name", "function", "tag", "attr-name"], style: { - color: "rgb(40, 42, 46)" - } - } - ] + color: "rgb(40, 42, 46)", + }, + }, + ], }; export default theme; diff --git a/src/themes/vsDark.js b/src/themes/vsDark.js index 37d3985..fd82aac 100644 --- a/src/themes/vsDark.js +++ b/src/themes/vsDark.js @@ -6,105 +6,105 @@ import type { PrismTheme } from "../types"; var theme: PrismTheme = { plain: { color: "#9CDCFE", - backgroundColor: "#1E1E1E" + backgroundColor: "#1E1E1E", }, styles: [ { types: ["prolog"], style: { - color: "rgb(0, 0, 128)" - } + color: "rgb(0, 0, 128)", + }, }, { types: ["comment"], style: { - color: "rgb(106, 153, 85)" - } + color: "rgb(106, 153, 85)", + }, }, { types: ["builtin", "changed", "keyword"], style: { - color: "rgb(86, 156, 214)" - } + color: "rgb(86, 156, 214)", + }, }, { types: ["number", "inserted"], style: { - color: "rgb(181, 206, 168)" - } + color: "rgb(181, 206, 168)", + }, }, { types: ["constant"], style: { - color: "rgb(100, 102, 149)" - } + color: "rgb(100, 102, 149)", + }, }, { types: ["attr-name", "variable"], style: { - color: "rgb(156, 220, 254)" - } + color: "rgb(156, 220, 254)", + }, }, { types: ["deleted", "string", "attr-value"], style: { - color: "rgb(206, 145, 120)" - } + color: "rgb(206, 145, 120)", + }, }, { types: ["selector"], style: { - color: "rgb(215, 186, 125)" - } + color: "rgb(215, 186, 125)", + }, }, { // Fix tag color types: ["tag"], style: { - color: "rgb(78, 201, 176)" - } + color: "rgb(78, 201, 176)", + }, }, { // Fix tag color for HTML types: ["tag"], languages: ["markup"], style: { - color: "rgb(86, 156, 214)" - } + color: "rgb(86, 156, 214)", + }, }, { types: ["punctuation", "operator"], style: { - color: "rgb(212, 212, 212)" - } + color: "rgb(212, 212, 212)", + }, }, { // Fix punctuation color for HTML types: ["punctuation"], languages: ["markup"], style: { - color: "#808080" - } + color: "#808080", + }, }, { types: ["function"], style: { - color: "rgb(220, 220, 170)" - } + color: "rgb(220, 220, 170)", + }, }, { types: ["class-name"], style: { - color: "rgb(78, 201, 176)" - } + color: "rgb(78, 201, 176)", + }, }, { types: ["char"], style: { - color: "rgb(209, 105, 105)" - } - } - ] + color: "rgb(209, 105, 105)", + }, + }, + ], }; export default theme; diff --git a/src/utils/normalizeTokens.js b/src/utils/normalizeTokens.js index 35a9b62..1aaf556 100755 --- a/src/utils/normalizeTokens.js +++ b/src/utils/normalizeTokens.js @@ -1,107 +1,107 @@ -// @flow - -import type { PrismToken, Token } from "../types"; - -const newlineRe = /\r\n|\r|\n/; - -// Empty lines need to contain a single empty token, denoted with { empty: true } -const normalizeEmptyLines = (line: Token[]) => { - if (line.length === 0) { - line.push({ - types: ["plain"], - content: "\n", - empty: true - }); - } else if (line.length === 1 && line[0].content === "") { - line[0].content = "\n"; - line[0].empty = true; - } -}; - -const appendTypes = (types: string[], add: string[] | string): string[] => { - const typesSize = types.length; - if (typesSize > 0 && types[typesSize - 1] === add) { - return types; - } - - return types.concat(add); -}; - -// Takes an array of Prism's tokens and groups them by line, turning plain -// strings into tokens as well. Tokens can become recursive in some cases, -// which means that their types are concatenated. Plain-string tokens however -// are always of type "plain". -// This is not recursive to avoid exceeding the call-stack limit, since it's unclear -// how nested Prism's tokens can become -const normalizeTokens = (tokens: Array): Token[][] => { - const typeArrStack: string[][] = [[]]; - const tokenArrStack = [tokens]; - const tokenArrIndexStack = [0]; - const tokenArrSizeStack = [tokens.length]; - - let i = 0; - let stackIndex = 0; - let currentLine = []; - - const acc = [currentLine]; - - while (stackIndex > -1) { - while ( - (i = tokenArrIndexStack[stackIndex]++) < tokenArrSizeStack[stackIndex] - ) { - let content; - let types = typeArrStack[stackIndex]; - - const tokenArr = tokenArrStack[stackIndex]; - const token = tokenArr[i]; - - // Determine content and append type to types if necessary - if (typeof token === "string") { - types = stackIndex > 0 ? types : ["plain"]; - content = token; - } else { - types = appendTypes(types, token.type); - if (token.alias) { - types = appendTypes(types, token.alias); - } - - content = token.content; - } - - // If token.content is an array, increase the stack depth and repeat this while-loop - if (typeof content !== "string") { - stackIndex++; - typeArrStack.push(types); - tokenArrStack.push(content); - tokenArrIndexStack.push(0); - tokenArrSizeStack.push(content.length); - continue; - } - - // Split by newlines - const splitByNewlines = content.split(newlineRe); - const newlineCount = splitByNewlines.length; - - currentLine.push({ types, content: splitByNewlines[0] }); - - // Create a new line for each string on a new line - for (let i = 1; i < newlineCount; i++) { - normalizeEmptyLines(currentLine); - acc.push((currentLine = [])); - currentLine.push({ types, content: splitByNewlines[i] }); - } - } - - // Decreate the stack depth - stackIndex--; - typeArrStack.pop(); - tokenArrStack.pop(); - tokenArrIndexStack.pop(); - tokenArrSizeStack.pop(); - } - - normalizeEmptyLines(currentLine); - return acc; -}; - -export default normalizeTokens; +// @flow + +import type { PrismToken, Token } from "../types"; + +const newlineRe = /\r\n|\r|\n/; + +// Empty lines need to contain a single empty token, denoted with { empty: true } +const normalizeEmptyLines = (line: Token[]) => { + if (line.length === 0) { + line.push({ + types: ["plain"], + content: "\n", + empty: true, + }); + } else if (line.length === 1 && line[0].content === "") { + line[0].content = "\n"; + line[0].empty = true; + } +}; + +const appendTypes = (types: string[], add: string[] | string): string[] => { + const typesSize = types.length; + if (typesSize > 0 && types[typesSize - 1] === add) { + return types; + } + + return types.concat(add); +}; + +// Takes an array of Prism's tokens and groups them by line, turning plain +// strings into tokens as well. Tokens can become recursive in some cases, +// which means that their types are concatenated. Plain-string tokens however +// are always of type "plain". +// This is not recursive to avoid exceeding the call-stack limit, since it's unclear +// how nested Prism's tokens can become +const normalizeTokens = (tokens: Array): Token[][] => { + const typeArrStack: string[][] = [[]]; + const tokenArrStack = [tokens]; + const tokenArrIndexStack = [0]; + const tokenArrSizeStack = [tokens.length]; + + let i = 0; + let stackIndex = 0; + let currentLine = []; + + const acc = [currentLine]; + + while (stackIndex > -1) { + while ( + (i = tokenArrIndexStack[stackIndex]++) < tokenArrSizeStack[stackIndex] + ) { + let content; + let types = typeArrStack[stackIndex]; + + const tokenArr = tokenArrStack[stackIndex]; + const token = tokenArr[i]; + + // Determine content and append type to types if necessary + if (typeof token === "string") { + types = stackIndex > 0 ? types : ["plain"]; + content = token; + } else { + types = appendTypes(types, token.type); + if (token.alias) { + types = appendTypes(types, token.alias); + } + + content = token.content; + } + + // If token.content is an array, increase the stack depth and repeat this while-loop + if (typeof content !== "string") { + stackIndex++; + typeArrStack.push(types); + tokenArrStack.push(content); + tokenArrIndexStack.push(0); + tokenArrSizeStack.push(content.length); + continue; + } + + // Split by newlines + const splitByNewlines = content.split(newlineRe); + const newlineCount = splitByNewlines.length; + + currentLine.push({ types, content: splitByNewlines[0] }); + + // Create a new line for each string on a new line + for (let i = 1; i < newlineCount; i++) { + normalizeEmptyLines(currentLine); + acc.push((currentLine = [])); + currentLine.push({ types, content: splitByNewlines[i] }); + } + } + + // Decreate the stack depth + stackIndex--; + typeArrStack.pop(); + tokenArrStack.pop(); + tokenArrIndexStack.pop(); + tokenArrSizeStack.pop(); + } + + normalizeEmptyLines(currentLine); + return acc; +}; + +export default normalizeTokens; diff --git a/src/utils/themeToDict.js b/src/utils/themeToDict.js index ff4c03f..07d463c 100755 --- a/src/utils/themeToDict.js +++ b/src/utils/themeToDict.js @@ -1,41 +1,41 @@ -// @flow - -import type { Language, StyleObj, PrismTheme, PrismThemeEntry } from "../types"; - -export type ThemeDict = { - root: StyleObj, - plain: StyleObj, - [type: string]: StyleObj -}; - -const themeToDict = (theme: PrismTheme, language: Language): ThemeDict => { - const { plain } = theme; - - // $FlowFixMe - const base: ThemeDict = Object.create(null); - - const themeDict = theme.styles.reduce((acc, themeEntry) => { - const { types, languages, style } = themeEntry; - if (languages && !languages.includes(language)) { - return acc; - } - - themeEntry.types.forEach(type => { - // $FlowFixMe - const accStyle: StyleObj = { ...acc[type], ...style }; - - acc[type] = accStyle; - }); - - return acc; - }, base); - - // $FlowFixMe - themeDict.root = (plain: StyleObj); - // $FlowFixMe - themeDict.plain = ({ ...plain, backgroundColor: null }: StyleObj); - - return themeDict; -}; - -export default themeToDict; +// @flow + +import type { Language, StyleObj, PrismTheme, PrismThemeEntry } from "../types"; + +export type ThemeDict = { + root: StyleObj, + plain: StyleObj, + [type: string]: StyleObj, +}; + +const themeToDict = (theme: PrismTheme, language: Language): ThemeDict => { + const { plain } = theme; + + // $FlowFixMe + const base: ThemeDict = Object.create(null); + + const themeDict = theme.styles.reduce((acc, themeEntry) => { + const { types, languages, style } = themeEntry; + if (languages && !languages.includes(language)) { + return acc; + } + + themeEntry.types.forEach((type) => { + // $FlowFixMe + const accStyle: StyleObj = { ...acc[type], ...style }; + + acc[type] = accStyle; + }); + + return acc; + }, base); + + // $FlowFixMe + themeDict.root = (plain: StyleObj); + // $FlowFixMe + themeDict.plain = ({ ...plain, backgroundColor: null }: StyleObj); + + return themeDict; +}; + +export default themeToDict;