1
- ; ( function ( root , factory ) {
2
- if ( typeof exports === 'object' ) {
3
- module . exports = factory ( )
4
- } else {
5
- root . markdownitHightlightjs = factory ( )
6
- }
7
- } ) ( this , function ( ) {
1
+ /**
2
+ * Preset to use highlight.js with markdown-it.
3
+ *
4
+ * Original Copy: https://github.com/valeriangalliat/markdown-it-highlightjs/tree/v3.0.0/index.js
5
+ * The above link gives a 404. Here is my forked copy and Babel-converted ES5 version:
6
+ * (ES6) https://github.com/cslzchen/markdown-it-highlightjs/blob/release/3.0.0/index.js
7
+ * (ES5) https://github.com/cslzchen/markdown-it-highlightjs/blob/release/3.0.0/index.es5.js
8
+ * Version: https://github.com/valeriangalliat/markdown-it-highlightjs/releases/tag/v3.0.0
9
+ * Changes: 1. Use https://babeljs.io/repl/ to convert ES6 to ES5, 2. Use `window` instead of `require`
10
+ */
8
11
9
- const maybe = f => {
10
- try {
11
- return f ( )
12
- } catch ( e ) {
13
- return false
14
- }
15
- }
16
-
17
- // Highlight with given language.
18
- const highlight = ( code , lang ) =>
19
- maybe ( ( ) => hljs . highlight ( lang , code , true ) . value ) || ''
20
-
21
- // Highlight with given language or automatically.
22
- const highlightAuto = ( code , lang ) =>
23
- lang
24
- ? highlight ( code , lang )
25
- : maybe ( ( ) => hljs . highlightAuto ( code ) . value ) || ''
26
-
27
- // Wrap a render function to add `hljs` class to code blocks.
28
- const wrap = render =>
29
- function ( ...args ) {
30
- return render . apply ( this , args )
31
- . replace ( '<code class="' , '<code class="hljs ' )
32
- . replace ( '<code>' , '<code class="hljs">' )
33
- }
34
- var defaults = {
35
- auto : true ,
36
- code : true
37
- }
38
-
39
- return function ( md , opts ) {
40
- opts = Object . assign ( { } , defaults , opts )
41
-
42
- md . options . highlight = opts . auto ? highlightAuto : highlight
43
- md . renderer . rules . fence = wrap ( md . renderer . rules . fence )
44
-
45
- if ( opts . code ) {
46
- md . renderer . rules . code_block = wrap ( md . renderer . rules . code_block )
12
+ ( function ( root , factory ) {
13
+ if ( typeof exports === "object" ) {
14
+ module . exports = factory ( ) ;
15
+ } else {
16
+ root . markdownitHightlightjs = factory ( ) ;
47
17
}
48
- }
18
+ } ) ( this , function ( ) {
19
+
20
+ "use strict" ;
21
+
22
+ var maybe = function maybe ( f ) {
23
+ try {
24
+ return f ( ) ;
25
+ } catch ( e ) {
26
+ return false ;
27
+ }
28
+ } ;
29
+
30
+ // Highlight with given language.
31
+ var highlight = function highlight ( code , lang ) {
32
+ return maybe ( function ( ) {
33
+ return hljs . highlight ( lang , code , true ) . value ;
34
+ } ) || "" ;
35
+ } ;
36
+
37
+ // Highlight with given language or automatically.
38
+ var highlightAuto = function highlightAuto ( code , lang ) {
39
+ return lang ? highlight ( code , lang ) : maybe ( function ( ) {
40
+ return hljs . highlightAuto ( code ) . value ;
41
+ } ) || "" ;
42
+ } ;
43
+
44
+ // Wrap a render function to add `hljs` class to code blocks.
45
+ var wrap = function wrap ( render ) {
46
+ return function ( ) {
47
+ for ( var _len = arguments . length , args = Array ( _len ) , _key = 0 ; _key < _len ; _key ++ ) {
48
+ args [ _key ] = arguments [ _key ] ;
49
+ }
50
+
51
+ return render . apply ( this , args ) . replace ( "<code class=\"" , "<code class=\"hljs " ) . replace ( "<code>" , "<code class=\"hljs\">" ) ;
52
+ } ;
53
+ } ;
54
+
55
+ var defaults = {
56
+ auto : true ,
57
+ code : true
58
+ } ;
59
+
60
+ return function ( md , opts ) {
61
+
62
+ opts = Object . assign ( { } , defaults , opts ) ;
63
+
64
+ md . options . highlight = opts . auto ? highlightAuto : highlight ;
65
+ md . renderer . rules . fence = wrap ( md . renderer . rules . fence ) ;
66
+
67
+ if ( opts . code ) {
68
+ md . renderer . rules . code_block = wrap ( md . renderer . rules . code_block ) ;
69
+ }
70
+ } ;
49
71
50
- } )
72
+ } ) ;
0 commit comments