Javascript code highlighting-wangHighLighter. js, javascript and js
1. Introduction
(First post the github address of wangHighLighter. js: Ghost)
Some may have followed my wangEditor-a bootstrap-based Rich Text Editor. Creating a rich text editor doesn't really mean much. There are some editor plug-ins on the Internet. However, Rich Text editors with code insertion and code highlighting are rare. This is one of them.
Second, the Code highlighting tool on the Internet (such as SyntaxHighlighter) usually needs to reference js and css to the page. static text is already present on the page, so that the highlighted effect is displayed in combination. However, this situation cannot be applied to the rich text editor, because once the edited content of the Rich Text Editor is saved, it cannot be determined in which environment to load the display, how can we expect all pages to reference third-party highlight plug-ins? Therefore, we need to perform one-step conversion:
You do not know what it means. In fact, we need to use the code edited from the rich text box to get rid of it everywhere, rather than relying on third-party plug-ins (such as SyntaxHighlighter)
So I developed a plug-in for wangEditor for code highlighting -- wangHighLighter --: https://github.com/wangfupeng1988/wangHighLighter
2. Use wangHighLighter. js
WangHighLighter. js currently supports more than 20 common languages and provides 7 common theme styles for you to choose from.
The application is also very simple, and only three steps are required:
1. Reference wangHighLighter. js
Reference wangHighLighter. js on the page
<script src="js/wangHighLighter-1.0.0-min.js" type="text/javascript"></script>
Second, bind the language and topic
Bind the languages and themes supported by wangHighLighter. js to the drop-down list for users to choose from, such:
The wangHighLighter. getLangArray () method can be used to obtain arrays of all languages. The wangHighLighter. getThemeArray () method can be used to obtain arrays of all themes. Therefore, you can use the following code to bind the language and topic:
Language: <select id = "sltLang"> </select> topic: <select id = "sltTheme"> </select> <script type = "text/javascript" >$ (function () {var $ sltLang = $ ('# sltLang '), langArray = wangHighLighter. getLangArray (), $ sltTheme = $ ('# sltTheme'), themeArray = wangHighLighter. getThemeArray (), item, for (item in langArray) {$ sltLang. append ($ ('<option>' + langArray [item] + '</option>');} for (item in themeArray) {$ sltTheme. append ($ ('<option>' + themeArray [item] + '</option>') ;}}); </script>Third, Application
You can use the wangHighLighter. highLight (lang, theme, code) method to highLight the code and return it. You only need to input the language, topic, and prepare the highlighted string and execute one step to solve the problem. Is it very simple?
$ Btn1.click (function () {var code = $ txt1.val (), // obtain the original string lang in textarea = $ sltLang. val (), // obtain the language theme = $ sltTheme. val (), // obtain the topic highLightCode; highLightCode = wangHighLighter. highLight (lang, theme, code); // The highlighted code nvidiv1.html (highLightCode );});
The implementation result of this Code is as follows:
3. Application in wangEditor
WangEditorIt is a bootstrap-based Rich Text Editor. Now wangHighLighter. js has been used to implement the "insert code" function for wangEditor! Make wangEditor a rich text editor with rare Open Source Plug-in code on the Internet.
4. Upgrade & Update
WangHighLighter. js is currently in version 1.0.0, and some bugs and defects need to be fixed. I will update and improve it in my spare time. It is a powerful helper for the rich text editor to insert code!
In addition, if you have time, I will write an article to describe the source code design of wangHighLighter. js, including the program structure and the application of regular expressions.
QQ Group 164999061, welcome to join the discussion!
Bytes -------------------------------------------------------------------------------------------------------------
Welcome to my Weibo.
You are also welcome to follow my tutorial:
From design to Model《Deep understanding of javascript prototype and closure SeriesMicrosoft petshop4.0 source code explanation video json2.js source code interpretation video
Bytes -------------------------------------------------------------------------------------------------------------