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

fix: autolink option should not apply on markdown syntax #162

Merged
merged 1 commit into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 54 additions & 6 deletions lib/renderer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
'use strict';

const marked = require('marked');
const { escape } = require('marked/src/helpers');
const { encodeURL, slugize, stripHTML, url_for, isExternalLink } = require('hexo-util');
const MarkedRenderer = marked.Renderer;
const MarkedTokenizer = marked.Tokenizer;

const anchorId = (str, transformOption) => {
return slugize(str.trim(), {transform: transformOption});
Expand Down Expand Up @@ -39,19 +41,15 @@ class Renderer extends MarkedRenderer {
return `<h${level} id="${id}"><a href="#${id}" class="headerlink" title="${stripHTML(text)}"></a>${text}</h${level}>`;
}

// Support AutoLink option
link(href, title, text) {
const { autolink, external_link, sanitizeUrl } = this.options;
const { external_link, sanitizeUrl } = this.options;
const { url: urlCfg } = this.hexo.config;

if (sanitizeUrl) {
if (href.startsWith('javascript:') || href.startsWith('vbscript:') || href.startsWith('data:')) {
href = '';
}
}
if (!autolink && href === text && title == null) {
return href;
}

let out = `<a href="${encodeURL(href)}"`;
if (title) {
Expand Down Expand Up @@ -117,6 +115,53 @@ marked.setOptions({
langPrefix: ''
});

class Tokenizer extends MarkedTokenizer {
// Support AutoLink option
// https://github.com/markedjs/marked/blob/b6773fca412c339e0cedd56b63f9fa1583cfd372/src/Tokenizer.js#L606-L641
url(src, mangle) {
const { options, rules } = this;
const { mangle: isMangle, autolink } = options;

if (!autolink) return;

const cap = rules.inline.url.exec(src);
if (cap) {
let text, href;
if (cap[2] === '@') {
text = escape(isMangle ? mangle(cap[0]) : cap[0]);
href = 'mailto:' + text;
} else {
// do extended autolink path validation
let prevCapZero;
do {
prevCapZero = cap[0];
cap[0] = rules.inline._backpedal.exec(cap[0])[0];
} while (prevCapZero !== cap[0]);
text = escape(cap[0]);
if (cap[1] === 'www.') {
href = 'http://' + text;
} else {
href = text;
}
}

return {
type: 'link',
raw: cap[0],
text,
href,
tokens: [
{
type: 'text',
raw: text,
text
}
]
};
}
}
}

module.exports = function(data, options) {
const { post_asset_folder, marked: markedCfg, source_dir } = this.config;
const { prependRoot, postAsset } = markedCfg;
Expand All @@ -126,6 +171,8 @@ module.exports = function(data, options) {
const renderer = new Renderer(this);
this.execFilterSync('marked:renderer', renderer, {context: this});

const tokenizer = new Tokenizer();

let postPath = '';
if (path && post_asset_folder && prependRoot && postAsset) {
const Post = this.model('Post');
Expand All @@ -136,6 +183,7 @@ module.exports = function(data, options) {
}

return marked(text, Object.assign({
renderer
renderer,
tokenizer
}, markedCfg, options, { postPath }));
};
26 changes: 19 additions & 7 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,26 +165,38 @@ describe('Marked renderer', () => {
const body = [
'Great website http://hexo.io',
'',
'[Hexo](http://hexo.io)'
'A webpage www.example.com',
'',
'[Hexo](http://hexo.io)',
'',
'[http://lorem.com/foo/](http://lorem.com/foo/)',
'',
'<http://dolor.com>'
].join('\n');

it('autolink enabled', () => {
const result = r({text: body});

result.should.eql([
'<p>Great website <a href="http://hexo.io/">http://hexo.io</a></p>\n',
'<p><a href="http://hexo.io/">Hexo</a></p>\n'
].join(''));
'<p>Great website <a href="http://hexo.io/">http://hexo.io</a></p>',
'<p>A webpage <a href="http://www.example.com/">www.example.com</a></p>',
'<p><a href="http://hexo.io/">Hexo</a></p>',
'<p><a href="http://lorem.com/foo/">http://lorem.com/foo/</a></p>',
'<p><a href="http://dolor.com/">http://dolor.com</a></p>'
].join('\n') + '\n');
});

it('autolink disabled', () => {
ctx.config.marked.autolink = false;
const result = r({text: body});

result.should.eql([
'<p>Great website http://hexo.io</p>\n',
'<p><a href="http://hexo.io/">Hexo</a></p>\n'
].join(''));
'<p>Great website http://hexo.io</p>',
'<p>A webpage www.example.com</p>',
'<p><a href="http://hexo.io/">Hexo</a></p>',
'<p><a href="http://lorem.com/foo/">http://lorem.com/foo/</a></p>',
'<p><a href="http://dolor.com/">http://dolor.com</a></p>'
].join('\n') + '\n');
});
});

Expand Down