Skip to content

Commit

Permalink
Refactor(es2015 & RegExp) (#97)
Browse files Browse the repository at this point in the history
* Auto Refactoring from lebab
* Remove no use Groups
* Remove namespace object
* indexOf to startsWith
* Reduce prot scope
  • Loading branch information
segayuu authored and yoshinorin committed May 25, 2019
1 parent 944fa64 commit 27eb9c1
Showing 1 changed file with 26 additions and 36 deletions.
62 changes: 26 additions & 36 deletions lib/renderer.js
@@ -1,12 +1,9 @@
'use strict';

var marked = require('marked');
var stripIndent = require('strip-indent');
var util = require('hexo-util');

var highlight = util.highlight;
var stripHTML = util.stripHTML;
var MarkedRenderer = marked.Renderer;
const marked = require('marked');
const stripIndent = require('strip-indent');
const { stripHTML, highlight, slugize } = require('hexo-util');
const MarkedRenderer = marked.Renderer;

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

// Add id attribute to headings
Renderer.prototype.heading = function(text, level) {
var transformOption = this.options.modifyAnchors;
var id = anchorId(stripHTML(text), transformOption);
var headingId = this._headingId;
const transformOption = this.options.modifyAnchors;
let id = anchorId(stripHTML(text), transformOption);
const headingId = this._headingId;

// Add a number after id if repeated
if (headingId[id]) {
id += '-' + headingId[id]++;
id += `-${headingId[id]++}`;
} else {
headingId[id] = 1;
}
// add headerlink
return '<h' + level + ' id="' + id + '"><a href="#' + id + '" class="headerlink" title="' + stripHTML(text) + '"></a>' + text + '</h' + level + '>';
return `<h${level} id="${id}"><a href="#${id}" class="headerlink" title="${stripHTML(text)}"></a>${text}</h${level}>`;
};

function anchorId(str, transformOption) {
return util.slugize(str.trim(), {transform: transformOption});
return slugize(str.trim(), {transform: transformOption});
}

// Support AutoLink option
Renderer.prototype.link = function(href, title, text) {
var prot;

if (this.options.sanitize) {
let prot;

try {
prot = decodeURIComponent(unescape(href))
.replace(/[^\w:]/g, '')
Expand All @@ -49,7 +46,7 @@ Renderer.prototype.link = function(href, title, text) {
return '';
}

if (prot.indexOf('javascript:') === 0 || prot.indexOf('vbscript:') === 0 || prot.indexOf('data:') === 0) {
if (prot.startsWith('javascript:') || prot.startsWith('vbscript:') || prot.startsWith('data:')) {
return '';
}
}
Expand All @@ -58,41 +55,34 @@ Renderer.prototype.link = function(href, title, text) {
return href;
}

var out = '<a href="' + href + '"';
let out = `<a href="${href}"`;

if (title) {
out += ' title="' + title + '"';
out += ` title="${title}"`;
}

out += '>' + text + '</a>';
out += `>${text}</a>`;
return out;
};

// Support Basic Description Lists
Renderer.prototype.paragraph = function(text) {
var result = '';
var dlTest = /(^|\s)(\S.+)(<br>:(\s+))(\S.+)/;

var dl
= '<dl>'
+ '<dt>$2</dt>'
+ '<dd>$5</dd>'
+ '</dl>';

if (text.match(dlTest)) {
result = text.replace(dlTest, dl);
} else {
result = '<p>' + text + '</p>\n';
Renderer.prototype.paragraph = text => {
const dlTest = /(?:^|\s)(\S.+)<br>:\s+(\S.+)/;

const dl = '<dl><dt>$1</dt><dd>$2</dd></dl>';

if (dlTest.test(text)) {
return text.replace(dlTest, dl);
}

return result;
return `<p>${text}</p>\n`;
};

marked.setOptions({
langPrefix: '',
highlight: function(code, lang) {
highlight(code, lang) {
return highlight(stripIndent(code), {
lang: lang,
lang,
gutter: false,
wrap: false
});
Expand Down

0 comments on commit 27eb9c1

Please sign in to comment.