test
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

thanks

12Me21 af5f3e73 f706bf2e

+103
+103
test.html
··· 64 64 res = process(ex) 65 65 document.write(res+"<hr>") 66 66 </script> 67 + 68 + <script> 69 + function flattenStyledTree(tree, currentStyles = []) { 70 + const result = []; 71 + 72 + for (let i = 0; i < tree.length; i++) { 73 + const item = tree[i]; 74 + const itemIsFirst = i === 0; 75 + const itemIsLast = i === tree.length - 1; 76 + 77 + if (typeof item === 'string') { 78 + let text = item 79 + // Base case: it's a text node 80 + const styles = currentStyles.map(({style, surround, isFirstChild, isLastChild}) => { 81 + let strip = [0,0] 82 + if (isFirstChild && itemIsFirst) { 83 + strip[0] = surround[0].length 84 + text = surround[0] + text 85 + } 86 + if (isLastChild && itemIsLast) { 87 + strip[1] = surround[1].length 88 + text = text + surround[1] 89 + } 90 + return {style, strip}; 91 + }); 92 + result.push({ 93 + text, 94 + features: styles, 95 + }); 96 + } else { 97 + // It's a styled node - recurse with updated style tracking 98 + const newStyles = [ 99 + ...currentStyles.map(s => ({ 100 + ...s, 101 + isFirstChild: s.isFirstChild && itemIsFirst, 102 + isLastChild: s.isLastChild && itemIsLast 103 + })), 104 + {style: item.style, surround: item.surround, isFirstChild: true, isLastChild: true} 105 + ]; 106 + const flattened = flattenStyledTree(item.children, newStyles); 107 + result.push(...flattened); 108 + } 109 + } 110 + 111 + return result; 112 + } 113 + 114 + // Example usage: 115 + const input = [ 116 + {style:"italic", surround:["/","/"], children:[ 117 + "abc", 118 + {style:"bold", surround:["**","**"], children: [ 119 + "def", 120 + ]}, 121 + "ghi", 122 + ]}, 123 + ]; 124 + 125 + const output = flattenStyledTree(input); 126 + console.log(JSON.stringify(output, null, 2)); 127 + // Expected output: 128 + // [ 129 + // {text: "abc", styles: [{style: "italic", side: "begin"}]}, 130 + // {text: "def", styles: [{style: "italic", side: "end"}, {style: "bold", side: "end"}]} 131 + // ] 132 + </script> 133 + 134 + <!--<script> 135 + let tr = [ 136 + {type:"italic", surround:["*","*"], children:[ 137 + "abc", 138 + {type:"bold", surround:["**","**"], children: [ 139 + "def", 140 + ]}, 141 + ]}, 142 + ] 143 + function gen(tree) { 144 + let str = "" 145 + let facets = [] 146 + let open = null 147 + function rec(tree) { 148 + for (let x of tree) { 149 + if ("string"==typeof x) { 150 + str += x 151 + } else { 152 + let start = str.length 153 + if (open) { 154 + open.end = 155 + } 156 + open = {start,style:x.type,strip:[x.surround[0].length,0]} 157 + str += x.surround[0] 158 + str += gen(x.children) 159 + str += x.surround[1] 160 + facets.push() 161 + } 162 + } 163 + } 164 + rec(tree) 165 + return str 166 + } 167 + console.log(gen(tr)) 168 + </script> 169 + -->