mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-19 20:17:06 +02:00
Run Prettier
This commit is contained in:
parent
a7b5148e06
commit
fbae29b0ff
29 changed files with 1311 additions and 987 deletions
|
@ -7,8 +7,8 @@
|
|||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
const React = require('react');
|
||||
const unindent = require('./unindent.js');
|
||||
const React = require("react");
|
||||
const unindent = require("./unindent.js");
|
||||
|
||||
/* http://prismjs.com/download.html?themes=prism&languages=markup+clike+javascript+jsx */
|
||||
/**
|
||||
|
@ -20,15 +20,22 @@ const unindent = require('./unindent.js');
|
|||
// Private helper vars
|
||||
const lang = /\blang(?:uage)?-(?!\*)(\w+)\b/i;
|
||||
|
||||
const _ = Prism = {
|
||||
const _ = (Prism = {
|
||||
util: {
|
||||
encode(tokens) {
|
||||
if (tokens instanceof Token) {
|
||||
return new Token(tokens.type, _.util.encode(tokens.content), tokens.alias);
|
||||
} else if (_.util.type(tokens) === 'Array') {
|
||||
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(/</g, '<').replace(/\u00a0/g, ' ');
|
||||
return tokens
|
||||
.replace(/&/g, "&")
|
||||
.replace(/</g, "<")
|
||||
.replace(/\u00a0/g, " ");
|
||||
}
|
||||
},
|
||||
|
||||
|
@ -41,7 +48,7 @@ const _ = Prism = {
|
|||
const type = _.util.type(o);
|
||||
|
||||
switch (type) {
|
||||
case 'Object':
|
||||
case "Object":
|
||||
var clone = {};
|
||||
|
||||
for (const key in o) {
|
||||
|
@ -52,13 +59,18 @@ const _ = Prism = {
|
|||
|
||||
return clone;
|
||||
|
||||
case 'Array':
|
||||
case "Array":
|
||||
// Check for existence for IE8
|
||||
return o.map && o.map(v => { return _.util.clone(v); });
|
||||
return (
|
||||
o.map &&
|
||||
o.map(v => {
|
||||
return _.util.clone(v);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
return o;
|
||||
},
|
||||
}
|
||||
},
|
||||
|
||||
languages: {
|
||||
|
@ -100,13 +112,9 @@ const _ = Prism = {
|
|||
const ret = {};
|
||||
|
||||
for (const token in grammar) {
|
||||
|
||||
if (grammar.hasOwnProperty(token)) {
|
||||
|
||||
if (token == before) {
|
||||
|
||||
for (var newToken in insert) {
|
||||
|
||||
if (insert.hasOwnProperty(newToken)) {
|
||||
ret[newToken] = insert[newToken];
|
||||
}
|
||||
|
@ -124,7 +132,7 @@ const _ = Prism = {
|
|||
}
|
||||
});
|
||||
|
||||
return root[inside] = ret;
|
||||
return (root[inside] = ret);
|
||||
},
|
||||
|
||||
// Traverse a language definition with Depth First Search
|
||||
|
@ -133,45 +141,55 @@ const _ = Prism = {
|
|||
if (o.hasOwnProperty(i)) {
|
||||
callback.call(o, i, o[i], type || i);
|
||||
|
||||
if (_.util.type(o[i]) === 'Object') {
|
||||
if (_.util.type(o[i]) === "Object") {
|
||||
_.languages.DFS(o[i], callback);
|
||||
} else if (_.util.type(o[i]) === 'Array') {
|
||||
} else if (_.util.type(o[i]) === "Array") {
|
||||
_.languages.DFS(o[i], callback, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
|
||||
highlightAll(async, callback) {
|
||||
const elements = document.querySelectorAll('code[class*="language-"], [class*="language-"] code, code[class*="lang-"], [class*="lang-"] code');
|
||||
const elements = document.querySelectorAll(
|
||||
'code[class*="language-"], [class*="language-"] code, code[class*="lang-"], [class*="lang-"] code'
|
||||
);
|
||||
|
||||
for (let i = 0, element; element = elements[i++];) {
|
||||
for (let i = 0, element; (element = elements[i++]); ) {
|
||||
_.highlightElement(element, async === true, callback);
|
||||
}
|
||||
},
|
||||
|
||||
highlightElement(element, async, callback) {
|
||||
// Find language
|
||||
let language, grammar, parent = element;
|
||||
let language,
|
||||
grammar,
|
||||
parent = element;
|
||||
|
||||
while (parent && !lang.test(parent.className)) {
|
||||
parent = parent.parentNode;
|
||||
}
|
||||
|
||||
if (parent) {
|
||||
language = (parent.className.match(lang) || [, ''])[1];
|
||||
language = (parent.className.match(lang) || [, ""])[1];
|
||||
grammar = _.languages[language];
|
||||
}
|
||||
|
||||
// Set language on the element, if not present
|
||||
element.className = element.className.replace(lang, '').replace(/\s+/g, ' ') + ' language-' + language;
|
||||
element.className =
|
||||
element.className.replace(lang, "").replace(/\s+/g, " ") +
|
||||
" language-" +
|
||||
language;
|
||||
|
||||
// Set language on the parent, for styling
|
||||
parent = element.parentNode;
|
||||
|
||||
if (/pre/i.test(parent.nodeName)) {
|
||||
parent.className = parent.className.replace(lang, '').replace(/\s+/g, ' ') + ' language-' + language;
|
||||
parent.className =
|
||||
parent.className.replace(lang, "").replace(/\s+/g, " ") +
|
||||
" language-" +
|
||||
language;
|
||||
}
|
||||
|
||||
if (!grammar) {
|
||||
|
@ -184,16 +202,16 @@ const _ = Prism = {
|
|||
return;
|
||||
}
|
||||
|
||||
code = code.replace(/^(?:\r?\n|\r)/, '');
|
||||
code = code.replace(/^(?:\r?\n|\r)/, "");
|
||||
|
||||
const env = {
|
||||
element,
|
||||
language,
|
||||
grammar,
|
||||
code,
|
||||
code
|
||||
};
|
||||
|
||||
_.hooks.run('before-highlight', env);
|
||||
_.hooks.run("before-highlight", env);
|
||||
|
||||
if (async && _self.Worker) {
|
||||
const worker = new Worker(_.filename);
|
||||
|
@ -201,28 +219,30 @@ const _ = Prism = {
|
|||
worker.onmessage = function(evt) {
|
||||
env.highlightedCode = Token.stringify(JSON.parse(evt.data), language);
|
||||
|
||||
_.hooks.run('before-insert', env);
|
||||
_.hooks.run("before-insert", env);
|
||||
|
||||
env.element.innerHTML = env.highlightedCode;
|
||||
|
||||
callback && callback.call(env.element);
|
||||
_.hooks.run('after-highlight', env);
|
||||
_.hooks.run("after-highlight", env);
|
||||
};
|
||||
|
||||
worker.postMessage(JSON.stringify({
|
||||
language: env.language,
|
||||
code: env.code,
|
||||
}));
|
||||
} else {
|
||||
worker.postMessage(
|
||||
JSON.stringify({
|
||||
language: env.language,
|
||||
code: env.code
|
||||
})
|
||||
);
|
||||
} else {
|
||||
env.highlightedCode = _.highlight(env.code, env.grammar, env.language);
|
||||
|
||||
_.hooks.run('before-insert', env);
|
||||
_.hooks.run("before-insert", env);
|
||||
|
||||
env.element.innerHTML = env.highlightedCode;
|
||||
|
||||
callback && callback.call(element);
|
||||
|
||||
_.hooks.run('after-highlight', env);
|
||||
_.hooks.run("after-highlight", env);
|
||||
}
|
||||
},
|
||||
|
||||
|
@ -252,7 +272,7 @@ const _ = Prism = {
|
|||
}
|
||||
|
||||
let patterns = grammar[token];
|
||||
patterns = (_.util.type(patterns) === 'Array') ? patterns : [patterns];
|
||||
patterns = _.util.type(patterns) === "Array" ? patterns : [patterns];
|
||||
|
||||
for (let j = 0; j < patterns.length; ++j) {
|
||||
let pattern = patterns[j],
|
||||
|
@ -263,7 +283,8 @@ const _ = Prism = {
|
|||
|
||||
pattern = pattern.pattern || pattern;
|
||||
|
||||
for (let i = 0; i < strarr.length; i++) { // Don't cache length as it changes during the loop
|
||||
for (let i = 0; i < strarr.length; i++) {
|
||||
// Don't cache length as it changes during the loop
|
||||
|
||||
const str = strarr[i];
|
||||
|
||||
|
@ -298,7 +319,11 @@ const _ = Prism = {
|
|||
args.push(before);
|
||||
}
|
||||
|
||||
const wrapped = new Token(token, inside ? _.tokenize(match, inside) : match, alias);
|
||||
const wrapped = new Token(
|
||||
token,
|
||||
inside ? _.tokenize(match, inside) : match,
|
||||
alias
|
||||
);
|
||||
|
||||
args.push(wrapped);
|
||||
|
||||
|
@ -333,25 +358,25 @@ const _ = Prism = {
|
|||
return;
|
||||
}
|
||||
|
||||
for (let i = 0, callback; callback = callbacks[i++];) {
|
||||
for (let i = 0, callback; (callback = callbacks[i++]); ) {
|
||||
callback(env);
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const Token = _.Token = function(type, content, alias) {
|
||||
const Token = (_.Token = function(type, content, alias) {
|
||||
this.type = type;
|
||||
this.content = content;
|
||||
this.alias = alias;
|
||||
};
|
||||
});
|
||||
|
||||
Token.reactify = function(o, language, parent, key) {
|
||||
if (typeof o == 'string') {
|
||||
if (typeof o == "string") {
|
||||
return o;
|
||||
}
|
||||
|
||||
if (_.util.type(o) === 'Array') {
|
||||
if (_.util.type(o) === "Array") {
|
||||
return o.map((element, i) => {
|
||||
return Token.reactify(element, language, o, i);
|
||||
});
|
||||
|
@ -360,183 +385,186 @@ Token.reactify = function(o, language, parent, key) {
|
|||
const env = {
|
||||
type: o.type,
|
||||
content: Token.reactify(o.content, language, parent),
|
||||
tag: 'span',
|
||||
classes: ['token', o.type],
|
||||
attributes: {key},
|
||||
tag: "span",
|
||||
classes: ["token", o.type],
|
||||
attributes: { key },
|
||||
language,
|
||||
parent,
|
||||
parent
|
||||
};
|
||||
|
||||
if (env.type == 'comment') {
|
||||
if (env.type == "comment") {
|
||||
env.attributes.spellCheck = true;
|
||||
}
|
||||
|
||||
if (o.alias) {
|
||||
const aliases = _.util.type(o.alias) === 'Array' ? o.alias : [o.alias];
|
||||
const aliases = _.util.type(o.alias) === "Array" ? o.alias : [o.alias];
|
||||
Array.prototype.push.apply(env.classes, aliases);
|
||||
}
|
||||
|
||||
_.hooks.run('wrap', env);
|
||||
_.hooks.run("wrap", env);
|
||||
|
||||
env.attributes.className = env.classes.join(' ');
|
||||
env.attributes.className = env.classes.join(" ");
|
||||
|
||||
return React.DOM[env.tag](env.attributes, env.content);
|
||||
};
|
||||
|
||||
Prism.languages.markup = {
|
||||
'comment': /<!--[\w\W]*?-->/,
|
||||
'prolog': /<\?[\w\W]+?\?>/,
|
||||
'doctype': /<!DOCTYPE[\w\W]+?>/,
|
||||
'cdata': /<!\[CDATA\[[\w\W]*?]]>/i,
|
||||
'tag': {
|
||||
comment: /<!--[\w\W]*?-->/,
|
||||
prolog: /<\?[\w\W]+?\?>/,
|
||||
doctype: /<!DOCTYPE[\w\W]+?>/,
|
||||
cdata: /<!\[CDATA\[[\w\W]*?]]>/i,
|
||||
tag: {
|
||||
pattern: /<\/?[^\s>\/]+(?:\s+[^\s>\/=]+(?:=(?:("|')(?:\\\1|\\?(?!\1)[\w\W])*\1|[^\s'">=]+))?)*\s*\/?>/i,
|
||||
inside: {
|
||||
'tag': {
|
||||
tag: {
|
||||
pattern: /^<\/?[^\s>\/]+/i,
|
||||
inside: {
|
||||
'punctuation': /^<\/?/,
|
||||
'namespace': /^[^\s>\/:]+:/,
|
||||
},
|
||||
punctuation: /^<\/?/,
|
||||
namespace: /^[^\s>\/:]+:/
|
||||
}
|
||||
},
|
||||
'attr-value': {
|
||||
"attr-value": {
|
||||
pattern: /=(?:('|")[\w\W]*?(\1)|[^\s>]+)/i,
|
||||
inside: {
|
||||
'punctuation': /[=>"']/,
|
||||
},
|
||||
punctuation: /[=>"']/
|
||||
}
|
||||
},
|
||||
'punctuation': /\/?>/,
|
||||
'attr-name': {
|
||||
punctuation: /\/?>/,
|
||||
"attr-name": {
|
||||
pattern: /[^\s>\/]+/,
|
||||
inside: {
|
||||
'namespace': /^[^\s>\/:]+:/,
|
||||
},
|
||||
},
|
||||
|
||||
},
|
||||
namespace: /^[^\s>\/:]+:/
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
'entity': /&#?[\da-z]{1,8};/i,
|
||||
entity: /&#?[\da-z]{1,8};/i
|
||||
};
|
||||
|
||||
// Plugin to make entity title show the real entity, idea by Roman Komarov
|
||||
Prism.hooks.add('wrap', env => {
|
||||
|
||||
if (env.type === 'entity') {
|
||||
env.attributes['title'] = env.content.replace(/&/, '&');
|
||||
Prism.hooks.add("wrap", env => {
|
||||
if (env.type === "entity") {
|
||||
env.attributes["title"] = env.content.replace(/&/, "&");
|
||||
}
|
||||
});
|
||||
|
||||
Prism.languages.clike = {
|
||||
'comment': [
|
||||
comment: [
|
||||
{
|
||||
pattern: /(^|[^\\])\/\*[\w\W]*?\*\//,
|
||||
lookbehind: true,
|
||||
lookbehind: true
|
||||
},
|
||||
{
|
||||
pattern: /(^|[^\\:])\/\/.*/,
|
||||
lookbehind: true,
|
||||
},
|
||||
lookbehind: true
|
||||
}
|
||||
],
|
||||
'string': /("|')(\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/,
|
||||
'class-name': {
|
||||
string: /("|')(\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/,
|
||||
"class-name": {
|
||||
pattern: /((?:(?:class|interface|extends|implements|trait|instanceof|new)\s+)|(?:catch\s+\())[a-z0-9_\.\\]+/i,
|
||||
lookbehind: true,
|
||||
inside: {
|
||||
punctuation: /(\.|\\)/,
|
||||
},
|
||||
punctuation: /(\.|\\)/
|
||||
}
|
||||
},
|
||||
'keyword': /\b(if|else|while|do|for|return|in|instanceof|function|new|try|throw|catch|finally|null|break|continue)\b/,
|
||||
'boolean': /\b(true|false)\b/,
|
||||
'function': /[a-z0-9_]+(?=\()/i,
|
||||
'number': /\b-?(0x[\dA-Fa-f]+|\d*\.?\d+([Ee]-?\d+)?)\b/,
|
||||
'operator': /[-+]{1,2}|!|<=?|>=?|={1,3}|&{1,2}|\|?\||\?|\*|\/|~|\^|%/,
|
||||
'punctuation': /[{}[\];(),.:]/,
|
||||
keyword: /\b(if|else|while|do|for|return|in|instanceof|function|new|try|throw|catch|finally|null|break|continue)\b/,
|
||||
boolean: /\b(true|false)\b/,
|
||||
function: /[a-z0-9_]+(?=\()/i,
|
||||
number: /\b-?(0x[\dA-Fa-f]+|\d*\.?\d+([Ee]-?\d+)?)\b/,
|
||||
operator: /[-+]{1,2}|!|<=?|>=?|={1,3}|&{1,2}|\|?\||\?|\*|\/|~|\^|%/,
|
||||
punctuation: /[{}[\];(),.:]/
|
||||
};
|
||||
|
||||
Prism.languages.javascript = Prism.languages.extend('clike', {
|
||||
'keyword': /\b(as|async|await|break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|export|extends|false|finally|for|from|function|get|if|implements|import|in|instanceof|interface|let|new|null|of|package|private|protected|public|return|set|static|super|switch|this|throw|true|try|typeof|var|void|while|with|yield)\b/,
|
||||
'number': /\b-?(0x[\dA-Fa-f]+|0b[01]+|0o[0-7]+|\d*\.?\d+([Ee][+-]?\d+)?|NaN|Infinity)\b/,
|
||||
'function': /(?!\d)[a-z0-9_$]+(?=\()/i,
|
||||
Prism.languages.javascript = Prism.languages.extend("clike", {
|
||||
keyword: /\b(as|async|await|break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|export|extends|false|finally|for|from|function|get|if|implements|import|in|instanceof|interface|let|new|null|of|package|private|protected|public|return|set|static|super|switch|this|throw|true|try|typeof|var|void|while|with|yield)\b/,
|
||||
number: /\b-?(0x[\dA-Fa-f]+|0b[01]+|0o[0-7]+|\d*\.?\d+([Ee][+-]?\d+)?|NaN|Infinity)\b/,
|
||||
function: /(?!\d)[a-z0-9_$]+(?=\()/i
|
||||
});
|
||||
|
||||
Prism.languages.insertBefore('javascript', 'keyword', {
|
||||
'regex': {
|
||||
Prism.languages.insertBefore("javascript", "keyword", {
|
||||
regex: {
|
||||
pattern: /(^|[^/])\/(?!\/)(\[.+?]|\\.|[^/\\\r\n])+\/[gimyu]{0,5}(?=\s*($|[\r\n,.;})]))/,
|
||||
lookbehind: true,
|
||||
},
|
||||
lookbehind: true
|
||||
}
|
||||
});
|
||||
|
||||
Prism.languages.insertBefore('javascript', 'class-name', {
|
||||
'template-string': {
|
||||
Prism.languages.insertBefore("javascript", "class-name", {
|
||||
"template-string": {
|
||||
pattern: /`(?:\\`|\\?[^`])*`/,
|
||||
inside: {
|
||||
'interpolation': {
|
||||
interpolation: {
|
||||
pattern: /\$\{[^}]+\}/,
|
||||
inside: {
|
||||
'interpolation-punctuation': {
|
||||
"interpolation-punctuation": {
|
||||
pattern: /^\$\{|\}$/,
|
||||
alias: 'punctuation',
|
||||
alias: "punctuation"
|
||||
},
|
||||
rest: Prism.languages.javascript,
|
||||
},
|
||||
rest: Prism.languages.javascript
|
||||
}
|
||||
},
|
||||
'string': /[\s\S]+/,
|
||||
},
|
||||
},
|
||||
string: /[\s\S]+/
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (Prism.languages.markup) {
|
||||
Prism.languages.insertBefore('markup', 'tag', {
|
||||
'script': {
|
||||
Prism.languages.insertBefore("markup", "tag", {
|
||||
script: {
|
||||
pattern: /<script[\w\W]*?>[\w\W]*?<\/script>/i,
|
||||
inside: {
|
||||
'tag': {
|
||||
tag: {
|
||||
pattern: /<script[\w\W]*?>|<\/script>/i,
|
||||
inside: Prism.languages.markup.tag.inside,
|
||||
inside: Prism.languages.markup.tag.inside
|
||||
},
|
||||
rest: Prism.languages.javascript,
|
||||
rest: Prism.languages.javascript
|
||||
},
|
||||
alias: 'language-javascript',
|
||||
},
|
||||
alias: "language-javascript"
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
(function(Prism) {
|
||||
|
||||
const javascript = Prism.util.clone(Prism.languages.javascript);
|
||||
|
||||
Prism.languages.jsx = Prism.languages.extend('markup', javascript);
|
||||
Prism.languages.jsx = Prism.languages.extend("markup", javascript);
|
||||
Prism.languages.jsx.tag.pattern = /<\/?[\w:-]+\s*(?:\s+[\w:-]+(?:=(?:("|')(\\?[\w\W])*?\1|[^\s'">=]+|(\{[\w\W]*?\})))?\s*)*\/?>/i;
|
||||
|
||||
Prism.languages.jsx.tag.inside['attr-value'].pattern = /=[^\{](?:('|")[\w\W]*?(\1)|[^\s>]+)/i;
|
||||
Prism.languages.jsx.tag.inside[
|
||||
"attr-value"
|
||||
].pattern = /=[^\{](?:('|")[\w\W]*?(\1)|[^\s>]+)/i;
|
||||
|
||||
Prism.languages.insertBefore('inside', 'attr-value', {
|
||||
'script': {
|
||||
pattern: /=(\{[\w\W]*?\})/i,
|
||||
inside: {
|
||||
'function' : Prism.languages.javascript.function,
|
||||
'punctuation': /[={}[\];(),.:]/,
|
||||
'keyword': Prism.languages.javascript.keyword,
|
||||
},
|
||||
'alias': 'language-javascript',
|
||||
Prism.languages.insertBefore(
|
||||
"inside",
|
||||
"attr-value",
|
||||
{
|
||||
script: {
|
||||
pattern: /=(\{[\w\W]*?\})/i,
|
||||
inside: {
|
||||
function: Prism.languages.javascript.function,
|
||||
punctuation: /[={}[\];(),.:]/,
|
||||
keyword: Prism.languages.javascript.keyword
|
||||
},
|
||||
alias: "language-javascript"
|
||||
}
|
||||
},
|
||||
}, Prism.languages.jsx.tag);
|
||||
|
||||
}(Prism));
|
||||
Prism.languages.jsx.tag
|
||||
);
|
||||
})(Prism);
|
||||
|
||||
const PrismComponent = React.createClass({
|
||||
statics: {
|
||||
_,
|
||||
_
|
||||
},
|
||||
getDefaultProps() {
|
||||
return {
|
||||
language: 'javascript',
|
||||
language: "javascript"
|
||||
};
|
||||
},
|
||||
render() {
|
||||
const lines = [];
|
||||
if (this.props.line) {
|
||||
this.props.line.split(',').forEach(range => {
|
||||
const parts = range.split('-');
|
||||
this.props.line.split(",").forEach(range => {
|
||||
const parts = range.split("-");
|
||||
if (parts.length === 1) {
|
||||
lines.push(parts[0].trim());
|
||||
} else {
|
||||
|
@ -550,20 +578,20 @@ const PrismComponent = React.createClass({
|
|||
}
|
||||
const grammar = _.languages[this.props.language];
|
||||
return (
|
||||
<pre className={'prism language-' + this.props.language}>
|
||||
<pre className={"prism language-" + this.props.language}>
|
||||
{Token.reactify(_.tokenize(this.props.children, grammar))}
|
||||
{lines.map((line, ii) => {
|
||||
return (
|
||||
<div
|
||||
className="line-highlight"
|
||||
key={ii}
|
||||
style={{height: 20, top: 20 * (line - 1)}}
|
||||
style={{ height: 20, top: 20 * (line - 1) }}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
</pre>
|
||||
);
|
||||
},
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = PrismComponent;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue