MCPcopy Create free account
hub / github.com/uiwjs/react-md-editor / insertBeforeEachLine

Function insertBeforeEachLine

core/src/utils/markdownUtils.ts:160–191  ·  view source on GitHub ↗
(
  selectedText: string,
  insertBefore: string | AlterLineFunction,
)

Source from the content-addressed store, hash-verified

158 * Inserts insertionString before each line
159 */
160export function insertBeforeEachLine(
161 selectedText: string,
162 insertBefore: string | AlterLineFunction,
163): { modifiedText: string; insertionLength: number } {
164 const lines = selectedText.split(/\n/);
165
166 let insertionLength = 0;
167 const modifiedText = lines
168 .map((item, index) => {
169 if (typeof insertBefore === 'string') {
170 if (item.startsWith(insertBefore)) {
171 insertionLength -= insertBefore.length;
172 return item.slice(insertBefore.length);
173 }
174 insertionLength += insertBefore.length;
175 return insertBefore + item;
176 }
177 if (typeof insertBefore === 'function') {
178 if (item.startsWith(insertBefore(item, index))) {
179 insertionLength -= insertBefore(item, index).length;
180 return item.slice(insertBefore(item, index).length);
181 }
182 const insertionResult = insertBefore(item, index);
183 insertionLength += insertionResult.length;
184 return insertBefore(item, index) + item;
185 }
186 throw Error('insertion is expected to be either a string or a function');
187 })
188 .join('\n');
189
190 return { modifiedText, insertionLength };
191}

Callers 3

handleKeyDownFunction · 0.90
quote.tsxFile · 0.90
makeListFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…