markdown-it-attrs
Advanced tools
Comparing version 4.1.4 to 4.1.6
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.markdownItAttrs = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ | ||
'use strict'; | ||
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } | ||
var patternsConfig = require('./patterns.js'); | ||
var defaultOptions = { | ||
@@ -13,3 +11,2 @@ leftDelimiter: '{', | ||
}; | ||
module.exports = function attributes(md, options_) { | ||
@@ -19,6 +16,4 @@ var options = Object.assign({}, defaultOptions); | ||
var patterns = patternsConfig(options); | ||
function curlyAttrs(state) { | ||
var tokens = state.tokens; | ||
var _loop = function _loop(i) { | ||
@@ -28,16 +23,11 @@ for (var p = 0; p < patterns.length; p++) { | ||
var j = null; // position of child with offset 0 | ||
var match = pattern.tests.every(function (t) { | ||
var res = test(tokens, i, t); | ||
if (res.j !== null) { | ||
j = res.j; | ||
} | ||
return res.match; | ||
}); | ||
if (match) { | ||
pattern.transform(tokens, i, j); | ||
if (pattern.name === 'inline attributes' || pattern.name === 'inline nesting 0') { | ||
@@ -50,3 +40,2 @@ // retry, may be several inline attributes | ||
}; | ||
for (var i = 0; i < tokens.length; i++) { | ||
@@ -56,5 +45,5 @@ _loop(i); | ||
} | ||
md.core.ruler.before('linkify', 'curly_attributes', curlyAttrs); | ||
}; | ||
/** | ||
@@ -68,4 +57,2 @@ * Test if t matches token stream. | ||
*/ | ||
function test(tokens, i, t) { | ||
@@ -75,6 +62,5 @@ var res = { | ||
j: null // position of child | ||
}; | ||
}; | ||
var ii = t.shift !== undefined ? i + t.shift : t.position; | ||
if (t.shift !== undefined && ii < 0) { | ||
@@ -84,3 +70,2 @@ // we should never shift to negative indexes (rolling around to back of array) | ||
} | ||
var token = get(tokens, ii); // supports negative ii | ||
@@ -91,10 +76,7 @@ | ||
} | ||
var _loop2 = function _loop2() { | ||
var key = _Object$keys[_i]; | ||
if (key === 'shift' || key === 'position') { | ||
return "continue"; | ||
} | ||
if (token[key] === undefined) { | ||
@@ -105,3 +87,2 @@ return { | ||
} | ||
if (key === 'children' && isArrayOfObjects(t.children)) { | ||
@@ -116,7 +97,5 @@ var _ret2 = function () { | ||
} | ||
var match; | ||
var childTests = t.children; | ||
var children = token.children; | ||
if (childTests.every(function (tt) { | ||
@@ -129,3 +108,2 @@ return tt.position !== undefined; | ||
}); | ||
if (match) { | ||
@@ -141,17 +119,13 @@ // we may need position of child in transform | ||
}); | ||
if (match) { | ||
res.j = _j; // all tests true, continue with next key of pattern t | ||
res.j = _j; | ||
// all tests true, continue with next key of pattern t | ||
return "break"; | ||
} | ||
}; | ||
for (var _j = 0; _j < children.length; _j++) { | ||
var _ret3 = _loop3(_j); | ||
if (_ret3 === "break") break; | ||
} | ||
} | ||
if (match === false) { | ||
@@ -164,3 +138,2 @@ return { | ||
} | ||
return { | ||
@@ -170,6 +143,4 @@ v: "continue" | ||
}(); | ||
if (_typeof(_ret2) === "object") return _ret2.v; | ||
} | ||
switch (_typeof(t[key])) { | ||
@@ -184,5 +155,3 @@ case 'boolean': | ||
} | ||
break; | ||
case 'function': | ||
@@ -194,5 +163,3 @@ if (!t[key](token[key])) { | ||
} | ||
break; | ||
case 'object': | ||
@@ -203,3 +170,2 @@ if (isArrayOfFunctions(t[key])) { | ||
}); | ||
if (r === false) { | ||
@@ -210,8 +176,5 @@ return { | ||
} | ||
break; | ||
} | ||
// fall through for objects !== arrays of functions | ||
default: | ||
@@ -221,15 +184,12 @@ throw new Error("Unknown type of pattern test (key: ".concat(key, "). Test should be of type boolean, number, string, function or array of functions.")); | ||
}; | ||
for (var _i = 0, _Object$keys = Object.keys(t); _i < _Object$keys.length; _i++) { | ||
var _ret = _loop2(); | ||
if (_ret === "continue") continue; | ||
if (_typeof(_ret) === "object") return _ret.v; | ||
} // no tests returned false -> all tests returns true | ||
} | ||
// no tests returned false -> all tests returns true | ||
res.match = true; | ||
return res; | ||
} | ||
function isArrayOfObjects(arr) { | ||
@@ -240,3 +200,2 @@ return Array.isArray(arr) && arr.length && arr.every(function (i) { | ||
} | ||
function isArrayOfFunctions(arr) { | ||
@@ -247,2 +206,3 @@ return Array.isArray(arr) && arr.length && arr.every(function (i) { | ||
} | ||
/** | ||
@@ -254,9 +214,7 @@ * Get n item of array. Supports negative n, where -1 is last | ||
*/ | ||
function get(arr, n) { | ||
return n >= 0 ? arr[n] : arr[arr.length + n]; | ||
} // get last element of array, safe - returns {} if not found | ||
} | ||
// get last element of array, safe - returns {} if not found | ||
function last(arr) { | ||
@@ -268,2 +226,3 @@ return arr.slice(-1)[0] || {}; | ||
'use strict'; | ||
/** | ||
@@ -273,8 +232,5 @@ * If a pattern matches the token stream, | ||
*/ | ||
var utils = require('./utils.js'); | ||
module.exports = function (options) { | ||
var __hr = new RegExp('^ {0,3}[-*_]{3,} ?' + utils.escapeRegExp(options.leftDelimiter) + '[^' + utils.escapeRegExp(options.rightDelimiter) + ']'); | ||
return [{ | ||
@@ -328,3 +284,2 @@ /** | ||
utils.addAttrs(attrs, attrToken); | ||
if (token.content.length === endChar + options.rightDelimiter.length) { | ||
@@ -361,6 +316,6 @@ tokens[i].children.splice(j, 1); | ||
var tableOpen = utils.getMatchingOpeningToken(tokens, i); | ||
var attrs = utils.getAttrs(token.content, 0, options); // add attributes | ||
utils.addAttrs(attrs, tableOpen); // remove <p>{.c}</p> | ||
var attrs = utils.getAttrs(token.content, 0, options); | ||
// add attributes | ||
utils.addAttrs(attrs, tableOpen); | ||
// remove <p>{.c}</p> | ||
tokens.splice(i + 1, 3); | ||
@@ -379,3 +334,2 @@ } | ||
nesting: -1 // closing inline tag, </em>{.a} | ||
}, { | ||
@@ -421,7 +375,5 @@ shift: 0, | ||
var ii = i - 2; | ||
while (tokens[ii - 1] && tokens[ii - 1].type !== 'ordered_list_open' && tokens[ii - 1].type !== 'bullet_list_open') { | ||
ii--; | ||
} | ||
utils.addAttrs(attrs, tokens[ii - 1]); | ||
@@ -513,10 +465,8 @@ tokens[i].children = tokens[i].children.slice(0, -2); | ||
var token = tokens[i].children[j]; | ||
var attrs = utils.getAttrs(token.content, 0, options); // find last closing tag | ||
var attrs = utils.getAttrs(token.content, 0, options); | ||
// find last closing tag | ||
var ii = i + 1; | ||
while (tokens[ii + 1] && tokens[ii + 1].nesting === -1) { | ||
ii++; | ||
} | ||
var openingToken = utils.getMatchingOpeningToken(tokens, ii); | ||
@@ -580,7 +530,5 @@ utils.addAttrs(attrs, openingToken); | ||
var ii = i + 1; | ||
while (tokens[ii + 1] && tokens[ii + 1].nesting === -1) { | ||
ii++; | ||
} | ||
var openingToken = utils.getMatchingOpeningToken(tokens, ii); | ||
@@ -592,5 +540,5 @@ utils.addAttrs(attrs, openingToken); | ||
}]; | ||
}; // get last element of array or string | ||
}; | ||
// get last element of array or string | ||
function last(arr) { | ||
@@ -620,6 +568,7 @@ return arr.slice(-1)[0]; | ||
var parsingKey = true; | ||
var valueInsideQuotes = false; // read inside {} | ||
var valueInsideQuotes = false; | ||
// read inside {} | ||
// start + left delimiter length to avoid beginning { | ||
// breaks when } is found or end of string | ||
for (var i = start + options.leftDelimiter.length; i < str.length; i++) { | ||
@@ -630,14 +579,13 @@ if (str.slice(i, i + options.rightDelimiter.length) === options.rightDelimiter) { | ||
} | ||
break; | ||
} | ||
var char_ = str.charAt(i); | ||
var char_ = str.charAt(i); // switch to reading value if equal sign | ||
// switch to reading value if equal sign | ||
if (char_ === keySeparator && parsingKey) { | ||
parsingKey = false; | ||
continue; | ||
} // {.class} {..css-module} | ||
} | ||
// {.class} {..css-module} | ||
if (char_ === classChar && key === '') { | ||
@@ -650,8 +598,7 @@ if (str.charAt(i + 1) === classChar) { | ||
} | ||
parsingKey = false; | ||
continue; | ||
} // {#id} | ||
} | ||
// {#id} | ||
if (char_ === idChar && key === '') { | ||
@@ -661,16 +608,15 @@ key = 'id'; | ||
continue; | ||
} // {value="inside quotes"} | ||
} | ||
if (char_ === '"' && value === '') { | ||
// {value="inside quotes"} | ||
if (char_ === '"' && value === '' && !valueInsideQuotes) { | ||
valueInsideQuotes = true; | ||
continue; | ||
} | ||
if (char_ === '"' && valueInsideQuotes) { | ||
valueInsideQuotes = false; | ||
continue; | ||
} // read next key/value pair | ||
} | ||
// read next key/value pair | ||
if (char_ === pairSeparator && !valueInsideQuotes) { | ||
@@ -681,3 +627,2 @@ if (key === '') { | ||
} | ||
attrs.push([key, value]); | ||
@@ -688,10 +633,10 @@ key = ''; | ||
continue; | ||
} // continue if character not allowed | ||
} | ||
// continue if character not allowed | ||
if (parsingKey && char_.search(allowedKeyChars) === -1) { | ||
continue; | ||
} // no other conditions met; append to key/value | ||
} | ||
// no other conditions met; append to key/value | ||
if (parsingKey) { | ||
@@ -701,6 +646,4 @@ key += char_; | ||
} | ||
value += char_; | ||
} | ||
if (options.allowedAttributes && options.allowedAttributes.length) { | ||
@@ -710,13 +653,11 @@ var allowedAttributes = options.allowedAttributes; | ||
var attr = attrPair[0]; | ||
function isAllowedAttribute(allowedAttribute) { | ||
return attr === allowedAttribute || allowedAttribute instanceof RegExp && allowedAttribute.test(attr); | ||
} | ||
return allowedAttributes.some(isAllowedAttribute); | ||
}); | ||
} | ||
return attrs; | ||
}; | ||
/** | ||
@@ -728,8 +669,5 @@ * add attributes from [['key', 'val']] list | ||
*/ | ||
exports.addAttrs = function (attrs, token) { | ||
for (var j = 0, l = attrs.length; j < l; ++j) { | ||
var key = attrs[j][0]; | ||
if (key === 'class') { | ||
@@ -743,5 +681,5 @@ token.attrJoin('class', attrs[j][1]); | ||
} | ||
return token; | ||
}; | ||
/** | ||
@@ -757,4 +695,2 @@ * Does string have properly formatted curly? | ||
*/ | ||
exports.hasDelimiters = function (where, options) { | ||
@@ -764,2 +700,3 @@ if (!where) { | ||
} | ||
/** | ||
@@ -769,12 +706,8 @@ * @param {string} str | ||
*/ | ||
return function (str) { | ||
// we need minimum three chars, for example {b} | ||
var minCurlyLength = options.leftDelimiter.length + 1 + options.rightDelimiter.length; | ||
if (!str || typeof str !== 'string' || str.length < minCurlyLength) { | ||
return false; | ||
} | ||
function validCurlyLength(curly) { | ||
@@ -785,6 +718,4 @@ var isClass = curly.charAt(options.leftDelimiter.length) === '.'; | ||
} | ||
var start, end, slice, nextChar; | ||
var rightDelimiterMinimumShift = minCurlyLength - options.rightDelimiter.length; | ||
switch (where) { | ||
@@ -795,12 +726,9 @@ case 'start': | ||
start = slice === options.leftDelimiter ? 0 : -1; | ||
end = start === -1 ? -1 : str.indexOf(options.rightDelimiter, rightDelimiterMinimumShift); // check if next character is not one of the delimiters | ||
end = start === -1 ? -1 : str.indexOf(options.rightDelimiter, rightDelimiterMinimumShift); | ||
// check if next character is not one of the delimiters | ||
nextChar = str.charAt(end + options.rightDelimiter.length); | ||
if (nextChar && options.rightDelimiter.indexOf(nextChar) !== -1) { | ||
end = -1; | ||
} | ||
break; | ||
case 'end': | ||
@@ -812,3 +740,2 @@ // last char should be } | ||
break; | ||
case 'only': | ||
@@ -821,15 +748,12 @@ // '{.a}' | ||
break; | ||
default: | ||
throw new Error("Unexpected case ".concat(where, ", expected 'start', 'end' or 'only'")); | ||
} | ||
return start !== -1 && end !== -1 && validCurlyLength(str.substring(start, end + options.rightDelimiter.length)); | ||
}; | ||
}; | ||
/** | ||
* Removes last curly from string. | ||
*/ | ||
exports.removeDelimiter = function (str, options) { | ||
@@ -842,2 +766,3 @@ var start = escapeRegExp(options.leftDelimiter); | ||
}; | ||
/** | ||
@@ -850,26 +775,20 @@ * Escapes special characters in string s such that the string | ||
*/ | ||
function escapeRegExp(s) { | ||
return s.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&'); | ||
} | ||
exports.escapeRegExp = escapeRegExp; | ||
exports.escapeRegExp = escapeRegExp; | ||
/** | ||
* find corresponding opening block | ||
*/ | ||
exports.getMatchingOpeningToken = function (tokens, i) { | ||
if (tokens[i].type === 'softbreak') { | ||
return false; | ||
} // non closing blocks, example img | ||
} | ||
// non closing blocks, example img | ||
if (tokens[i].nesting === 0) { | ||
return tokens[i]; | ||
} | ||
var level = tokens[i].level; | ||
var type = tokens[i].type.replace('_close', '_open'); | ||
for (; i >= 0; --i) { | ||
@@ -880,10 +799,8 @@ if (tokens[i].type === type && tokens[i].level === level) { | ||
} | ||
return false; | ||
}; | ||
/** | ||
* from https://github.com/markdown-it/markdown-it/blob/master/lib/common/utils.js | ||
*/ | ||
var HTML_ESCAPE_TEST_RE = /[&<>"]/; | ||
@@ -897,7 +814,5 @@ var HTML_ESCAPE_REPLACE_RE = /[&<>"]/g; | ||
}; | ||
function replaceUnsafeChar(ch) { | ||
return HTML_REPLACEMENTS[ch]; | ||
} | ||
exports.escapeHtml = function (str) { | ||
@@ -907,3 +822,2 @@ if (HTML_ESCAPE_TEST_RE.test(str)) { | ||
} | ||
return str; | ||
@@ -910,0 +824,0 @@ }; |
{ | ||
"name": "markdown-it-attrs", | ||
"version": "4.1.4", | ||
"version": "4.1.6", | ||
"description": "Add classes, identifiers and attributes to your markdown with {} curly brackets, similar to pandoc's header attributes", | ||
@@ -39,6 +39,6 @@ "main": "index.js", | ||
"devDependencies": { | ||
"@babel/core": "^7.15.5", | ||
"@babel/preset-env": "^7.15.6", | ||
"@babel/core": "^7.20.5", | ||
"@babel/preset-env": "^7.20.2", | ||
"babelify": "^10.0.0", | ||
"browserify": "^16.5.2", | ||
"browserify": "^17.0.0", | ||
"coveralls": "^3.1.1", | ||
@@ -45,0 +45,0 @@ "eslint": "^8.4.1", |
@@ -57,3 +57,3 @@ /** | ||
// {value="inside quotes"} | ||
if (char_ === '"' && value === '') { | ||
if (char_ === '"' && value === '' && !valueInsideQuotes) { | ||
valueInsideQuotes = true; | ||
@@ -60,0 +60,0 @@ continue; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
62363
14
1550