From 011f80159104496496ab9c7961056efea0c62ce0 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 27 Oct 2022 10:18:23 +0200 Subject: [PATCH] Add TypeScript syntax highlighting Closes #214 Add brush based on: https://github.com/syntaxhighlighter/brush-typescript/blob/07d5fa0a57527d2f11d601676ea68a6ab91d3e6d/brush.js --- syntaxhighlighter.php | 4 ++++ third-party-brushes/shBrushTs.js | 38 ++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 third-party-brushes/shBrushTs.js diff --git a/syntaxhighlighter.php b/syntaxhighlighter.php index a521482..d88b5ea 100644 --- a/syntaxhighlighter.php +++ b/syntaxhighlighter.php @@ -162,6 +162,7 @@ function_exists( 'parse_blocks' ) // WordPress 5.0+ wp_register_script( 'syntaxhighlighter-brush-matlabkey', plugins_url( 'third-party-brushes/shBrushMatlabKey.js', __FILE__ ), array( 'syntaxhighlighter-core' ), '20091209' ); wp_register_script( 'syntaxhighlighter-brush-objc', plugins_url( 'third-party-brushes/shBrushObjC.js', __FILE__ ), array( 'syntaxhighlighter-core' ), '20091207' ); wp_register_script( 'syntaxhighlighter-brush-r', plugins_url( 'third-party-brushes/shBrushR.js', __FILE__ ), array( 'syntaxhighlighter-core' ), '20100919' ); + wp_register_script( 'syntaxhighlighter-brush-ts', plugins_url( 'third-party-brushes/shBrushTs.js', __FILE__ ), array( 'syntaxhighlighter-core' ), '20160926' ); // Register theme stylesheets wp_register_style( 'syntaxhighlighter-core', plugins_url( $this->shfolder . '/styles/shCore.css', __FILE__ ), array(), $this->agshver ); @@ -231,6 +232,8 @@ function_exists( 'parse_blocks' ) // WordPress 5.0+ 'rb' => 'ruby', 'ror' => 'ruby', 'ruby' => 'ruby', + 'ts' => 'ts', + 'typescript' => 'ts', 'scala' => 'scala', 'sql' => 'sql', 'swift' => 'swift', @@ -277,6 +280,7 @@ function_exists( 'parse_blocks' ) // WordPress 5.0+ 'scala' => __( 'Scala', 'syntaxhighlighter' ), 'swift' => __( 'Swift', 'syntaxhighlighter' ), 'sql' => __( 'SQL', 'syntaxhighlighter' ), + 'ts' => __( 'TypeScript', 'syntaxhighlighter' ), 'vb' => __( 'Visual Basic', 'syntaxhighlighter' ), 'xml' => __( 'HTML / XHTML / XML / XSLT', 'syntaxhighlighter' ), 'yaml' => __( 'YAML', 'syntaxhighlighter' ), diff --git a/third-party-brushes/shBrushTs.js b/third-party-brushes/shBrushTs.js new file mode 100644 index 0000000..3afff5a --- /dev/null +++ b/third-party-brushes/shBrushTs.js @@ -0,0 +1,38 @@ +SyntaxHighlighter.brushes.ts = function () { + const keywords = + "break case catch class continue " + + "default delete do else enum export extends false " + + "for function if implements import in instanceof " + + "interface let new null package private protected " + + "static return super switch " + + "this throw true try typeof var while with yield" + + " any bool declare get module never number public readonly set string"; // TypeScript-specific, everything above is common with JavaScript + + this.regexList = [ + { + regex: SyntaxHighlighter.regexLib.multiLineDoubleQuotedString, + css: "string", + }, + { + regex: SyntaxHighlighter.regexLib.multiLineSingleQuotedString, + css: "string", + }, + { + regex: SyntaxHighlighter.regexLib.singleLineCComments, + css: "comments", + }, + { + regex: SyntaxHighlighter.regexLib.multiLineCComments, + css: "comments", + }, + { + regex: new RegExp(this.getKeywords(keywords), "gm"), + css: "keyword", + }, + ]; + + this.forHtmlScript(SyntaxHighlighter.regexLib.scriptScriptTags); +}; + +SyntaxHighlighter.brushes.ts.prototype = new SyntaxHighlighter.Highlighter(); +SyntaxHighlighter.brushes.ts.aliases = ["ts", "typescript"];