diff --git a/README.md b/README.md index dabb609..919d226 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ marked: quotes: '“”‘’' modifyAnchors: 0 autolink: true + mangle: true sanitizeUrl: false headerIds: true lazyload: false @@ -53,6 +54,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 `https://hexo.io`. +- **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. `

text

`. Useful for inserting anchor link to each paragraph with a heading. - **lazyload** - Lazy loading images via `loading="lazy"` attribute. @@ -73,6 +76,8 @@ marked: * **nofollow** - Add `rel="noopener external nofollow noreferrer"` to all external links for security, privacy and SEO. [Read more](https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types). _This can be enabled regardless of `external_link.enable`_ - Example: `[foo](http://bar.com)` becomes `foo` +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 diff --git a/index.js b/index.js index 4a82c9b..7f5c4ce 100644 --- a/index.js +++ b/index.js @@ -12,6 +12,7 @@ hexo.config.marked = Object.assign({ smartypants: true, modifyAnchors: 0, autolink: true, + mangle: true, sanitizeUrl: false, headerIds: true, lazyload: false, diff --git a/test/index.js b/test/index.js index 27a4090..a85e26c 100644 --- a/test/index.js +++ b/test/index.js @@ -307,6 +307,43 @@ describe('Marked renderer', () => { }); }); + describe('mangle', () => { + const body = 'Contact: hi@example.com'; + const expected = '

Contact: hi@example.com

\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")',