Skip to content

Commit

Permalink
added tsx support (#1280)
Browse files Browse the repository at this point in the history
* added tsx support

* fixed order in components.js and added tsx example file

* escape less than and capitalize tsx
  • Loading branch information
fyrkant authored and Golmote committed Feb 5, 2018
1 parent 5de1b1f commit fbe82b8
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 4 deletions.
4 changes: 4 additions & 0 deletions components.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,10 @@ var components = {
"require": ["markup", "javascript"],
"owner": "vkbansal"
},
"tsx": {
"title": "React TSX",
"require": ["jsx", "typescript"]
},
"renpy": {
"title": "Ren'py",
"owner": "HyuchiaDiego"
Expand Down
2 changes: 2 additions & 0 deletions components/prism-tsx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var typescript = Prism.util.clone(Prism.languages.typescript);
Prism.languages.tsx = Prism.languages.extend('jsx', typescript);
1 change: 1 addition & 0 deletions components/prism-tsx.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions examples/prism-tsx.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<h1>React TSX</h1>
<p>To use this language, use the class "language-tsx".</p>

<h2>Full example</h2>
<pre><code>import * as React from 'react';

interface IState {
clicks: number;
}

export class Clicker extends React.Component&lt;any, IState> {
constructor(props) {
super(props);

this.state = {
clicks: 0,
};
}

public clickHandler = () => {
this.setState({ clicks: this.state.clicks + 1 });
}

public render() {
return (
&lt;div>
&lt;p>You have clicked the button {this.state.clicks} time(s).&lt;/p>
&lt;p>
&lt;button onClick={this.clickHandler}>click me&lt;/button>
&lt;/p>
&lt;/div>
);
}
}</code></pre>
2 changes: 1 addition & 1 deletion plugins/autoloader/prism-autoloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
}

// The dependencies map is built automatically with gulp
var lang_dependencies = /*languages_placeholder[*/{"javascript":"clike","actionscript":"javascript","arduino":"cpp","aspnet":"markup","bison":"c","c":"clike","csharp":"clike","cpp":"c","coffeescript":"javascript","crystal":"ruby","css-extras":"css","d":"clike","dart":"clike","django":"markup","fsharp":"clike","flow":"javascript","glsl":"clike","go":"clike","groovy":"clike","haml":"ruby","handlebars":"markup","haxe":"clike","java":"clike","jolie":"clike","kotlin":"clike","less":"css","markdown":"markup","n4js":"javascript","nginx":"clike","objectivec":"c","opencl":"cpp","parser":"markup","php":"clike","php-extras":"php","processing":"clike","protobuf":"clike","pug":"javascript","qore":"clike","jsx":["markup","javascript"],"reason":"clike","ruby":"clike","sass":"css","scss":"css","scala":"java","smarty":"markup","swift":"clike","textile":"markup","twig":"markup","typescript":"javascript","vbnet":"basic","wiki":"markup","xeora":"markup"}/*]*/;
var lang_dependencies = /*languages_placeholder[*/{"javascript":"clike","actionscript":"javascript","arduino":"cpp","aspnet":"markup","bison":"c","c":"clike","csharp":"clike","cpp":"c","coffeescript":"javascript","crystal":"ruby","css-extras":"css","d":"clike","dart":"clike","django":"markup","fsharp":"clike","flow":"javascript","glsl":"clike","go":"clike","groovy":"clike","haml":"ruby","handlebars":"markup","haxe":"clike","java":"clike","jolie":"clike","kotlin":"clike","less":"css","markdown":"markup","n4js":"javascript","nginx":"clike","objectivec":"c","opencl":"cpp","parser":"markup","php":"clike","php-extras":"php","processing":"clike","protobuf":"clike","pug":"javascript","qore":"clike","jsx":["markup","javascript"],"reason":"clike","ruby":"clike","sass":"css","scss":"css","scala":"java","smarty":"markup","swift":"clike","textile":"markup","tsx":["jsx","typescript"],"twig":"markup","typescript":"javascript","vbnet":"basic","wiki":"markup","xeora":"markup"}/*]*/;

var lang_data = {};

Expand Down
2 changes: 1 addition & 1 deletion plugins/autoloader/prism-autoloader.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugins/show-language/prism-show-language.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ if (!Prism.plugins.toolbar) {
}

// The languages map is built automatically with gulp
var Languages = /*languages_placeholder[*/{"html":"HTML","xml":"XML","svg":"SVG","mathml":"MathML","css":"CSS","clike":"C-like","javascript":"JavaScript","abap":"ABAP","actionscript":"ActionScript","apacheconf":"Apache Configuration","apl":"APL","applescript":"AppleScript","asciidoc":"AsciiDoc","asm6502":"6502 Assembly","aspnet":"ASP.NET (C#)","autohotkey":"AutoHotkey","autoit":"AutoIt","basic":"BASIC","csharp":"C#","cpp":"C++","coffeescript":"CoffeeScript","csp":"Content-Security-Policy","css-extras":"CSS Extras","django":"Django/Jinja2","fsharp":"F#","glsl":"GLSL","graphql":"GraphQL","http":"HTTP","hpkp":"HTTP Public-Key-Pins","hsts":"HTTP Strict-Transport-Security","ichigojam":"IchigoJam","inform7":"Inform 7","json":"JSON","latex":"LaTeX","livescript":"LiveScript","lolcode":"LOLCODE","matlab":"MATLAB","mel":"MEL","n4js":"N4JS","nasm":"NASM","nginx":"nginx","nsis":"NSIS","objectivec":"Objective-C","ocaml":"OCaml","opencl":"OpenCL","parigp":"PARI/GP","php":"PHP","php-extras":"PHP Extras","powershell":"PowerShell","properties":".properties","protobuf":"Protocol Buffers","jsx":"React JSX","renpy":"Ren'py","rest":"reST (reStructuredText)","sas":"SAS","sass":"Sass (Sass)","scss":"Sass (Scss)","sql":"SQL","typescript":"TypeScript","vbnet":"VB.Net","vhdl":"VHDL","vim":"vim","wiki":"Wiki markup","xojo":"Xojo (REALbasic)","yaml":"YAML"}/*]*/;
var Languages = /*languages_placeholder[*/{"html":"HTML","xml":"XML","svg":"SVG","mathml":"MathML","css":"CSS","clike":"C-like","javascript":"JavaScript","abap":"ABAP","actionscript":"ActionScript","apacheconf":"Apache Configuration","apl":"APL","applescript":"AppleScript","asciidoc":"AsciiDoc","asm6502":"6502 Assembly","aspnet":"ASP.NET (C#)","autohotkey":"AutoHotkey","autoit":"AutoIt","basic":"BASIC","csharp":"C#","cpp":"C++","coffeescript":"CoffeeScript","csp":"Content-Security-Policy","css-extras":"CSS Extras","django":"Django/Jinja2","fsharp":"F#","glsl":"GLSL","graphql":"GraphQL","http":"HTTP","hpkp":"HTTP Public-Key-Pins","hsts":"HTTP Strict-Transport-Security","ichigojam":"IchigoJam","inform7":"Inform 7","json":"JSON","latex":"LaTeX","livescript":"LiveScript","lolcode":"LOLCODE","matlab":"MATLAB","mel":"MEL","n4js":"N4JS","nasm":"NASM","nginx":"nginx","nsis":"NSIS","objectivec":"Objective-C","ocaml":"OCaml","opencl":"OpenCL","parigp":"PARI/GP","php":"PHP","php-extras":"PHP Extras","powershell":"PowerShell","properties":".properties","protobuf":"Protocol Buffers","jsx":"React JSX","renpy":"Ren'py","rest":"reST (reStructuredText)","sas":"SAS","sass":"Sass (Sass)","scss":"Sass (Scss)","sql":"SQL","tsx":"React Tsx","typescript":"TypeScript","vbnet":"VB.Net","vhdl":"VHDL","vim":"vim","wiki":"Wiki markup","xojo":"Xojo (REALbasic)","yaml":"YAML"}/*]*/;
Prism.plugins.toolbar.registerButton('show-language', function(env) {
var pre = env.element.parentNode;
if (!pre || !/pre/i.test(pre.nodeName)) {
Expand Down
2 changes: 1 addition & 1 deletion plugins/show-language/prism-show-language.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

106 changes: 106 additions & 0 deletions tests/languages/tsx/tag_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
var myDivElement = <div className="foo" />;
var myElement = <MyComponent someProperty={true} />;
<div {...foo}>
class Test extends Component {
render() {
return <p onClick={this.clickHandler}>Hello world.</p>;
}
}

----------------------------------------------------

[
["keyword", "var"],
" myDivElement ",
["operator", "="],
["tag", [
["tag", [
["punctuation", "<"],
"div"
]],
["attr-name", ["className"]],
["attr-value", [
["punctuation", "="],
["punctuation", "\""],
"foo",
["punctuation", "\""]
]],
["punctuation", "/>"]
]],
["punctuation", ";"],

["keyword", "var"],
" myElement ",
["operator", "="],
["tag", [
["tag", [
["punctuation", "<"],
"MyComponent"
]],
["attr-name", ["someProperty"]],
["script", [
["punctuation", "="],
["punctuation", "{"],
["boolean", "true"],
["punctuation", "}"]
]],
["punctuation", "/>"]
]],
["punctuation", ";"],
["tag", [
["tag", [
["punctuation", "<"],
"div"
]],
["spread", [
["punctuation", "{"],
["punctuation", "..."],
["attr-value", "foo"],
["punctuation", "}"]
]],
["punctuation", ">"]
]],

["keyword", "class"],
["class-name", ["Test"]],
["keyword", "extends"],
["class-name", ["Component"]],
["punctuation", "{"],
["function", "render"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "{"],
["keyword","return"],
["tag", [
["tag", [
["punctuation", "<"],
"p"
]],
["attr-name", ["onClick"]],
["script", [
["punctuation", "="],
["punctuation", "{"],
["keyword", "this"],
["punctuation", "."],
"clickHandler",
["punctuation", "}"]
]],
["punctuation", ">"]
]],
"Hello world",
["punctuation", "."],
["tag", [
["tag", [
["punctuation", "</"],
"p"
]],
["punctuation", ">"]
]],
["punctuation", ";"],
["punctuation", "}"],
["punctuation","}"]
]

----------------------------------------------------

Checks for TSX tags.

0 comments on commit fbe82b8

Please sign in to comment.