From ea33616b5c7ef9332af3689d97a3f4d1900a2c77 Mon Sep 17 00:00:00 2001 From: ache Date: Tue, 23 Oct 2018 03:26:11 +0200 Subject: Compatibility with upcomping version of remark-parse --- __tests__/index.js | 26 +------------- dist/dom-event-handler.js | 3 ++ dist/index.js | 86 +++++++++++++++++++++++++++++++++++++++++++---- src/dom-event-handler.js | 2 +- src/index.js | 42 ++++++----------------- 5 files changed, 95 insertions(+), 64 deletions(-) create mode 100644 dist/dom-event-handler.js diff --git a/__tests__/index.js b/__tests__/index.js index f96acd4..a8f4df8 100644 --- a/__tests__/index.js +++ b/__tests__/index.js @@ -159,7 +159,7 @@ This is an awesome code }); test('fenced code brackets', t => { - const fencedCodeString = `~~~lang{info=string} + const fencedCodeString = `~~~lang {info=string} This is an awesome code ~~~ @@ -180,27 +180,3 @@ This is an awesome code `)); }); -test('fenced code fallback', t => { - const fallbackFCstring = `~~~lang -This is an awesome code - -~~~ -{info=string} -`; - const {contents} = render(fallbackFCstring); - t.deepEqual(parse(contents), parse(`
This is an awesome code
-
`)); -}); - -test('fenced code mix', t => { - const fallbackFCstring = `~~~lang{info=strong} -This is an awesome code - -~~~ -{info=string} -`; - const {contents} = render(fallbackFCstring); - t.deepEqual(parse(contents), parse(`
This is an awesome code
-
`)); -}); - diff --git a/dist/dom-event-handler.js b/dist/dom-event-handler.js new file mode 100644 index 0000000..a5dc889 --- /dev/null +++ b/dist/dom-event-handler.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = ['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']; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index 1e83804..ab2f4a2 100644 --- a/dist/index.js +++ b/dist/index.js @@ -3,11 +3,13 @@ var parseAttr = require('md-attr-parser'); var htmlElemAttr = require('html-element-attributes'); -var supportedElements = ['link', 'atxHeading', 'strong', 'emphasis', 'deletion', 'code', 'setextHeading']; +var supportedElements = ['link', 'atxHeading', 'strong', 'emphasis', 'deletion', 'code', 'setextHeading', 'fencedCode']; var blockElements = ['atxHeading', 'setextHeading']; +var particularElements = ['fencedCode']; -// The list of DOM Event handler -var 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']; +var particularTokenize = {}; + +var DOMEventHandler = require('./dom-event-handler.js'); /* Table convertion between type and HTML tagName */ var convTypeTag = { @@ -18,6 +20,7 @@ var convTypeTag = { emphasis: 'em', delete: 's', inlineCode: 'code', + code: 'code', '*': '*' }; @@ -62,7 +65,7 @@ function tokenizeGenerator(prefix, oldParser, config) { // Then we check for attributes if (index + prefix.length < length && value.charAt(index + prefix.length) === '{') { // If any, parse it - parsedAttr = parseAttr(value, index + prefix.length); + parsedAttr = parseAttr(value, index + prefix.length, config.mdAttrConfig); } // If parsed configure the node @@ -177,6 +180,71 @@ function filterAttributes(prop, config, type) { return prop; } +/* This is a special modification of the function tokenizeGenerator + * to parse the fencedCode info string and the fallback + * customAttr parser + * + * It's only temporary + */ +function tokenizeFencedCode(oldParser, config) { + var prefix = '\n'; + function token(eat, value, silent) { + // This we call the old tokenize + var self = this; + var eaten = oldParser.call(self, eat, value, silent); + + var parsedAttr = void 0; + var parsedByCustomAttr = false; + + if (!eaten || !eaten.position) { + return undefined; + } + + var type = convTypeTag[eaten.type]; + + // First, parse the info string + // which is the 'lang' attributes of 'eaten'. + + if (eaten.lang) { + // Then the meta + if (eaten.meta) { + parsedAttr = parseAttr(eaten.meta); + } else { + // If it's an old version, we can still find from the attributes + // from 'value' ¯\_(ツ)_/¯ + // Bad hack, will be deleted soon + parsedAttr = parseAttr(value, value.indexOf(' ')); + } + } + + // If parsed configure the node + if (parsedAttr) { + if (config.scope && config.scope !== 'none') { + var filtredProp = filterAttributes(parsedAttr.prop, config, type); + + if (filtredProp !== {}) { + if (eaten.data) { + eaten.data.hProperties = Object.assign({}, eaten.data.hProperties, filtredProp); + } else { + eaten.data = { hProperties: filtredProp }; + } + } + } + if (parsedByCustomAttr) { + eaten = eat(prefix + parsedAttr.eaten)(eaten); + } + } + + return eaten; + } + + // Return the new tokenizer function + + return token; +} + +particularTokenize.fencedCode = tokenizeFencedCode; + remarkAttr.SUPPORTED_ELEMENTS = supportedElements; module.exports = remarkAttr; @@ -189,7 +257,8 @@ function remarkAttr(userConfig) { allowDangerousDOMEventHandlers: false, elements: supportedElements, extend: {}, - scope: 'extended' + scope: 'extended', + mdAttrConfig: undefined }; var config = Object.assign({}, defaultConfig, userConfig); @@ -206,9 +275,12 @@ function remarkAttr(userConfig) { if (blockElements.indexOf(elem) >= 0) { var oldElem = tokenizersBlock[elem]; tokenizersBlock[elem] = tokenizeGenerator('\n', oldElem, config); + } else if (particularElements.indexOf(elem) >= 0) { + var _oldElem = tokenizersBlock[elem]; + tokenizersBlock[elem] = particularTokenize[elem](_oldElem, config); } else { - var _oldElem = tokenizers[elem]; - var elemTokenize = tokenizeGenerator('', _oldElem, config); + var _oldElem2 = tokenizers[elem]; + var elemTokenize = tokenizeGenerator('', _oldElem2, config); elemTokenize.locator = tokenizers[elem].locator; tokenizers[elem] = elemTokenize; } diff --git a/src/dom-event-handler.js b/src/dom-event-handler.js index dc4f3bd..d559e91 100644 --- a/src/dom-event-handler.js +++ b/src/dom-event-handler.js @@ -1,6 +1,6 @@ 'use strict'; -module.export = [ +module.exports = [ 'onabort', 'onautocomplete', 'onautocompleteerror', diff --git a/src/index.js b/src/index.js index 49ee990..2e839e8 100644 --- a/src/index.js +++ b/src/index.js @@ -159,6 +159,8 @@ function filterAttributes(prop, config, type) { /* This is a special modification of the function tokenizeGenerator * to parse the fencedCode info string and the fallback * customAttr parser + * + * It's only temporary */ function tokenizeFencedCode(oldParser, config) { const prefix = '\n'; @@ -167,10 +169,8 @@ function tokenizeFencedCode(oldParser, config) { const self = this; let eaten = oldParser.call(self, eat, value, silent); - let index = 0; let parsedAttr; - let parsedByCustomAttr = false; - const {length} = value; + const parsedByCustomAttr = false; if (!eaten || !eaten.position) { return undefined; @@ -182,37 +182,17 @@ function tokenizeFencedCode(oldParser, config) { // which is the 'lang' attributes of 'eaten'. if (eaten.lang) { - let infoPart = ''; - if (eaten.lang.indexOf(' ') >= 0) { - if (eaten.lang.indexOf('{') >= 0) { - const posStart = Math.min(eaten.lang.indexOf(' '), eaten.lang.indexOf('{')); - infoPart = eaten.lang.substr(posStart); - - if (posStart === eaten.lang.indexOf('{')) { - eaten.lang = eaten.lang.substr(0, eaten.lang.indexOf('{')) + ' ' + infoPart; - } - } else { - infoPart = eaten.lang.substr(eaten.lang.indexOf(' ')); - } - } else if (eaten.lang.indexOf('{') >= 0) { - infoPart = eaten.lang.substr(eaten.lang.indexOf('{')); - eaten.lang = eaten.lang.substr(0, eaten.lang.indexOf('{')) + ' ' + infoPart; - } - - if (infoPart) { - parsedAttr = parseAttr(infoPart, 0, config.mdAttrConfig); + // Then the meta + if (eaten.meta) { + parsedAttr = parseAttr(eaten.meta); + } else { + // If it's an old version, we can still find from the attributes + // from 'value' ¯\_(ツ)_/¯ + // Bad hack, will be deleted soon + parsedAttr = parseAttr(value, value.indexOf(' ')); } } - index = eaten.position.end.offset - eaten.position.start.offset; - - // Then we check for attributes - if (index + prefix.length < length && value.charAt(index + prefix.length) === '{') { - // If any, parse it - parsedAttr = {...parsedAttr, ...parseAttr(value, index + prefix.length, config.mdAttrConfig)}; - parsedByCustomAttr = Boolean(parsedAttr); - } - // If parsed configure the node if (parsedAttr) { if (config.scope && config.scope !== 'none') { -- cgit v1.2.3