Skip to content

Commit

Permalink
Merge pull request #164 from curbengh/mangle
Browse files Browse the repository at this point in the history
feat: mangle option
  • Loading branch information
curbengh committed Sep 3, 2020
2 parents 37009ed + ed13a28 commit 9f4a0e1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ marked:
quotes: '“”‘’'
modifyAnchors: 0
autolink: true
mangle: true
sanitizeUrl: false
headerIds: true
lazyload: false
Expand All @@ -54,6 +55,8 @@ marked:
* Both double and single quotes substitution must be specified, otherwise it will be silently ignored.
- **modifyAnchors** - Transform the anchorIds into lower case (`1`) or upper case (`2`).
- **autolink** - Enable autolink for URLs. E.g. `https://hexo.io` will become `<a href="https://hexo.io">https://hexo.io</a>`.
- **mangle** - Escape autolinked email address with HTML character references.
* This is to obscure email address from _basic_ crawler used by spam bot, while still readable to web browsers.
- **sanitizeUrl** - Remove URLs that start with `javascript:`, `vbscript:` and `data:`.
- **headerIds** - Insert header id, e.g. `<h1 id="value">text</h1>`. Useful for inserting anchor link to each paragraph with a heading.
- **lazyload** - Lazy loading images via `loading="lazy"` attribute.
Expand All @@ -75,6 +78,8 @@ marked:
- Example: `[foo](http://bar.com)` becomes `<a href="http://bar.com" rel="noopener external nofollow noreferrer">foo</a>`
- **disableNunjucks**: If true, Nunjucks tags `{{ }}` or `{% %}` (usually used by [tag plugins](https://hexo.io/docs/tag-plugins)) will not be rendered.

For more options, see [Marked](https://marked.js.org/using_advanced#options). Due to the customizations implemented by this plugin, some of the Marked's options may not work as expected. Feel free to raise an [issue](https://github.com/hexojs/hexo-renderer-marked/issues) to us for clarification.

## Extras

### Definition/Description Lists
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ hexo.config.marked = Object.assign({
smartypants: true,
modifyAnchors: 0,
autolink: true,
mangle: true,
sanitizeUrl: false,
headerIds: true,
lazyload: false,
Expand Down
37 changes: 37 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,43 @@ describe('Marked renderer', () => {
});
});

describe('mangle', () => {
const body = 'Contact: hi@example.com';
const expected = '<p>Contact: <a href="mailto:hi@example.com">hi@example.com</a></p>\n';
// https://stackoverflow.com/a/39243641
const unescape = str => {
return str.replace(/&([^;]+);/g, (entity, entityCode) => {
const hex = entityCode.match(/^#x([\da-fA-F]+)$/);
const digit = entityCode.match(/^#(\d+)$/);

if (hex) {
return String.fromCharCode(parseInt(hex[1], 16));
} else if (digit) {
return String.fromCharCode(~~digit[1]);
}
return entity;

});
};

// mangle option only applies to autolinked email address
beforeEach(() => { hexo.config.marked.autolink = true; });

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

result.should.include('&#');
unescape(result).should.eql(expected);
});

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

result.should.eql(expected);
});
});

it('should render link with title', () => {
const body = [
'[text](http://link.com/ "a-title")',
Expand Down

0 comments on commit 9f4a0e1

Please sign in to comment.