MCPcopy Create free account
hub / github.com/HubSpot/draft-convert / joinChunks

Function joinChunks

src/convertFromHTML.js:231–269  ·  view source on GitHub ↗
(A, B, flat = false)

Source from the content-addressed store, hash-verified

229}
230
231function joinChunks(A, B, flat = false) {
232 // Sometimes two blocks will touch in the DOM and we need to strip the
233 // extra delimiter to preserve niceness.
234 const firstInB = B.text.slice(0, 1);
235 const lastInA = A.text.slice(-1);
236
237 const adjacentDividers = lastInA === '\r' && firstInB === '\r';
238 const isJoiningBlocks = A.text !== '\r' && B.text !== '\r'; // when joining two full blocks like this we want to pop one divider
239 const addingNewlineToEmptyBlock =
240 A.text === '\r' && !A.isNewline && B.isNewline; // when joining a newline to an empty block we want to remove the newline
241
242 if (adjacentDividers && (isJoiningBlocks || addingNewlineToEmptyBlock)) {
243 A.text = A.text.slice(0, -1);
244 A.inlines.pop();
245 A.entities.pop();
246 A.blocks.pop();
247 }
248
249 // Kill whitespace after blocks if flat mode is on
250 if (A.text.slice(-1) === '\r' && flat === true) {
251 if (B.text === SPACE || B.text === '\n') {
252 return A;
253 } else if (firstInB === SPACE || firstInB === '\n') {
254 B.text = B.text.slice(1);
255 B.inlines.shift();
256 B.entities.shift();
257 }
258 }
259
260 const isNewline = A.text.length === 0 && B.isNewline;
261
262 return {
263 text: A.text + B.text,
264 inlines: A.inlines.concat(B.inlines),
265 entities: A.entities.concat(B.entities),
266 blocks: A.blocks.concat(B.blocks),
267 isNewline,
268 };
269}
270
271/*
272 * Check to see if we have anything like <p> <blockquote> <h1>... to create

Callers 1

genFragmentFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…