Skip to content

Commit

Permalink
feat: restore plugin sanitize function, rename to sanitizeUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
weyusi committed Jul 12, 2019
1 parent 28ce9bf commit ce94875
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -28,6 +28,7 @@ marked:
smartypants: true
modifyAnchors: ''
autolink: true
sanitizeUrl: false
```

- **gfm** - Enables [GitHub flavored markdown](https://help.github.com/articles/github-flavored-markdown)
Expand All @@ -37,6 +38,7 @@ marked:
- **smartypants** - Use "smart" typograhic punctuation for things like quotes and dashes.
- **modifyAnchors** - Use for transform anchorIds. if `1` to lowerCase and if `2` to upperCase. **Must be integer**.
- **autolink** - Enable autolink for URLs. E.g. `https://hexo.io` will become `<a href="https://hexo.io">https://hexo.io</a>`.
- **sanitizeUrl** - Remove URLs that start with `javascript:`, `vbscript:` and `data:`.

## Extras

Expand Down
3 changes: 2 additions & 1 deletion index.js
Expand Up @@ -11,7 +11,8 @@ hexo.config.marked = Object.assign({
smartLists: true,
smartypants: true,
modifyAnchors: '',
autolink: true
autolink: true,
sanitizeUrl: false
}, hexo.config.marked);

hexo.extend.renderer.register('md', 'html', renderer, true);
Expand Down
16 changes: 16 additions & 0 deletions lib/renderer.js
Expand Up @@ -35,6 +35,22 @@ function anchorId(str, transformOption) {

// Support AutoLink option
Renderer.prototype.link = function(href, title, text) {
if (this.options.sanitizeUrl) {
let prot;

try {
prot = decodeURIComponent(unescape(href))
.replace(/[^\w:]/g, '')
.toLowerCase();
} catch (e) {
return '';
}

if (prot.startsWith('javascript:') || prot.startsWith('vbscript:') || prot.startsWith('data:')) {
return '';
}
}

if (!this.options.autolink && href === text && title == null) {
return href;
}
Expand Down

0 comments on commit ce94875

Please sign in to comment.