mirror of
https://github.com/tenrok/BBob.git
synced 2026-06-17 19:21:20 +03:00
refactor(*): move helper fucntions from core and preset-html5 to separate package — plugin-helper
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
dist
|
||||
test
|
||||
rollup.config.js
|
||||
|
||||
@@ -3,7 +3,7 @@ const {
|
||||
OPEN_BRAKET,
|
||||
CLOSE_BRAKET,
|
||||
SLASH,
|
||||
} = require('./char');
|
||||
} = require('@bbob/plugin-helper/lib/char');
|
||||
|
||||
// type, value, line, row,
|
||||
const TOKEN_TYPE_ID = 'type'; // 0;
|
||||
|
||||
@@ -5,7 +5,7 @@ const {
|
||||
PLACEHOLDER_SPACE, PLACEHOLDER_SPACE_TAB,
|
||||
SLASH,
|
||||
BACKSLASH,
|
||||
} = require('./char');
|
||||
} = require('@bbob/plugin-helper/lib/char');
|
||||
const Token = require('./Token');
|
||||
|
||||
const createTokenOfType = (type, value, line, row) => new Token(type, value, line, row);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
const Tokenizer = require('./Tokenizer');
|
||||
const TagNode = require('./TagNode');
|
||||
const TagNode = require('@bbob/plugin-helper/lib/TagNode');
|
||||
|
||||
/**
|
||||
* @private
|
||||
@@ -28,14 +28,6 @@ let tokenizer = null;
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
let tokens = null;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param tag
|
||||
* @param attrs
|
||||
* @param content
|
||||
*/
|
||||
const newTagNode = (tag, attrs = {}, content = []) => new TagNode(tag, attrs, content);
|
||||
|
||||
const createTokenizer = (input, onToken) => new Tokenizer(input, { onToken });
|
||||
|
||||
/**
|
||||
@@ -56,7 +48,7 @@ const getTagNode = () => (tagNodes.length ? tagNodes[tagNodes.length - 1] : null
|
||||
* @param {Token} token
|
||||
* @return {Array}
|
||||
*/
|
||||
const createTagNode = token => tagNodes.push(newTagNode(token.getValue()));
|
||||
const createTagNode = token => tagNodes.push(TagNode.create(token.getValue()));
|
||||
/**
|
||||
* @private
|
||||
* @param {Token} token
|
||||
|
||||
@@ -19,6 +19,9 @@
|
||||
"html"
|
||||
],
|
||||
"main": "lib/index.js",
|
||||
"dependencies": {
|
||||
"@bbob/plugin-helper": "^1.0.0"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/JiLiZART/bbob.git"
|
||||
|
||||
@@ -1,25 +1,7 @@
|
||||
const {
|
||||
getChar, N, CLOSE_BRAKET, OPEN_BRAKET, SLASH,
|
||||
getChar, OPEN_BRAKET, CLOSE_BRAKET, SLASH,
|
||||
} = require('./char');
|
||||
|
||||
const EOL = getChar(N);
|
||||
const isNode = el => typeof el === 'object' && el.tag;
|
||||
const isStringNode = el => typeof el === 'string';
|
||||
const isEOL = el => el === EOL;
|
||||
|
||||
const getNodeLength = (node) => {
|
||||
if (isNode(node)) {
|
||||
return node.content.reduce((count, contentNode) => count + getNodeLength(contentNode), 0);
|
||||
} else if (isStringNode(node)) {
|
||||
return node.length;
|
||||
}
|
||||
|
||||
return 0;
|
||||
};
|
||||
|
||||
const appendToNode = (node, value) => {
|
||||
node.content.push(value);
|
||||
};
|
||||
const { getNodeLength, appendToNode } = require('./index');
|
||||
|
||||
class TagNode {
|
||||
constructor(tag, attrs, content) {
|
||||
@@ -53,7 +35,5 @@ class TagNode {
|
||||
}
|
||||
|
||||
module.exports = TagNode;
|
||||
module.exports.isNode = isNode;
|
||||
module.exports.isStringNode = isStringNode;
|
||||
module.exports.isEOL = isEOL;
|
||||
module.exports.appendToNode = appendToNode;
|
||||
module.exports.create = (tag, attrs = {}, content = []) => new TagNode(tag, attrs, content);
|
||||
module.exports.isOf = (node, type) => (node.tag === type);
|
||||
@@ -0,0 +1,29 @@
|
||||
const { getChar, N } = require('./char');
|
||||
|
||||
const isTagNode = el => typeof el === 'object' && el.tag;
|
||||
const isStringNode = el => typeof el === 'string';
|
||||
|
||||
const EOL = getChar(N);
|
||||
const isEOL = el => el === EOL;
|
||||
|
||||
const getNodeLength = (node) => {
|
||||
if (isTagNode(node)) {
|
||||
return node.content.reduce((count, contentNode) => count + getNodeLength(contentNode), 0);
|
||||
} else if (isStringNode(node)) {
|
||||
return node.length;
|
||||
}
|
||||
|
||||
return 0;
|
||||
};
|
||||
|
||||
const appendToNode = (node, value) => {
|
||||
node.content.push(value);
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
appendToNode,
|
||||
getNodeLength,
|
||||
isTagNode,
|
||||
isStringNode,
|
||||
isEOL,
|
||||
};
|
||||
@@ -0,0 +1,32 @@
|
||||
{
|
||||
"name": "@bbob/plugin-helper",
|
||||
"version": "1.0.0",
|
||||
"description": "Set of utils to help write plugins",
|
||||
"main": "lib/index.js",
|
||||
"directories": {
|
||||
"lib": "lib"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/JiLiZART/bbob.git"
|
||||
},
|
||||
"keywords": [
|
||||
"bbob",
|
||||
"plugin",
|
||||
"helper"
|
||||
],
|
||||
"bugs": {
|
||||
"url": "https://github.com/JiLiZART/bbob/issues"
|
||||
},
|
||||
"homepage": "https://github.com/JiLiZART/bbob#readme",
|
||||
"scripts": {
|
||||
"test": "../../node_modules/.bin/jest --",
|
||||
"cover": "../../node_modules/.bin/jest --coverage",
|
||||
"lint": "../../node_modules/.bin/eslint ."
|
||||
},
|
||||
"author": "Nikolay Kostyurin <jilizart@gmail.com>",
|
||||
"license": "MIT",
|
||||
"publishConfig": {
|
||||
"registry": "https://registry.npmjs.org/"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
|
||||
describe('@bbob/plugin-helper', () => {
|
||||
test('one', () => {
|
||||
expect(1 + 2).toBe(3)
|
||||
})
|
||||
});
|
||||
@@ -0,0 +1,128 @@
|
||||
/* eslint-disable no-plusplus,no-lonely-if */
|
||||
const { isStringNode, isTagNode } = require('@bbob/plugin-helper');
|
||||
const TagNode = require('@bbob/plugin-helper/lib/TagNode');
|
||||
|
||||
const isStartsWith = (node, type) => (node[0] === type);
|
||||
|
||||
const styleMap = {
|
||||
color: val => `color:${val};`,
|
||||
size: val => `font-size:${val};`,
|
||||
};
|
||||
|
||||
const getStyleFromAttrs = attrs =>
|
||||
Object
|
||||
.keys(attrs)
|
||||
.reduce((acc, key) => (styleMap[key] ? acc.concat(styleMap[key](attrs[key])) : acc), [])
|
||||
.join(' ');
|
||||
|
||||
const asListItems = (content) => {
|
||||
let listIdx = 0;
|
||||
const listItems = [];
|
||||
|
||||
const createItemNode = () => TagNode.create('li');
|
||||
const ensureListItem = (val) => {
|
||||
listItems[listIdx] = listItems[listIdx] || val;
|
||||
};
|
||||
const addItem = (val) => {
|
||||
if (listItems[listIdx] && listItems[listIdx].content) {
|
||||
listItems[listIdx].content = listItems[listIdx].content.concat(val);
|
||||
} else {
|
||||
listItems[listIdx] = listItems[listIdx].concat(val);
|
||||
}
|
||||
};
|
||||
|
||||
content.forEach((el) => {
|
||||
if (isStringNode(el) && isStartsWith(el, '*')) {
|
||||
if (listItems[listIdx]) {
|
||||
listIdx++;
|
||||
}
|
||||
ensureListItem(createItemNode());
|
||||
addItem(el.substr(1));
|
||||
} else if (isTagNode(el) && TagNode.isOf(el, '*')) {
|
||||
if (listItems[listIdx]) {
|
||||
listIdx++;
|
||||
}
|
||||
ensureListItem(createItemNode());
|
||||
} else if (!isTagNode(listItems[listIdx])) {
|
||||
listIdx++;
|
||||
ensureListItem(el);
|
||||
} else if (listItems[listIdx]) {
|
||||
addItem(el);
|
||||
} else {
|
||||
ensureListItem(el);
|
||||
}
|
||||
});
|
||||
|
||||
return [].concat(listItems);
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
b: node => ({
|
||||
tag: 'span',
|
||||
attrs: {
|
||||
style: 'font-weight: bold;',
|
||||
},
|
||||
content: node.content,
|
||||
}),
|
||||
i: node => ({
|
||||
tag: 'span',
|
||||
attrs: {
|
||||
style: 'font-style: italic;',
|
||||
},
|
||||
content: node.content,
|
||||
}),
|
||||
u: node => ({
|
||||
tag: 'span',
|
||||
attrs: {
|
||||
style: 'text-decoration: underline;',
|
||||
},
|
||||
content: node.content,
|
||||
}),
|
||||
s: node => ({
|
||||
tag: 'span',
|
||||
attrs: {
|
||||
style: 'text-decoration: line-through;',
|
||||
},
|
||||
content: node.content,
|
||||
}),
|
||||
url: (node, { render }) => ({
|
||||
tag: 'a',
|
||||
attrs: {
|
||||
href: node.attrs.url ? node.attrs.url : render(node.content),
|
||||
},
|
||||
content: node.content,
|
||||
}),
|
||||
img: (node, { render }) => ({
|
||||
tag: 'img',
|
||||
attrs: {
|
||||
src: render(node.content),
|
||||
},
|
||||
content: null,
|
||||
}),
|
||||
quote: node => ({
|
||||
tag: 'blockquote',
|
||||
attrs: {},
|
||||
content: [{
|
||||
tag: 'p',
|
||||
attrs: {},
|
||||
content: node.content,
|
||||
}],
|
||||
}),
|
||||
code: node => ({
|
||||
tag: 'pre',
|
||||
attrs: {},
|
||||
content: node.content,
|
||||
}),
|
||||
style: node => ({
|
||||
tag: 'span',
|
||||
attrs: {
|
||||
style: getStyleFromAttrs(node.attrs),
|
||||
},
|
||||
content: node.content,
|
||||
}),
|
||||
list: node => ({
|
||||
tag: 'ul',
|
||||
attrs: {},
|
||||
content: asListItems(node.content),
|
||||
}),
|
||||
};
|
||||
@@ -1,133 +1,5 @@
|
||||
/* eslint-disable no-plusplus,no-lonely-if */
|
||||
const isStringNode = node => (typeof node === 'string');
|
||||
const isStartsWith = (node, type) => (node[0] === type);
|
||||
|
||||
const isTagNode = node => (typeof node === 'object' && Boolean(node.tag));
|
||||
const isTagOf = (node, type) => (node.tag === type);
|
||||
const createTagNode = tag => ({ tag, attrs: {}, content: [] });
|
||||
|
||||
const styleMap = {
|
||||
color: val => `color:${val};`,
|
||||
size: val => `font-size:${val};`,
|
||||
};
|
||||
|
||||
const getStyleFromAttrs = attrs =>
|
||||
Object
|
||||
.keys(attrs)
|
||||
.reduce((acc, key) => (styleMap[key] ? acc.concat(styleMap[key](attrs[key])) : acc), [])
|
||||
.join(' ');
|
||||
|
||||
const asListItems = (content) => {
|
||||
const listItems = [];
|
||||
let listIdx = 0;
|
||||
const createItemNode = () => createTagNode('li');
|
||||
const ensureListItem = (val) => {
|
||||
listItems[listIdx] = listItems[listIdx] || val;
|
||||
};
|
||||
const addItem = (val) => {
|
||||
if (listItems[listIdx] && listItems[listIdx].content) {
|
||||
listItems[listIdx].content = listItems[listIdx].content.concat(val);
|
||||
} else {
|
||||
listItems[listIdx] = listItems[listIdx].concat(val);
|
||||
}
|
||||
};
|
||||
content.forEach((el) => {
|
||||
if (isStringNode(el) && isStartsWith(el, '*')) {
|
||||
if (listItems[listIdx]) {
|
||||
listIdx++;
|
||||
}
|
||||
ensureListItem(createItemNode());
|
||||
addItem(el.substr(1));
|
||||
} else if (isTagNode(el) && isTagOf(el, '*')) {
|
||||
if (listItems[listIdx]) {
|
||||
listIdx++;
|
||||
}
|
||||
ensureListItem(createItemNode());
|
||||
} else {
|
||||
if (!isTagNode(listItems[listIdx])) {
|
||||
listIdx++;
|
||||
ensureListItem(el);
|
||||
} else if (listItems[listIdx]) {
|
||||
addItem(el);
|
||||
} else {
|
||||
ensureListItem(el);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return [].concat(listItems);
|
||||
};
|
||||
|
||||
const defaultProcessors = {
|
||||
b: node => ({
|
||||
tag: 'span',
|
||||
attrs: {
|
||||
style: 'font-weight: bold;',
|
||||
},
|
||||
content: node.content,
|
||||
}),
|
||||
i: node => ({
|
||||
tag: 'span',
|
||||
attrs: {
|
||||
style: 'font-style: italic;',
|
||||
},
|
||||
content: node.content,
|
||||
}),
|
||||
u: node => ({
|
||||
tag: 'span',
|
||||
attrs: {
|
||||
style: 'text-decoration: underline;',
|
||||
},
|
||||
content: node.content,
|
||||
}),
|
||||
s: node => ({
|
||||
tag: 'span',
|
||||
attrs: {
|
||||
style: 'text-decoration: line-through;',
|
||||
},
|
||||
content: node.content,
|
||||
}),
|
||||
url: (node, { render }) => ({
|
||||
tag: 'a',
|
||||
attrs: {
|
||||
href: node.attrs.url ? node.attrs.url : render(node.content),
|
||||
},
|
||||
content: node.content,
|
||||
}),
|
||||
img: (node, { render }) => ({
|
||||
tag: 'img',
|
||||
attrs: {
|
||||
src: render(node.content),
|
||||
},
|
||||
content: null,
|
||||
}),
|
||||
quote: node => ({
|
||||
tag: 'blockquote',
|
||||
attrs: {},
|
||||
content: [{
|
||||
tag: 'p',
|
||||
attrs: {},
|
||||
content: node.content,
|
||||
}],
|
||||
}),
|
||||
code: node => ({
|
||||
tag: 'pre',
|
||||
attrs: {},
|
||||
content: node.content,
|
||||
}),
|
||||
style: node => ({
|
||||
tag: 'span',
|
||||
attrs: {
|
||||
style: getStyleFromAttrs(node.attrs),
|
||||
},
|
||||
content: node.content,
|
||||
}),
|
||||
list: node => ({
|
||||
tag: 'ul',
|
||||
attrs: {},
|
||||
content: asListItems(node.content),
|
||||
}),
|
||||
};
|
||||
const { isTagNode } = require('@bbob/plugin-helper');
|
||||
const defaultProcessors = require('./default');
|
||||
|
||||
module.exports = function html5Preset(opts = {}) {
|
||||
const processors = Object.assign({}, defaultProcessors, opts.processors || {});
|
||||
|
||||
@@ -6,6 +6,9 @@
|
||||
"directories": {
|
||||
"lib": "lib"
|
||||
},
|
||||
"dependencies": {
|
||||
"@bbob/plugin-helper": "^1.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@bbob/core": "^1.0.6"
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user