aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorache <ache@ache.one>2018-05-10 10:24:28 +0200
committerache <ache@ache.one>2018-05-10 10:24:28 +0200
commit10fd17730e6f3148c49618b927f631a73a70a78f (patch)
treeea13dae37b428f3c702fb0202f8617f64c2ae276
parentUpdate README (diff)
Add a configuration option
-rw-r--r--index.js98
1 files changed, 89 insertions, 9 deletions
diff --git a/index.js b/index.js
index 6eb862d..cf37de4 100644
--- a/index.js
+++ b/index.js
@@ -1,21 +1,89 @@
'use strict';
-var parseAttr = require('md-attr-parser');
+const parseAttr = require('md-attr-parser');
+const htmlElemAttr = require('html-element-attributes');
-module.exports = linkAttr;
+const DOMEventHandler = [
+ "onabort", "onautocomplete", "onautocompleteerror",
+ "onblur", "oncancel", "oncanplay",
+ "oncanplaythrough", "onchange", "onclick",
+ "onclose", "oncontextmenu", "oncuechange",
+ "ondblclick", "ondrag", "ondragend",
+ "ondragenter", "ondragexit", "ondragleave",
+ "ondragover", "ondragstart", "ondrop",
+ "ondurationchange", "onemptied", "onended",
+ "onerror", "onfocus", "oninput",
+ "oninvalid", "onkeydown", "onkeypress",
+ "onkeyup", "onload", "onloadeddata",
+ "onloadedmetadata", "onloadstart", "onmousedown",
+ "onmouseenter", "onmouseleave", "onmousemove",
+ "onmouseout", "onmouseover", "onmouseup",
+ "onmousewheel", "onpause", "onplay",
+ "onplaying", "onprogress", "onratechange",
+ "onreset", "onresize", "onscroll",
+ "onseeked", "onseeking", "onselect",
+ "onshow", "onsort", "onstalled",
+ "onsubmit", "onsuspend", "ontimeupdate",
+ "ontoggle", "onvolumechange", "onwaiting"
+];
+const convTypeTag = {
+ 'image':'img',
+ 'link': 'a',
+ 'header': 'h1',
+};
-function linkAttr() {
- var parser = this.Parser;
- var tokenizers;
+function filterAttributes( prop, config, type ) {
+ const scope = config.scope;
+ const allowDangerousDOMEventHandlers = config.allowDangerousDOMEventHandlers;
+
+ if( scope === "specific" ) {
+ Object.getOwnPropertyNames(prop).forEach ( p => {
+ if( htmlElemAttr[type].indexOf(p) < 0 &&
+ htmlElemAttr["*"].indexOf(p) < 0 &&
+ DOMEventHandler.indexOf(p) < 0 ) {
+ console.log(p);
+ delete prop[p];
+ }
+ });
+ } else if ( scope === "global" ) {
+ Object.getOwnPropertyNames(prop).forEach ( p => {
+ if( htmlElemAttr["*"].indexOf(p) < 0 &&
+ DOMEventHandler.indexOf(p) < 0 ) {
+ delete prop[p];
+ }
+ });
+ }
+ if( ! allowDangerousDOMEventHandlers ) {
+ Object.getOwnPropertyNames(prop).forEach ( p => {
+ if( DOMEventHandler.indexOf(p) >= 0 ) {
+ delete prop[p];
+ }
+ });
+ }
+ return prop;
+}
+
+module.exports = function linkAttr( config ) {
+ let parser = this.Parser;
+
+ if( config === undefined ) {
+ config = {
+ allowDangerousDOMEventHandlers: false,
+ elements: ["links","images","headers"],
+ extends: [],
+ scope: "specific",
+ };
+ }
if (!isRemarkParser(parser)) {
throw new Error('Missing parser to attach `remark-attr` [link] (to)');
}
- tokenizers = parser.prototype.inlineTokenizers;
+ let tokenizers = parser.prototype.inlineTokenizers;
+
+ const oldLink = tokenizers.link;
linkTokenize.locator = tokenizers.link.locator;
- let oldLink = tokenizers.link;
function linkTokenize(eat, value, silent) {
let linkEaten = oldLink.bind(this)(eat,value,silent);
@@ -31,6 +99,9 @@ function linkAttr() {
if( !linkEaten || !linkEaten.position ) {
return undefined;
}
+
+ const type = convTypeTag[linkEaten.type];
+
index = linkEaten.position.end.offset - linkEaten.position.start.offset;
if (index < length && value.charAt(index) === '{' ) {
@@ -38,12 +109,21 @@ function linkAttr() {
}
if (parsedAttr) {
- linkEaten.data = {hProperties: parsedAttr.prop};
+ if( config.scope && config.scope != "none" ) {
+
+ const filtredProp = filterAttributes( parsedAttr.prop, config, type );
+ if( filtredProp !== {} ) {
+ if( linkEaten.data ) {
+ linkEaten.data.hProperties = filtredProp;
+ } else {
+ linkEaten.data = {hProperties: filtredProp};
+ }
+ }
+ }
linkEaten = eat(parsedAttr.eaten)(linkEaten);
}
return linkEaten;
}
-
tokenizers.link = linkTokenize;
}