Skip to content

Commit 27eb9c1

Browse files
segayuuyoshinorin
authored andcommitted
Refactor(es2015 & RegExp) (#97)
* Auto Refactoring from lebab * Remove no use Groups * Remove namespace object * indexOf to startsWith * Reduce prot scope
1 parent 944fa64 commit 27eb9c1

File tree

1 file changed

+26
-36
lines changed

1 file changed

+26
-36
lines changed

lib/renderer.js

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
'use strict';
22

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;
107

118
function Renderer() {
129
MarkedRenderer.apply(this);
@@ -18,29 +15,29 @@ require('util').inherits(Renderer, MarkedRenderer);
1815

1916
// Add id attribute to headings
2017
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;
2421

2522
// Add a number after id if repeated
2623
if (headingId[id]) {
27-
id += '-' + headingId[id]++;
24+
id += `-${headingId[id]++}`;
2825
} else {
2926
headingId[id] = 1;
3027
}
3128
// 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}>`;
3330
};
3431

3532
function anchorId(str, transformOption) {
36-
return util.slugize(str.trim(), {transform: transformOption});
33+
return slugize(str.trim(), {transform: transformOption});
3734
}
3835

3936
// Support AutoLink option
4037
Renderer.prototype.link = function(href, title, text) {
41-
var prot;
42-
4338
if (this.options.sanitize) {
39+
let prot;
40+
4441
try {
4542
prot = decodeURIComponent(unescape(href))
4643
.replace(/[^\w:]/g, '')
@@ -49,7 +46,7 @@ Renderer.prototype.link = function(href, title, text) {
4946
return '';
5047
}
5148

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:')) {
5350
return '';
5451
}
5552
}
@@ -58,41 +55,34 @@ Renderer.prototype.link = function(href, title, text) {
5855
return href;
5956
}
6057

61-
var out = '<a href="' + href + '"';
58+
let out = `<a href="${href}"`;
6259

6360
if (title) {
64-
out += ' title="' + title + '"';
61+
out += ` title="${title}"`;
6562
}
6663

67-
out += '>' + text + '</a>';
64+
out += `>${text}</a>`;
6865
return out;
6966
};
7067

7168
// Support Basic Description Lists
72-
Renderer.prototype.paragraph = function(text) {
73-
var result = '';
74-
var dlTest = /(^|\s)(\S.+)(<br>:(\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.+)<br>:\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);
8676
}
8777

88-
return result;
78+
return `<p>${text}</p>\n`;
8979
};
9080

9181
marked.setOptions({
9282
langPrefix: '',
93-
highlight: function(code, lang) {
83+
highlight(code, lang) {
9484
return highlight(stripIndent(code), {
95-
lang: lang,
85+
lang,
9686
gutter: false,
9787
wrap: false
9888
});

0 commit comments

Comments
 (0)