diff --git a/src/types.js b/src/types.js index bb33d54..f1a4d13 100755 --- a/src/types.js +++ b/src/types.js @@ -1,112 +1,122 @@ -// @flow - -import type { Key } from "react"; -import includedLangs from "./vendor/prism/includeLangs"; - -export type Language = $Keys; - -export type PrismGrammar = { - [key: string]: mixed -}; - -type LanguagesDict = { - [lang: Language]: PrismGrammar -}; - -export type PrismToken = { - type: string | string[], - alias: string | string[], - content: Array | string -}; - -export type Token = { - types: string[], - content: string, - empty?: boolean -}; - -export type PrismLib = { - languages: LanguagesDict, - tokenize: ( - code: string, - grammar: PrismGrammar, - language: Language - ) => Array, - highlight: (code: string, grammar: PrismGrammar, language: Language) => string -}; - -export type StyleObj = { - [key: string]: string | number | null -}; - -export type LineInputProps = { - key?: Key, - style?: StyleObj, - className?: string, - line: Token[], - [key: string]: mixed -}; - -export type LineOutputProps = { - key?: Key, - style?: StyleObj, - className: string, - [key: string]: mixed -}; - -export type TokenInputProps = { - key?: Key, - style?: StyleObj, - className?: string, - token: Token, - [key: string]: mixed -}; - -export type TokenOutputProps = { - key?: Key, - style?: StyleObj, - className: string, - children: string, - [key: string]: mixed -}; - -export type RenderProps = { - tokens: Token[][], - className: string, - getLineProps: (input: LineInputProps) => LineOutputProps, - getTokenProps: (input: TokenInputProps) => TokenOutputProps -}; - -export type PrismThemeEntry = { - color?: string, - backgroundColor?: string, - fontStyle?: "normal" | "italic", - fontWeight?: - | "normal" - | "bold" - | "100" - | "200" - | "300" - | "400" - | "500" - | "600" - | "700" - | "800" - | "900", - textDecorationLine?: - | "none" - | "underline" - | "line-through" - | "underline line-through", - opacity?: number, - [styleKey: string]: string | number | void -}; - -export type PrismTheme = { - plain: PrismThemeEntry, - styles: Array<{ - types: string[], - style: PrismThemeEntry, - languages?: Language[] - }> -}; +// @flow + +import type { Key } from "react"; +import includedLangs from "./vendor/prism/includeLangs"; + +export type Language = $Keys; + +export type PrismGrammar = { + [key: string]: mixed, +}; + +type LanguagesDict = { + [lang: Language]: PrismGrammar, +}; + +export type PrismToken = { + type: string | string[], + alias: string | string[], + content: Array | string, +}; + +export type Token = { + types: string[], + content: string, + empty?: boolean, +}; + +export type PrismLib = { + languages: LanguagesDict, + tokenize: ( + code: string, + grammar: PrismGrammar, + language: Language + ) => Array, + highlight: ( + code: string, + grammar: PrismGrammar, + language: Language + ) => string, + hooks: { + run: ( + name: string, + env: { code: string, grammar: PrismGrammar, language: Language } + ) => void, + }, +}; + +export type StyleObj = { + [key: string]: string | number | null, +}; + +export type LineInputProps = { + key?: Key, + style?: StyleObj, + className?: string, + line: Token[], + [key: string]: mixed, +}; + +export type LineOutputProps = { + key?: Key, + style?: StyleObj, + className: string, + [key: string]: mixed, +}; + +export type TokenInputProps = { + key?: Key, + style?: StyleObj, + className?: string, + token: Token, + [key: string]: mixed, +}; + +export type TokenOutputProps = { + key?: Key, + style?: StyleObj, + className: string, + children: string, + [key: string]: mixed, +}; + +export type RenderProps = { + tokens: Token[][], + className: string, + getLineProps: (input: LineInputProps) => LineOutputProps, + getTokenProps: (input: TokenInputProps) => TokenOutputProps, +}; + +export type PrismThemeEntry = { + color?: string, + backgroundColor?: string, + fontStyle?: "normal" | "italic", + fontWeight?: + | "normal" + | "bold" + | "100" + | "200" + | "300" + | "400" + | "500" + | "600" + | "700" + | "800" + | "900", + textDecorationLine?: + | "none" + | "underline" + | "line-through" + | "underline line-through", + opacity?: number, + [styleKey: string]: string | number | void, +}; + +export type PrismTheme = { + plain: PrismThemeEntry, + styles: Array<{ + types: string[], + style: PrismThemeEntry, + languages?: Language[], + }>, +}; diff --git a/src/vendor/prism/prism-core.js b/src/vendor/prism/prism-core.js index 1fbea36..029da46 100755 --- a/src/vendor/prism/prism-core.js +++ b/src/vendor/prism/prism-core.js @@ -1,429 +1,430 @@ -/** - * Prism: Lightweight, robust, elegant syntax highlighting - * MIT license http://www.opensource.org/licenses/mit-license.php/ - * @author Lea Verou http://lea.verou.me - */ - -/** - * prism-react-renderer: - * This file has been modified to remove: - * - globals and window dependency - * - worker support - * - highlightAll and other element dependent methods - * - _.hooks helpers - * - UMD/node-specific hacks - * It has also been run through prettier - */ - -var Prism = (function() { - // Private helper vars - var lang = /\blang(?:uage)?-([\w-]+)\b/i; - var uniqueId = 0; - - var _ = { - util: { - encode: function(tokens) { - if (tokens instanceof Token) { - return new Token( - tokens.type, - _.util.encode(tokens.content), - tokens.alias - ); - } else if (_.util.type(tokens) === "Array") { - return tokens.map(_.util.encode); - } else { - return tokens - .replace(/&/g, "&") - .replace(/ text.length) { - // Something went terribly wrong, ABORT, ABORT! - return; - } - - if (str instanceof Token) { - continue; - } - - if (greedy && i != strarr.length - 1) { - pattern.lastIndex = pos; - var match = pattern.exec(text); - if (!match) { - break; - } - - var from = match.index + (lookbehind ? match[1].length : 0), - to = match.index + match[0].length, - k = i, - p = pos; - - for ( - var len = strarr.length; - k < len && - (p < to || (!strarr[k].type && !strarr[k - 1].greedy)); - ++k - ) { - p += strarr[k].length; - // Move the index i to the element in strarr that is closest to from - if (from >= p) { - ++i; - pos = p; - } - } - - // If strarr[i] is a Token, then the match starts inside another Token, which is invalid - if (strarr[i] instanceof Token) { - continue; - } - - // Number of tokens to delete and replace with the new match - delNum = k - i; - str = text.slice(pos, p); - match.index -= pos; - } else { - pattern.lastIndex = 0; - - var match = pattern.exec(str), - delNum = 1; - } - - if (!match) { - if (oneshot) { - break; - } - - continue; - } - - if (lookbehind) { - lookbehindLength = match[1] ? match[1].length : 0; - } - - var from = match.index + lookbehindLength, - match = match[0].slice(lookbehindLength), - to = from + match.length, - before = str.slice(0, from), - after = str.slice(to); - - var args = [i, delNum]; - - if (before) { - ++i; - pos += before.length; - args.push(before); - } - - var wrapped = new Token( - token, - inside ? _.tokenize(match, inside) : match, - alias, - match, - greedy - ); - - args.push(wrapped); - - if (after) { - args.push(after); - } - - Array.prototype.splice.apply(strarr, args); - - if (delNum != 1) - _.matchGrammar(text, strarr, grammar, i, pos, true, token); - - if (oneshot) break; - } - } - } - }, - - hooks: { - add: function() {} - }, - - tokenize: function(text, grammar, language) { - var strarr = [text]; - - var rest = grammar.rest; - - if (rest) { - for (var token in rest) { - grammar[token] = rest[token]; - } - - delete grammar.rest; - } - - _.matchGrammar(text, strarr, grammar, 0, 0, false); - - return strarr; - } - }; - - var Token = (_.Token = function(type, content, alias, matchedStr, greedy) { - this.type = type; - this.content = content; - this.alias = alias; - // Copy of the full string this token was created from - this.length = (matchedStr || "").length | 0; - this.greedy = !!greedy; - }); - - Token.stringify = function(o, language, parent) { - if (typeof o == "string") { - return o; - } - - if (_.util.type(o) === "Array") { - return o - .map(function(element) { - return Token.stringify(element, language, o); - }) - .join(""); - } - - var env = { - type: o.type, - content: Token.stringify(o.content, language, parent), - tag: "span", - classes: ["token", o.type], - attributes: {}, - language: language, - parent: parent - }; - - if (o.alias) { - var aliases = _.util.type(o.alias) === "Array" ? o.alias : [o.alias]; - Array.prototype.push.apply(env.classes, aliases); - } - - var attributes = Object.keys(env.attributes) - .map(function(name) { - return ( - name + - '="' + - (env.attributes[name] || "").replace(/"/g, """) + - '"' - ); - }) - .join(" "); - - return ( - "<" + - env.tag + - ' class="' + - env.classes.join(" ") + - '"' + - (attributes ? " " + attributes : "") + - ">" + - env.content + - "" - ); - }; - - return _; -})(); - -export default Prism; +/** + * Prism: Lightweight, robust, elegant syntax highlighting + * MIT license http://www.opensource.org/licenses/mit-license.php/ + * @author Lea Verou http://lea.verou.me + */ + +/** + * prism-react-renderer: + * This file has been modified to remove: + * - globals and window dependency + * - worker support + * - highlightAll and other element dependent methods + * - _.hooks helpers + * - UMD/node-specific hacks + * It has also been run through prettier + */ + +var Prism = (function () { + // Private helper vars + var lang = /\blang(?:uage)?-([\w-]+)\b/i; + var uniqueId = 0; + + var _ = { + util: { + encode: function (tokens) { + if (tokens instanceof Token) { + return new Token( + tokens.type, + _.util.encode(tokens.content), + tokens.alias + ); + } else if (_.util.type(tokens) === "Array") { + return tokens.map(_.util.encode); + } else { + return tokens + .replace(/&/g, "&") + .replace(/ text.length) { + // Something went terribly wrong, ABORT, ABORT! + return; + } + + if (str instanceof Token) { + continue; + } + + if (greedy && i != strarr.length - 1) { + pattern.lastIndex = pos; + var match = pattern.exec(text); + if (!match) { + break; + } + + var from = match.index + (lookbehind ? match[1].length : 0), + to = match.index + match[0].length, + k = i, + p = pos; + + for ( + var len = strarr.length; + k < len && + (p < to || (!strarr[k].type && !strarr[k - 1].greedy)); + ++k + ) { + p += strarr[k].length; + // Move the index i to the element in strarr that is closest to from + if (from >= p) { + ++i; + pos = p; + } + } + + // If strarr[i] is a Token, then the match starts inside another Token, which is invalid + if (strarr[i] instanceof Token) { + continue; + } + + // Number of tokens to delete and replace with the new match + delNum = k - i; + str = text.slice(pos, p); + match.index -= pos; + } else { + pattern.lastIndex = 0; + + var match = pattern.exec(str), + delNum = 1; + } + + if (!match) { + if (oneshot) { + break; + } + + continue; + } + + if (lookbehind) { + lookbehindLength = match[1] ? match[1].length : 0; + } + + var from = match.index + lookbehindLength, + match = match[0].slice(lookbehindLength), + to = from + match.length, + before = str.slice(0, from), + after = str.slice(to); + + var args = [i, delNum]; + + if (before) { + ++i; + pos += before.length; + args.push(before); + } + + var wrapped = new Token( + token, + inside ? _.tokenize(match, inside) : match, + alias, + match, + greedy + ); + + args.push(wrapped); + + if (after) { + args.push(after); + } + + Array.prototype.splice.apply(strarr, args); + + if (delNum != 1) + _.matchGrammar(text, strarr, grammar, i, pos, true, token); + + if (oneshot) break; + } + } + } + }, + + hooks: { + add: function () {}, + run: function (name, env) {}, + }, + + tokenize: function (text, grammar, language) { + var strarr = [text]; + + var rest = grammar.rest; + + if (rest) { + for (var token in rest) { + grammar[token] = rest[token]; + } + + delete grammar.rest; + } + + _.matchGrammar(text, strarr, grammar, 0, 0, false); + + return strarr; + }, + }; + + var Token = (_.Token = function (type, content, alias, matchedStr, greedy) { + this.type = type; + this.content = content; + this.alias = alias; + // Copy of the full string this token was created from + this.length = (matchedStr || "").length | 0; + this.greedy = !!greedy; + }); + + Token.stringify = function (o, language, parent) { + if (typeof o == "string") { + return o; + } + + if (_.util.type(o) === "Array") { + return o + .map(function (element) { + return Token.stringify(element, language, o); + }) + .join(""); + } + + var env = { + type: o.type, + content: Token.stringify(o.content, language, parent), + tag: "span", + classes: ["token", o.type], + attributes: {}, + language: language, + parent: parent, + }; + + if (o.alias) { + var aliases = _.util.type(o.alias) === "Array" ? o.alias : [o.alias]; + Array.prototype.push.apply(env.classes, aliases); + } + + var attributes = Object.keys(env.attributes) + .map(function (name) { + return ( + name + + '="' + + (env.attributes[name] || "").replace(/"/g, """) + + '"' + ); + }) + .join(" "); + + return ( + "<" + + env.tag + + ' class="' + + env.classes.join(" ") + + '"' + + (attributes ? " " + attributes : "") + + ">" + + env.content + + "" + ); + }; + + return _; +})(); + +export default Prism;