aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorache <ache@ache.one>2020-05-05 09:17:23 +0200
committerache <ache@ache.one>2020-05-05 09:17:23 +0200
commitdb50e2a7cf32dfa9f70c5ff4b3b4cb8800add9ac (patch)
treea66db38ad3f2767afec0f64e10fff9e7785c9f8e
parentAdd atx header inline test (diff)
Implemente atx header inline
-rw-r--r--src/index.js60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/index.js b/src/index.js
index 3007b90..c4c14ed 100644
--- a/src/index.js
+++ b/src/index.js
@@ -2,6 +2,7 @@
const parseAttr = require('md-attr-parser');
const htmlElemAttr = require('html-element-attributes');
+const isWhiteSpace = require('is-whitespace-character');
const supportedElements = ['link', 'atxHeading', 'strong', 'emphasis', 'deletion', 'code', 'setextHeading', 'fencedCode', 'reference'];
const blockElements = ['atxHeading', 'setextHeading'];
@@ -91,6 +92,65 @@ function tokenizeGenerator(prefix, oldParser, config) {
return token;
}
+function tokenizeModifierGenerator(oldParser, config) {
+ function token(eat, value, silent) {
+ // This we call the old tokenize
+ const self = this;
+ let eaten = oldParser.call(self, eat, value, silent);
+
+ let index = 0;
+ const {length} = value;
+
+ if (!eaten || !eaten.position ||
+ !eaten.children || eaten.children.length <= 0) {
+ return eaten;
+ }
+
+ const type = convTypeTag[eaten.type];
+
+ const lastChild = eaten.children[eaten.children.length - 1];
+
+ if (!lastChild.value || lastChild.value.length <= 0 ||
+ lastChild.value[lastChild.value.length - 1] !== '}') {
+ return eaten;
+ }
+
+ index = lastChild.value.lastIndexOf('{');
+
+ const parsedAttr = parseAttr(lastChild.value, index, config.mdAttrConfig);
+
+ if (parsedAttr.eaten.length !== lastChild.value.length - index) {
+ return eaten;
+ }
+
+ index -= 1;
+ while (index >= 0 && isWhiteSpace(lastChild.value[index])) {
+ index -= 1;
+ }
+
+ // If parsed configure the node
+ if (parsedAttr) {
+ if (config.scope && config.scope !== 'none') {
+ const filtredProp = filterAttributes(parsedAttr.prop, config, type);
+ if (filtredProp !== {}) {
+ if (eaten.data) {
+ eaten.data.hProperties = filtredProp;
+ } else {
+ eaten.data = {hProperties: filtredProp};
+ }
+ }
+ }
+
+ lastChild.value = lastChild.value.slice(0, index + 1);
+ }
+
+ return eaten;
+ }
+
+ // Return the new tokenizer function
+ return token;
+}
+
// A generic function to parse attributes
function filterAttributes(prop, config, type) {
const {scope} = config;