Skip to content

Commit 80dee2d

Browse files
committed
group(block) rendering changes:
* move group rendering to a separate method * depth is now passed along keys and data as a second param for block rederers
1 parent e33ecc8 commit 80dee2d

File tree

5 files changed

+61
-39
lines changed

5 files changed

+61
-39
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,12 @@ const renderers = {
6666
'header-one': (children) => children.map(child => <h1>{child}</h1>),
6767
'header-two': (children) => children.map(child => <h2>{child}</h2>),
6868
// You can also access the original keys of the blocks
69-
'code-block': (children, depth, { keys }) => <pre style={styles.codeBlock} key={keys[0]} >{addBreaklines(children)}</pre>,
70-
'unordered-list-item': (children, depth, { keys }) => <ul key={keys[keys.length - 1]} class={`ul-level-${depth}`}>{children.map(child => <li>{child}</li>)}</ul>,
71-
'ordered-list-item': (children, depth, { keys }) => <ol key={keys.join('|')} class={`ol-level-${depth}`}>{children.map((child, index)=> <li key={keys[index]}>{child}</li>)}</ol>,
69+
'code-block': (children, { keys }) => <pre style={styles.codeBlock} key={keys[0]} >{addBreaklines(children)}</pre>,
70+
// or depth for nested lists
71+
'unordered-list-item': (children, { depth, keys }) => <ul key={keys[keys.length - 1]} class={`ul-level-${depth}`}>{children.map(child => <li>{child}</li>)}</ul>,
72+
'ordered-list-item': (children, { depth, keys }) => <ol key={keys.join('|')} class={`ol-level-${depth}`}>{children.map((child, index)=> <li key={keys[index]}>{child}</li>)}</ol>,
7273
// If your blocks use meta data it can also be accessed like keys
73-
atomic: (children, depth, { keys, data }) => children.map((child, i) => <Atomic key={keys[i] {...data[i]} />),
74+
atomic: (children, { keys, data }) => children.map((child, i) => <Atomic key={keys[i] {...data[i]} />),
7475
},
7576
/**
7677
* Entities receive children and the entity data

example/src/Preview/Preview.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ const inline = {
3131
const addBreaklines = children => children.map(child => [child, <br />]);
3232

3333
const getList = ordered =>
34-
(children, depth, { keys }) => (
34+
(children, { depth, keys }) => (
3535
<List key={keys[0]} keys={keys} depth={depth} ordered={ordered}>
3636
{children.map((child, i) => <li key={keys[i]} >{child}</li>)}
3737
</List>
3838
);
3939

40-
const getAtomic = (children, _, { data, keys }) => data.map(
40+
const getAtomic = (children, { data, keys }) => data.map(
4141
(item, i) => <AtomicBlock key={keys[i]} {...data[i]} />
4242
);
4343

@@ -47,17 +47,17 @@ const getAtomic = (children, _, { data, keys }) => data.map(
4747
const blocks = {
4848
// Rendering blocks like this along with cleanup results in a single p tag for each paragraph
4949
// adding an empty block closes current paragraph and starts a new one
50-
unstyled: (children, _, { keys }) => <p key={keys[0]}>{addBreaklines(children)}</p>,
50+
unstyled: (children, { keys }) => <p key={keys[0]}>{addBreaklines(children)}</p>,
5151
atomic: getAtomic,
5252
blockquote:
53-
(children, _, { keys }) => <blockquote key={keys[0]} >{addBreaklines(children)}</blockquote>,
54-
'header-one': (children, _, { keys }) => children.map((child, i) => <h1 key={keys[i]}>{child}</h1>),
55-
'header-two': (children, _, { keys }) => children.map((child, i) => <h2 key={keys[i]}>{child}</h2>),
56-
'header-three': (children, _, { keys }) => children.map((child, i) => <h3 key={keys[i]}>{child}</h3>),
57-
'header-four': (children, _, { keys }) => children.map((child, i) => <h4 key={keys[i]}>{child}</h4>),
58-
'header-five': (children, _, { keys }) => children.map((child, i) => <h5 key={keys[i]}>{child}</h5>),
59-
'header-six': (children, _, { keys }) => children.map((child, i) => <h6 key={keys[i]}>{child}</h6>),
60-
'code-block': (children, _, { keys }) => <pre key={keys[0]} style={styles.codeBlock}>{addBreaklines(children)}</pre>,
53+
(children, { keys }) => <blockquote key={keys[0]} >{addBreaklines(children)}</blockquote>,
54+
'header-one': (children, { keys }) => children.map((child, i) => <h1 key={keys[i]}>{child}</h1>),
55+
'header-two': (children, { keys }) => children.map((child, i) => <h2 key={keys[i]}>{child}</h2>),
56+
'header-three': (children, { keys }) => children.map((child, i) => <h3 key={keys[i]}>{child}</h3>),
57+
'header-four': (children, { keys }) => children.map((child, i) => <h4 key={keys[i]}>{child}</h4>),
58+
'header-five': (children, { keys }) => children.map((child, i) => <h5 key={keys[i]}>{child}</h5>),
59+
'header-six': (children, { keys }) => children.map((child, i) => <h6 key={keys[i]}>{child}</h6>),
60+
'code-block': (children, { keys }) => <pre key={keys[0]} style={styles.codeBlock}>{addBreaklines(children)}</pre>,
6161
'unordered-list-item': getList(),
6262
'ordered-list-item': getList(true),
6363
};

src/render.js

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,32 @@ const byDepth = (blocks) => {
132132
return group;
133133
};
134134

135+
/**
136+
* Conditionaly render a group if its not empty,
137+
* pass all the params to the renderers
138+
*/
139+
const renderGroup = (group, blockRenderers, rendered, params) => {
140+
const {
141+
prevType: type,
142+
prevDepth: depth,
143+
prevKeys: keys,
144+
prevData: data,
145+
} = params;
146+
// in case current group is empty it should not be rendered
147+
if (group.length === 0) {
148+
return;
149+
}
150+
if (blockRenderers[type]) {
151+
rendered.push(blockRenderers[type](group, {
152+
depth,
153+
keys,
154+
data,
155+
}));
156+
return;
157+
}
158+
rendered.push(group);
159+
};
160+
135161

136162
/**
137163
* Renders blocks grouped by type using provided blockStyleRenderers
@@ -167,17 +193,14 @@ const renderBlocks = (blocks, inlineRenderers = {}, blockRenderers = {},
167193
getKeyGenerator()
168194
);
169195
// if type of the block has changed or the split flag is set
170-
// render the block and clear group
196+
// render and clear group
171197
if ((prevType && prevType !== block.type) || splitGroup) {
172-
// in case current group is empty it should not be rendered
173-
if (blockRenderers[prevType] && group.length > 0) {
174-
rendered.push(blockRenderers[prevType](group, prevDepth, {
175-
keys: prevKeys,
176-
data: prevData,
177-
}));
178-
} else if (group.length > 0) {
179-
rendered.push(group);
180-
}
198+
renderGroup(
199+
group,
200+
blockRenderers,
201+
rendered,
202+
{ prevType, prevDepth, prevKeys, prevData }
203+
);
181204
// reset group vars
182205
// IDEA: might be worth to group those into an instance and just newup a new one
183206
prevData = [];
@@ -201,14 +224,12 @@ const renderBlocks = (blocks, inlineRenderers = {}, blockRenderers = {},
201224
prevData.push(block.data);
202225
});
203226
// render last group
204-
if (blockRenderers[prevType]) {
205-
rendered.push(blockRenderers[prevType](group, prevDepth, {
206-
keys: prevKeys,
207-
data: prevData,
208-
}));
209-
} else {
210-
rendered.push(group);
211-
}
227+
renderGroup(
228+
group,
229+
blockRenderers,
230+
rendered,
231+
{ prevType, prevDepth, prevKeys, prevData }
232+
);
212233
return checkJoin(rendered, options);
213234
};
214235

test/cleanup.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const atomicBlocks = {
2020
const blocks = {
2121
unstyled: (children) => `<p>${joinRecursively(children)}</p>`,
2222
blockquote: (children) => `<blockquote>${joinRecursively(children)}</blockquote>`,
23-
atomic: (children, _, { keys, data }) => {
23+
atomic: (children, { keys, data }) => {
2424
const maped = children.map(
2525
(child, i) => atomicBlocks[data[i].type](child, data[i], keys[i])
2626
);

test/render.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const atomicBlocks = {
2626

2727
const dataBlocks = {
2828
unstyled: (children) => `<p>${joinRecursively(children)}</p>`,
29-
atomic: (children, _, { keys, data }) => {
29+
atomic: (children, { keys, data }) => {
3030
const maped = children.map(
3131
(child, i) => atomicBlocks[data[i].type](child, data[i], keys[i])
3232
);
@@ -46,13 +46,13 @@ const renderers = {
4646
};
4747

4848
const blocksWithKeys = {
49-
unstyled: (children, depth, { keys }) => `<p key="${keys.join(',')}">${joinRecursively(children)}</p>`,
49+
unstyled: (children, { keys }) => `<p key="${keys.join(',')}">${joinRecursively(children)}</p>`,
5050
blockquote:
51-
(children, depth, { keys }) => `<blockquote key="${keys.join(',')}">${joinRecursively(children)}</blockquote>`,
51+
(children, { keys }) => `<blockquote key="${keys.join(',')}">${joinRecursively(children)}</blockquote>`,
5252
'ordered-list-item':
53-
(children, depth, { keys }) => `<ol key="${keys.join(',')}">${makeList(children)}</ol>`,
53+
(children, { keys }) => `<ol key="${keys.join(',')}">${makeList(children)}</ol>`,
5454
'unordered-list-item':
55-
(children, depth, { keys }) => `<ul key="${keys.join(',')}">${makeList(children)}</ul>`,
55+
(children, { keys }) => `<ul key="${keys.join(',')}">${makeList(children)}</ul>`,
5656
};
5757

5858
// render to HTML

0 commit comments

Comments
 (0)