2
0
mirror of https://github.com/tenrok/BBob.git synced 2026-05-15 11:59:37 +03:00

fix(react): render words and spaces as single node in react (#226)

* fix(react): render words and spaces as single node in react

* chore: create metal-toys-heal.md
This commit is contained in:
Nikolay Kost
2024-03-27 03:29:16 +02:00
committed by GitHub
parent e875c7832d
commit f1f9eb39da
3 changed files with 30 additions and 14 deletions
+16 -14
View File
@@ -3,7 +3,7 @@ import React from 'react';
import core from '@bbob/core';
import * as html from '@bbob/html';
import { isTagNode, isStringNode } from '@bbob/plugin-helper';
import { isTagNode, isEOL } from '@bbob/plugin-helper';
const toAST = (source, plugins, options) => core(plugins)
.process(source, {
@@ -22,26 +22,28 @@ function tagToReactElement(node, index) {
}
function renderToReactNodes(nodes) {
let content = '';
const els = [].concat(nodes).reduce((arr, node, index) => {
if (isTagNode(node)) {
if (content !== '') {
arr.push(content);
content = '';
}
arr.push(tagToReactElement(node, index));
} else if (isStringNode(node)) {
if (content === '') {
content = node;
} else {
content += node;
}
return arr;
}
if (index === nodes.length - 1 && content !== '') {
arr.push(content);
if (isEOL(node)) {
arr.push(node);
return arr;
}
const lastIdx = arr.length - 1;
const prevNode = lastIdx >= 0 ? arr[lastIdx] : null;
if (prevNode !== null && !isTagNode(prevNode) && !isEOL(prevNode)) {
const prevArr = arr; // stupid eslint
prevArr[lastIdx] += node;
return prevArr;
}
arr.push(node);
return arr;
}, []);
return els;
+7
View File
@@ -15,6 +15,13 @@ describe('@bbob/react render', () => {
test('render simple text nodes', () => {
const html = render('some example words');
expect(html[0]).toStrictEqual("some example words")
})
test('render simple text nodes with line break', () => {
const html = render(`some
example
words`);
expect(html[0]).toStrictEqual("some")
})
})