MCPcopy Index your code
hub / github.com/HubSpot/draft-convert / convertToHTML

Function convertToHTML

src/convertToHTML.js:22–138  ·  view source on GitHub ↗
({
  styleToHTML = {},
  blockToHTML = {},
  entityToHTML = defaultEntityToHTML,
})

Source from the content-addressed store, hash-verified

20};
21
22const convertToHTML = ({
23 styleToHTML = {},
24 blockToHTML = {},
25 entityToHTML = defaultEntityToHTML,
26}) => contentState => {
27 invariant(
28 contentState !== null && contentState !== undefined,
29 'Expected contentState to be non-null'
30 );
31
32 let getBlockHTML;
33 if (blockToHTML.__isMiddleware === true) {
34 getBlockHTML = blockToHTML(blockTypeObjectFunction(defaultBlockHTML));
35 } else {
36 getBlockHTML = accumulateFunction(
37 blockTypeObjectFunction(blockToHTML),
38 blockTypeObjectFunction(defaultBlockHTML)
39 );
40 }
41
42 const rawState = convertToRaw(contentState);
43
44 let listStack = [];
45
46 let result = rawState.blocks
47 .map(block => {
48 const { type, depth } = block;
49
50 let closeNestTags = '';
51 let openNestTags = '';
52
53 const blockHTMLResult = getBlockHTML(block);
54 if (!blockHTMLResult) {
55 throw new Error(
56 `convertToHTML: missing HTML definition for block with type ${
57 block.type
58 }`
59 );
60 }
61
62 if (!blockHTMLResult.nest) {
63 // this block can't be nested, so reset all nesting if necessary
64 closeNestTags = listStack.reduceRight((string, nestedBlock) => {
65 return (
66 string +
67 getNestedBlockTags(getBlockHTML(nestedBlock), depth).nestEnd
68 );
69 }, '');
70 listStack = [];
71 } else {
72 while (
73 depth + 1 !== listStack.length ||
74 type !== listStack[depth].type
75 ) {
76 if (depth + 1 === listStack.length) {
77 // depth is right but doesn't match type
78 const blockToClose = listStack[depth];
79 closeNestTags += getNestedBlockTags(

Callers 3

convertToHTML.jsFile · 0.85

Calls 3

blockToHTMLFunction · 0.85
getNestedBlockTagsFunction · 0.85
getBlockTagsFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…