From 064a37372b23c50afaa3ebdafaeed9387c355029 Mon Sep 17 00:00:00 2001 From: Nikolay Kostyurin Date: Mon, 6 Aug 2018 02:27:24 +0200 Subject: [PATCH] style(preset-html5): fix eslint code style --- packages/bbob-preset-html5/lib/index.js | 68 ++++++------------------- 1 file changed, 15 insertions(+), 53 deletions(-) diff --git a/packages/bbob-preset-html5/lib/index.js b/packages/bbob-preset-html5/lib/index.js index ed8d563..606a563 100644 --- a/packages/bbob-preset-html5/lib/index.js +++ b/packages/bbob-preset-html5/lib/index.js @@ -1,50 +1,10 @@ -// [b]bolded text[/b] => bolded text -// [i]italicized text[/i] => italicized text -// [u]underlined text[/u] => underlined text -// [s]strikethrough text[/s] -// => strikethrough text -// [url]https://en.wikipedia.org[/url] => https://en.wikipedia.org -// [url=http://step.pgc.edu/]ECAT[/url] => ECAT -// [img]https://upload.wikimedia.org/wikipedia/commons/thumb/4/47/Go-home-2.svg/100px-Go-home-2.svg.png[/img] -// => -// [quote="author"]quoted text[/quote] =>

quoted text

-// [code]monospaced text[/code] =>
monospaced text
-// [style size="15px"]Large Text[/style] => Large Text -// [style color="red"]Red Text[/style] => Red Text +/* eslint-disable no-plusplus,no-lonely-if */ +const isStringNode = node => (typeof node === 'string'); +const isStartsWith = (node, type) => (node[0] === type); -/** - [list] - [*]Entry 1 - [*]Entry 2 - [/list] - - [list] - *Entry 1 - *Entry 2 - [/list] - - => - - */ - -/** - [table] - [tr] - [td]table 1[/td] - [td]table 2[/td] - [/tr] - [tr] - [td]table 3[/td] - [td]table 4[/td] - [/tr] - [/table] - => - - -
table 1table 2
table 3table 4
- */ - -// [b]bolded text[/b] => bolded text +const isTagNode = node => (typeof node === 'object' && Boolean(node.tag)); +const isTagOf = (node, type) => (node.tag === type); +const createTagNode = tag => ({ tag, attrs: {}, content: [] }); const getStyleFromAttrs = (attrs) => { const styles = []; @@ -63,7 +23,7 @@ const getStyleFromAttrs = (attrs) => { const asListItems = (content) => { const listItems = []; let listIdx = 0; - const createItemNode = () => ({ tag: 'li', attrs: {}, content: [] }); + const createItemNode = () => createTagNode('li'); const ensureListItem = (val) => { listItems[listIdx] = listItems[listIdx] || val; }; @@ -74,20 +34,20 @@ const asListItems = (content) => { listItems[listIdx] = listItems[listIdx].concat(val); } }; - content.forEach(el => { - if (typeof el === 'string' && el[0] === '*') { + content.forEach((el) => { + if (isStringNode(el) && isStartsWith(el, '*')) { if (listItems[listIdx]) { listIdx++; } ensureListItem(createItemNode()); addItem(el.substr(1)); - } else if (typeof el === 'object' && el.tag && el.tag === '*') { + } else if (isTagNode(el) && isTagOf(el, '*')) { if (listItems[listIdx]) { listIdx++; } ensureListItem(createItemNode()); } else { - if (listItems[listIdx] && !listItems[listIdx].tag) { + if (!isTagNode(listItems[listIdx])) { listIdx++; ensureListItem(el); } else if (listItems[listIdx]) { @@ -101,7 +61,7 @@ const asListItems = (content) => { return [].concat(listItems); }; -const processors = { +const defaultProcessors = { b: node => ({ tag: 'span', attrs: { @@ -173,8 +133,10 @@ const processors = { }; module.exports = function html5Preset(opts = {}) { + const processors = Object.assign({}, defaultProcessors, opts.processors || {}); + return function process(tree, core) { - tree.walk(node => (node.tag && processors[node.tag] + tree.walk(node => (isTagNode(node) && processors[node.tag] ? processors[node.tag](node, core) : node)); };