diff --git a/.gitignore b/.gitignore index c9106a7..783b690 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules .nyc_output +*.browser.js diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..c5e8689 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "mochaExplorer.files": "test.js", +} diff --git a/debug.js b/debug.js index 4416765..abdb685 100644 --- a/debug.js +++ b/debug.js @@ -2,7 +2,7 @@ const md = require('markdown-it')(); const markdownItAttrs = require('./'); -md.use(markdownItAttrs).use(require('../markdown-it-implicit-figures')); +md.use(markdownItAttrs); const src = `header1 | header2 ------- | ------- diff --git a/index.js b/index.js index 5e414b7..ff7d934 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,8 @@ const patternsConfig = require('./patterns.js'); const defaultOptions = { leftDelimiter: '{', rightDelimiter: '}', - allowedAttributes: [] + allowedAttributes: [], + disableParagraphBlock: false }; module.exports = function attributes(md, options_) { diff --git a/markdown-it-attrs.browser.js b/markdown-it-attrs.browser.js deleted file mode 100644 index ef90393..0000000 --- a/markdown-it-attrs.browser.js +++ /dev/null @@ -1,773 +0,0 @@ -(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= 0 ? j : children.length + j; - } - } else { - var _loop3 = function _loop3(_j) { - match = childTests.every(function (tt) { - return test(children, _j, tt).match; - }); - if (match) { - 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) { - return { - v: { - v: res - } - }; - } - return { - v: "continue" - }; - }(); - if (_typeof(_ret2) === "object") return _ret2.v; - } - switch (_typeof(t[key])) { - case 'boolean': - case 'number': - case 'string': - if (token[key] !== t[key]) { - return { - v: res - }; - } - break; - case 'function': - if (!t[key](token[key])) { - return { - v: res - }; - } - break; - case 'object': - if (isArrayOfFunctions(t[key])) { - var r = t[key].every(function (tt) { - return tt(token[key]); - }); - if (r === false) { - return { - v: res - }; - } - break; - } - // fall through for objects !== arrays of functions - default: - 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 - res.match = true; - return res; -} -function isArrayOfObjects(arr) { - return Array.isArray(arr) && arr.length && arr.every(function (i) { - return _typeof(i) === 'object'; - }); -} -function isArrayOfFunctions(arr) { - return Array.isArray(arr) && arr.length && arr.every(function (i) { - return typeof i === 'function'; - }); -} - -/** - * Get n item of array. Supports negative n, where -1 is last - * element in array. - * @param {array} arr - * @param {number} n - */ -function get(arr, n) { - return n >= 0 ? arr[n] : arr[arr.length + n]; -} - -// get last element of array, safe - returns {} if not found -function last(arr) { - return arr.slice(-1)[0] || {}; -} - -},{"./patterns.js":2}],2:[function(require,module,exports){ -'use strict'; - -/** - * If a pattern matches the token stream, - * then run transform. - */ -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 [{ - /** - * ```python {.cls} - * for i in range(10): - * print(i) - * ``` - */ - name: 'fenced code blocks', - tests: [{ - shift: 0, - block: true, - info: utils.hasDelimiters('end', options) - }], - transform: function transform(tokens, i) { - var token = tokens[i]; - var start = token.info.lastIndexOf(options.leftDelimiter); - var attrs = utils.getAttrs(token.info, start, options); - utils.addAttrs(attrs, token); - token.info = utils.removeDelimiter(token.info, options); - } - }, { - /** - * bla `click()`{.c} ![](img.png){.d} - * - * differs from 'inline attributes' as it does - * not have a closing tag (nesting: -1) - */ - name: 'inline nesting 0', - tests: [{ - shift: 0, - type: 'inline', - children: [{ - shift: -1, - type: function type(str) { - return str === 'image' || str === 'code_inline'; - } - }, { - shift: 0, - type: 'text', - content: utils.hasDelimiters('start', options) - }] - }], - transform: function transform(tokens, i, j) { - var token = tokens[i].children[j]; - var endChar = token.content.indexOf(options.rightDelimiter); - var attrToken = tokens[i].children[j - 1]; - var attrs = utils.getAttrs(token.content, 0, options); - utils.addAttrs(attrs, attrToken); - if (token.content.length === endChar + options.rightDelimiter.length) { - tokens[i].children.splice(j, 1); - } else { - token.content = token.content.slice(endChar + options.rightDelimiter.length); - } - } - }, { - /** - * | h1 | - * | -- | - * | c1 | - * - * {.c} - */ - name: 'tables', - tests: [{ - // let this token be i, such that for-loop continues at - // next token after tokens.splice - shift: 0, - type: 'table_close' - }, { - shift: 1, - type: 'paragraph_open' - }, { - shift: 2, - type: 'inline', - content: utils.hasDelimiters('only', options) - }], - transform: function transform(tokens, i) { - var token = tokens[i + 2]; - var tableOpen = utils.getMatchingOpeningToken(tokens, i); - var attrs = utils.getAttrs(token.content, 0, options); - // add attributes - utils.addAttrs(attrs, tableOpen); - // remove

{.c}

- tokens.splice(i + 1, 3); - } - }, { - /** - * *emphasis*{.with attrs=1} - */ - name: 'inline attributes', - tests: [{ - shift: 0, - type: 'inline', - children: [{ - shift: -1, - nesting: -1 // closing inline tag, {.a} - }, { - shift: 0, - type: 'text', - content: utils.hasDelimiters('start', options) - }] - }], - transform: function transform(tokens, i, j) { - var token = tokens[i].children[j]; - var content = token.content; - var attrs = utils.getAttrs(content, 0, options); - var openingToken = utils.getMatchingOpeningToken(tokens[i].children, j - 1); - utils.addAttrs(attrs, openingToken); - token.content = content.slice(content.indexOf(options.rightDelimiter) + options.rightDelimiter.length); - } - }, { - /** - * - item - * {.a} - */ - name: 'list softbreak', - tests: [{ - shift: -2, - type: 'list_item_open' - }, { - shift: 0, - type: 'inline', - children: [{ - position: -2, - type: 'softbreak' - }, { - position: -1, - type: 'text', - content: utils.hasDelimiters('only', options) - }] - }], - transform: function transform(tokens, i, j) { - var token = tokens[i].children[j]; - var content = token.content; - var attrs = utils.getAttrs(content, 0, options); - 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]); - tokens[i].children = tokens[i].children.slice(0, -2); - } - }, { - /** - * - nested list - * - with double \n - * {.a} <-- apply to nested ul - * - * {.b} <-- apply to root