diff --git a/packages/richtext-lexical/src/features/blocks/client/markdownTransformer.ts b/packages/richtext-lexical/src/features/blocks/client/markdownTransformer.ts index 5b2d7a45f..ea492f3d0 100644 --- a/packages/richtext-lexical/src/features/blocks/client/markdownTransformer.ts +++ b/packages/richtext-lexical/src/features/blocks/client/markdownTransformer.ts @@ -92,7 +92,7 @@ export const getBlockMarkdownTransformers = ({ const childrenString = linesInBetween.join('\n').trim() - const propsString: null | string = openMatch?.length > 2 ? openMatch[2]?.trim() : null + const propsString = openMatch[2]?.trim() const markdownToLexical = getMarkdownToLexical(allNodes, allTransformers) diff --git a/packages/richtext-lexical/src/features/blocks/server/linesFromMatchToContentAndPropsString.ts b/packages/richtext-lexical/src/features/blocks/server/linesFromMatchToContentAndPropsString.ts index e99c132c9..eb56719f1 100644 --- a/packages/richtext-lexical/src/features/blocks/server/linesFromMatchToContentAndPropsString.ts +++ b/packages/richtext-lexical/src/features/blocks/server/linesFromMatchToContentAndPropsString.ts @@ -38,17 +38,17 @@ export function linesFromStartToContentAndPropsString({ let isSelfClosing = false let isWithinCodeBlockAmount = 0 - const beforeStartLine = linesCopy[0].slice(0, startMatch.index) + const beforeStartLine = linesCopy[0]!.slice(0, startMatch.index) let endlineLastCharIndex = 0 let endLineIndex = startLineIndex - mainLoop: for (let lineIndex = 0; lineIndex < linesCopy.length; lineIndex++) { - const line = trimChildren ? linesCopy[lineIndex].trim() : linesCopy[lineIndex] + mainLoop: for (const [lineIndex, lineCopy] of linesCopy.entries()) { + const line = trimChildren ? lineCopy.trim() : lineCopy let amountOfBeginningSpacesRemoved = 0 if (trimChildren) { - for (let i = 0; i < linesCopy[lineIndex].length; i++) { - if (linesCopy[lineIndex][i] === ' ') { + for (let i = 0; i < lineCopy.length; i++) { + if (lineCopy[i] === ' ') { amountOfBeginningSpacesRemoved++ } else { break @@ -159,7 +159,7 @@ export function linesFromStartToContentAndPropsString({ } } - const afterEndLine = linesCopy[endLineIndex].trim().slice(endlineLastCharIndex) + const afterEndLine = linesCopy[endLineIndex]!.trim().slice(endlineLastCharIndex) return { afterEndLine, diff --git a/packages/richtext-lexical/src/features/blocks/server/markdownTransformer.ts b/packages/richtext-lexical/src/features/blocks/server/markdownTransformer.ts index ae40bc735..c57323790 100644 --- a/packages/richtext-lexical/src/features/blocks/server/markdownTransformer.ts +++ b/packages/richtext-lexical/src/features/blocks/server/markdownTransformer.ts @@ -361,18 +361,19 @@ function getMarkdownTransformerForBlock( if (beforeStartLine?.length) { prevNodes = markdownToLexical({ markdown: beforeStartLine })?.root?.children ?? [] - if (prevNodes?.length) { - rootNode.append($parseSerializedNode(prevNodes[0])) + const firstPrevNode = prevNodes?.[0] + if (firstPrevNode) { + rootNode.append($parseSerializedNode(firstPrevNode)) } } rootNode.append(node) if (afterEndLine?.length) { - nextNodes = markdownToLexical({ markdown: afterEndLine })?.root?.children ?? [] + nextNodes = markdownToLexical({ markdown: afterEndLine })?.root?.children const lastChild = rootNode.getChildren()[rootNode.getChildren().length - 1] - const children = ($parseSerializedNode(nextNodes[0]) as ElementNode)?.getChildren() + const children = ($parseSerializedNode(nextNodes[0]!) as ElementNode)?.getChildren() if (children?.length) { for (const child of children) { ;(lastChild as ElementNode).append(child) @@ -408,7 +409,7 @@ function getMarkdownTransformerForBlock( childrenString = linesInBetween.join('\n').trim() } - const propsString: null | string = openMatch?.length > 1 ? openMatch[1]?.trim() : null + const propsString = openMatch[1]?.trim() const markdownToLexical = getMarkdownToLexical(allNodes, allTransformers) diff --git a/packages/richtext-lexical/src/features/blocks/server/validate.ts b/packages/richtext-lexical/src/features/blocks/server/validate.ts index 546cb0fd8..c58f15c1e 100644 --- a/packages/richtext-lexical/src/features/blocks/server/validate.ts +++ b/packages/richtext-lexical/src/features/blocks/server/validate.ts @@ -44,7 +44,7 @@ export const blockValidationHOC = ( let errorPaths: string[] = [] for (const fieldKey in result) { - if (result[fieldKey].errorPaths) { + if (result[fieldKey]?.errorPaths) { errorPaths = errorPaths.concat(result[fieldKey].errorPaths) } } diff --git a/packages/richtext-lexical/src/features/experimental_table/client/plugins/TableCellResizerPlugin/index.tsx b/packages/richtext-lexical/src/features/experimental_table/client/plugins/TableCellResizerPlugin/index.tsx index 6a0a90a95..351312132 100644 --- a/packages/richtext-lexical/src/features/experimental_table/client/plugins/TableCellResizerPlugin/index.tsx +++ b/packages/richtext-lexical/src/features/experimental_table/client/plugins/TableCellResizerPlugin/index.tsx @@ -210,13 +210,15 @@ function TableCellResizer({ editor }: { editor: LexicalEditor }): JSX.Element { } const getCellColumnIndex = (tableCellNode: TableCellNode, tableMap: TableMapType) => { - for (let row = 0; row < tableMap.length; row++) { - for (let column = 0; column < tableMap[row].length; column++) { - if (tableMap[row][column].cell === tableCellNode) { - return column + let columnIndex: number | undefined + tableMap.forEach((row) => { + row.forEach((cell, columnIndexInner) => { + if (cell.cell === tableCellNode) { + columnIndex = columnIndexInner } - } - } + }) + }) + return columnIndex } const updateColumnWidth = useCallback( diff --git a/packages/richtext-lexical/src/features/experimental_table/markdownTransformer.ts b/packages/richtext-lexical/src/features/experimental_table/markdownTransformer.ts index 7776842a2..d0c71a498 100644 --- a/packages/richtext-lexical/src/features/experimental_table/markdownTransformer.ts +++ b/packages/richtext-lexical/src/features/experimental_table/markdownTransformer.ts @@ -65,8 +65,12 @@ export const TableMarkdownTransformer: (props: { }, regExp: TABLE_ROW_REG_EXP, replace: (parentNode, _1, match) => { + const match0 = match[0] + if (!match0) { + return + } // Header row - if (TABLE_ROW_DIVIDER_REG_EXP.test(match[0])) { + if (TABLE_ROW_DIVIDER_REG_EXP.test(match0)) { const table = parentNode.getPreviousSibling() if (!table || !$isTableNode(table)) { return @@ -91,7 +95,7 @@ export const TableMarkdownTransformer: (props: { return } - const matchCells = mapToTableCells(match[0], allTransformers) + const matchCells = mapToTableCells(match0, allTransformers) if (matchCells == null) { return @@ -136,7 +140,7 @@ export const TableMarkdownTransformer: (props: { table.append(tableRow) for (let i = 0; i < maxCells; i++) { - tableRow.append(i < cells.length ? cells[i] : $createTableCell('', allTransformers)) + tableRow.append(i < cells.length ? cells[i]! : $createTableCell('', allTransformers)) } } diff --git a/packages/richtext-lexical/src/features/heading/markdownTransformer.ts b/packages/richtext-lexical/src/features/heading/markdownTransformer.ts index b157d855a..54aafba6d 100644 --- a/packages/richtext-lexical/src/features/heading/markdownTransformer.ts +++ b/packages/richtext-lexical/src/features/heading/markdownTransformer.ts @@ -28,7 +28,7 @@ export const MarkdownTransformer: (enabledHeadingSizes: HeadingTagType[]) => Ele }, regExp, replace: createBlockNode((match) => { - const tag = ('h' + match[1].length) as HeadingTagType + const tag = ('h' + match[1]?.length) as HeadingTagType return $createHeadingNode(tag) }), } diff --git a/packages/richtext-lexical/src/features/link/client/plugins/floatingLinkEditor/LinkEditor/index.tsx b/packages/richtext-lexical/src/features/link/client/plugins/floatingLinkEditor/LinkEditor/index.tsx index ee4d54cca..f205e69e7 100644 --- a/packages/richtext-lexical/src/features/link/client/plugins/floatingLinkEditor/LinkEditor/index.tsx +++ b/packages/richtext-lexical/src/features/link/client/plugins/floatingLinkEditor/LinkEditor/index.tsx @@ -408,7 +408,7 @@ export function LinkEditor({ anchorElem }: { anchorElem: HTMLElement }): React.R linkParent = getSelectedNode(selection).getParent() } else { if (selectedNodes.length) { - linkParent = selectedNodes[0].getParent() + linkParent = selectedNodes[0]?.getParent() ?? null } } diff --git a/packages/richtext-lexical/src/features/link/nodes/LinkNode.ts b/packages/richtext-lexical/src/features/link/nodes/LinkNode.ts index 6a3951983..ac53ee440 100644 --- a/packages/richtext-lexical/src/features/link/nodes/LinkNode.ts +++ b/packages/richtext-lexical/src/features/link/nodes/LinkNode.ts @@ -295,9 +295,9 @@ export function $toggleLink(payload: ({ fields: LinkFields } & LinkPayload) | nu if ($isLinkNode(parent)) { const children = parent.getChildren() - for (let i = 0; i < children.length; i += 1) { - parent.insertBefore(children[i]) - } + children.forEach((child) => { + parent.insertBefore(child) + }) parent.remove() } @@ -307,7 +307,7 @@ export function $toggleLink(payload: ({ fields: LinkFields } & LinkPayload) | nu } // Add or merge LinkNodes if (nodes?.length === 1) { - const firstNode = nodes[0] + const firstNode = nodes[0]! // if the first node is a LinkNode or if its // parent is a LinkNode, we update the URL, target and rel. const linkNode: LinkNode | null = $isLinkNode(firstNode) @@ -375,10 +375,7 @@ export function $toggleLink(payload: ({ fields: LinkFields } & LinkPayload) | nu } if (linkNode !== null) { const children = node.getChildren() - - for (let i = 0; i < children.length; i += 1) { - linkNode.append(children[i]) - } + linkNode.append(...children) } node.remove() diff --git a/packages/richtext-lexical/src/features/link/server/validate.ts b/packages/richtext-lexical/src/features/link/server/validate.ts index 03c18baa3..68cd925fa 100644 --- a/packages/richtext-lexical/src/features/link/server/validate.ts +++ b/packages/richtext-lexical/src/features/link/server/validate.ts @@ -36,7 +36,7 @@ export const linkValidation = ( let errorPaths: string[] = [] for (const fieldKey in result) { - if (result[fieldKey].errorPaths) { + if (result[fieldKey]?.errorPaths) { errorPaths = errorPaths.concat(result[fieldKey].errorPaths) } } diff --git a/packages/richtext-lexical/src/features/lists/shared/markdown.ts b/packages/richtext-lexical/src/features/lists/shared/markdown.ts index 2044d3fe6..3adb9af2c 100644 --- a/packages/richtext-lexical/src/features/lists/shared/markdown.ts +++ b/packages/richtext-lexical/src/features/lists/shared/markdown.ts @@ -34,7 +34,7 @@ export const listReplace = (listType: ListType): ElementTransformer['replace'] = } listItem.append(...children) listItem.select(0, 0) - const indent = Math.floor(match[1].length / LIST_INDENT_SIZE) + const indent = Math.floor(match[1]!.length / LIST_INDENT_SIZE) if (indent) { listItem.setIndent(indent) }