2
0
mirror of https://github.com/tenrok/BBob.git synced 2026-06-20 20:00:33 +03:00

add html5 preset, some files structure changes

This commit is contained in:
Nikolay Kostyurin
2018-07-06 00:38:29 +02:00
parent 12144415cc
commit 21ef50cf1d
21 changed files with 322 additions and 80 deletions
+3
View File
@@ -0,0 +1,3 @@
package-lock.json
coverage
dist
+6
View File
@@ -0,0 +1,6 @@
package-lock.json
coverage
src
dist
!lib
*.test.js
+1
View File
@@ -0,0 +1 @@
Preset to render BBCode to HTML tags
+72
View File
@@ -0,0 +1,72 @@
// [b]bolded text[/b] => <span style="font-weight: bold;">bolded text</span>
// [i]italicized text[/i] => <span style="font-style: italic;">italicized text</span>
// [u]underlined text[/u] => <span style="text-decoration: underline;">underlined text</span>
// [s]strikethrough text[/s] => <span style="text-decoration: line-through;">strikethrough text</span>
// [url]https://en.wikipedia.org[/url] => <a href="https://en.wikipedia.org">https://en.wikipedia.org</a>
// [url=http://step.pgc.edu/]ECAT[/url] => <a href="http://step.pgc.edu/">ECAT</a>
// [img]https://upload.wikimedia.org/wikipedia/commons/thumb/4/47/Go-home-2.svg/100px-Go-home-2.svg.png[/img] => <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/47/Go-home-2.svg/100px-Go-home-2.svg.png" alt="" />
// [quote="author"]quoted text[/quote] => <blockquote><p>quoted text</p></blockquote>
// [code]monospaced text[/code] => <pre>monospaced text</pre>
// [style size="15px"]Large Text[/style] => <span style="font-size:15px">Large Text</span>
// [style color="red"]Red Text[/style] => <span style="color:red;">Red Text</span>
/**
[list]
[*]Entry 1
[*]Entry 2
[/list]
[list]
*Entry 1
*Entry 2
[/list]
=> <ul><li>Entry 1</li><li>Entry 2</li></ul>
*/
/**
[table]
[tr]
[td]table 1[/td]
[td]table 2[/td]
[/tr]
[tr]
[td]table 3[/td]
[td]table 4[/td]
[/tr]
[/table]
=> <table><tr><td>table 1</td><td>table 2</td></tr><tr><td>table 3</td><td>table 4</td></tr></table>
*/
// [b]bolded text[/b] => <span style="font-weight: bold;">bolded text</span>
const processors = {
b: node => ({
tag: 'span',
attrs: {
style: 'font-weight: bold;',
},
content: node.content,
}),
i: node => ({
tag: 'span',
attrs: {
style: 'font-style: italic;',
},
content: node.content,
}),
};
module.exports = function html5Preset(opts = {}) {
return function process(tree) {
tree.walk((node) => {
if (node.tag && processors[node.tag]) {
return processors[node.tag](node, opts);
}
return node;
});
};
};
+37
View File
@@ -0,0 +1,37 @@
{
"name": "@bbob/preset-html5",
"version": "1.0.0",
"description": "HTML5 Preset to transform BBCode to HTML",
"main": "lib/index.js",
"directories": {
"lib": "lib"
},
"devDependencies": {
"@bbob/core": "*"
},
"repository": {
"type": "git",
"url": "git+https://github.com/JiLiZART/bbob.git"
},
"keywords": [
"bbcode",
"preset",
"html5",
"bbode",
"transform"
],
"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,15 @@
const preset = require('../lib/index');
const bbob = require('@bbob/core');
const processor = bbob([
preset(),
]);
describe('bbob-preset-html5', () => {
test('render [url]', () => {
const input = '[url=https://ru.wikipedia.org]Text[/url]';
const result = '<a href="https://ru.wikipedia.org">Text</a>';
expect(processor.process(input, { sync: true })).toBe(result);
});
});