Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor(es2015) #97

Merged
merged 5 commits into from May 25, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)) {
yoshinorin marked this conversation as resolved.
Show resolved Hide resolved
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