1
1
'use strict' ;
2
2
3
- var marked = require ( 'marked' ) ;
4
- var stripIndent = require ( 'strip-indent' ) ;
5
- var util = require ( 'hexo-util' ) ;
6
-
7
- var highlight = util . highlight ;
8
- var stripHTML = util . stripHTML ;
9
- var MarkedRenderer = marked . Renderer ;
3
+ const marked = require ( 'marked' ) ;
4
+ const stripIndent = require ( 'strip-indent' ) ;
5
+ const { stripHTML, highlight, slugize } = require ( 'hexo-util' ) ;
6
+ const MarkedRenderer = marked . Renderer ;
10
7
11
8
function Renderer ( ) {
12
9
MarkedRenderer . apply ( this ) ;
@@ -18,29 +15,29 @@ require('util').inherits(Renderer, MarkedRenderer);
18
15
19
16
// Add id attribute to headings
20
17
Renderer . prototype . heading = function ( text , level ) {
21
- var transformOption = this . options . modifyAnchors ;
22
- var id = anchorId ( stripHTML ( text ) , transformOption ) ;
23
- var headingId = this . _headingId ;
18
+ const transformOption = this . options . modifyAnchors ;
19
+ let id = anchorId ( stripHTML ( text ) , transformOption ) ;
20
+ const headingId = this . _headingId ;
24
21
25
22
// Add a number after id if repeated
26
23
if ( headingId [ id ] ) {
27
- id += '-' + headingId [ id ] ++ ;
24
+ id += `- ${ headingId [ id ] ++ } ` ;
28
25
} else {
29
26
headingId [ id ] = 1 ;
30
27
}
31
28
// add headerlink
32
- return '<h' + level + ' id="' + id + ' "><a href="#' + id + ' " class="headerlink" title="' + stripHTML ( text ) + ' "></a>' + text + ' </h' + level + '>' ;
29
+ return `<h ${ level } id="${ id } "><a href="#${ id } " class="headerlink" title="${ stripHTML ( text ) } "></a>${ text } </h${ level } >` ;
33
30
} ;
34
31
35
32
function anchorId ( str , transformOption ) {
36
- return util . slugize ( str . trim ( ) , { transform : transformOption } ) ;
33
+ return slugize ( str . trim ( ) , { transform : transformOption } ) ;
37
34
}
38
35
39
36
// Support AutoLink option
40
37
Renderer . prototype . link = function ( href , title , text ) {
41
- var prot ;
42
-
43
38
if ( this . options . sanitize ) {
39
+ let prot ;
40
+
44
41
try {
45
42
prot = decodeURIComponent ( unescape ( href ) )
46
43
. replace ( / [ ^ \w : ] / g, '' )
@@ -49,7 +46,7 @@ Renderer.prototype.link = function(href, title, text) {
49
46
return '' ;
50
47
}
51
48
52
- if ( prot . indexOf ( 'javascript:' ) === 0 || prot . indexOf ( 'vbscript:' ) === 0 || prot . indexOf ( 'data:' ) === 0 ) {
49
+ if ( prot . startsWith ( 'javascript:' ) || prot . startsWith ( 'vbscript:' ) || prot . startsWith ( 'data:' ) ) {
53
50
return '' ;
54
51
}
55
52
}
@@ -58,41 +55,34 @@ Renderer.prototype.link = function(href, title, text) {
58
55
return href ;
59
56
}
60
57
61
- var out = ' <a href="' + href + '"' ;
58
+ let out = ` <a href="${ href } "` ;
62
59
63
60
if ( title ) {
64
- out += ' title="' + title + '"' ;
61
+ out += ` title="${ title } "` ;
65
62
}
66
63
67
- out += '>' + text + ' </a>' ;
64
+ out += `> ${ text } </a>` ;
68
65
return out ;
69
66
} ;
70
67
71
68
// Support Basic Description Lists
72
- Renderer . prototype . paragraph = function ( text ) {
73
- var result = '' ;
74
- var dlTest = / ( ^ | \s ) ( \S .+ ) ( < b r > : ( \s + ) ) ( \S .+ ) / ;
75
-
76
- var dl
77
- = '<dl>'
78
- + '<dt>$2</dt>'
79
- + '<dd>$5</dd>'
80
- + '</dl>' ;
81
-
82
- if ( text . match ( dlTest ) ) {
83
- result = text . replace ( dlTest , dl ) ;
84
- } else {
85
- result = '<p>' + text + '</p>\n' ;
69
+ Renderer . prototype . paragraph = text => {
70
+ const dlTest = / (?: ^ | \s ) ( \S .+ ) < b r > : \s + ( \S .+ ) / ;
71
+
72
+ const dl = '<dl><dt>$1</dt><dd>$2</dd></dl>' ;
73
+
74
+ if ( dlTest . test ( text ) ) {
75
+ return text . replace ( dlTest , dl ) ;
86
76
}
87
77
88
- return result ;
78
+ return `<p> ${ text } </p>\n` ;
89
79
} ;
90
80
91
81
marked . setOptions ( {
92
82
langPrefix : '' ,
93
- highlight : function ( code , lang ) {
83
+ highlight ( code , lang ) {
94
84
return highlight ( stripIndent ( code ) , {
95
- lang : lang ,
85
+ lang,
96
86
gutter : false ,
97
87
wrap : false
98
88
} ) ;
0 commit comments