Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(web): find matches after code blocks in markdown search (#6853)
gbirman7da8cd6579fc
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/find-and-replace/findAndReplacePlugin.test.ts b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/find-and-replace/findAndReplacePlugin.test.tsindex c4c67969a9..e59dd8159c 100644--- a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/find-and-replace/findAndReplacePlugin.test.ts+++ b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/find-and-replace/findAndReplacePlugin.test.ts@@ -1,11 +1,17 @@+import { $createListItemNode, $createListNode } from '@lexical/list';import {$createDocumentMentionNode,$createUserMentionNode,} from '@macro-inc/lexical-core';-import { SupportedNodeTypes } from '@macro-inc/lexical-core/node-list';+import {+ NodeReplacements,+ SupportedNodeTypes,+} from '@macro-inc/lexical-core/node-list';+import { markdownToSerializedEditorStateWithIds } from '@macro-inc/lexical-core/utils/markdown-state';import {$createParagraphNode,$createTextNode,+ $getNodeByKey,$getRoot,createEditor,type LexicalEditor,@@ -23,7 +29,7 @@ function createSearchEditor(): {} {const editor = createEditor({namespace: 'find-and-replace-plugin-test',- nodes: SupportedNodeTypes,+ nodes: [...SupportedNodeTypes, ...NodeReplacements],onError: (error) => {throw error;},@@ -130,3 +136,101 @@ describe('findAndReplacePlugin document mentions', () => {expect(getListOffset()).toEqual([]);});});++function searchMarkdown(markdown: string, query: string) {+ const { editor, getListOffset } = createSearchEditor();+ editor.setEditorState(+ editor.parseEditorState(markdownToSerializedEditorStateWithIds(markdown))+ );+ return editor.getEditorState().read(() => {+ editor.dispatchCommand(DO_SEARCH_COMMAND, query);+ return getListOffset().map((item) => ({+ text: $getNodeByKey(item.key)+ ?.getTextContent()+ .slice(item.offset.start, item.offset.end),+ isReplace: item.offset.isReplace,+ pairKey: item.pairKey,+ }));+ });+}++describe('findAndReplacePlugin multi-line blocks', () => {+ const markdown = [+ 'Intro paragraph.',+ '',+ '```graphql',+ 'type Calendar { id: ID!',+ '',+ ' isPrimary: Boolean! }',+ '```',+ '',+ '- **Fan-out:** a change is logged for every user.',+ ].join('\n');++ it('finds text inside a code block', () => {+ expect(searchMarkdown(markdown, 'isprimary')).toEqual([+ { text: 'isPrimary', isReplace: true, pairKey: 1 },+ ]);+ });++ it('finds text after a code block', () => {+ expect(searchMarkdown(markdown, 'fan-out')).toEqual([+ { text: 'Fan-out', isReplace: true, pairKey: 1 },+ ]);+ expect(searchMarkdown(markdown, 'every user')).toEqual([+ { text: 'every user', isReplace: true, pairKey: 1 },+ ]);+ });++ it('numbers every match in document order', () => {+ const matches = searchMarkdown(markdown, 'i');+ expect(matches.map((match) => match.text)).toEqual(+ matches.map(() => expect.stringMatching(/^i$/i))+ );+ expect(matches.map((match) => match.pairKey)).toEqual(+ matches.map((_, index) => index + 1)+ );+ expect(matches).toHaveLength(6);+ });++ it('splits a match across formatted text nodes', () => {+ expect(searchMarkdown('Say **hel**lo there', 'hello')).toEqual([+ { text: 'hel', isReplace: true, pairKey: 1 },+ { text: 'lo', isReplace: false, pairKey: 1 },+ ]);+ });++ it('does not match across block boundaries', () => {+ expect(searchMarkdown('first\n\nsecond', 'firstsecond')).toEqual([]);+ });+});++describe('findAndReplacePlugin nested blocks', () => {+ it('separates inline text from a block nested after it', () => {+ const { editor, getListOffset } = createSearchEditor();++ editor.update(+ () => {+ const parent = $createListItemNode();+ parent.append(+ $createTextNode('parent'),+ $createListNode('bullet').append(+ $createListItemNode().append($createTextNode('child'))+ )+ );+ $getRoot().clear().append($createListNode('bullet').append(parent));+ },+ { discrete: true }+ );++ editor.getEditorState().read(() => {+ editor.dispatchCommand(DO_SEARCH_COMMAND, 'parentchild');+ });+ expect(getListOffset()).toEqual([]);++ editor.getEditorState().read(() => {+ editor.dispatchCommand(DO_SEARCH_COMMAND, 'child');+ });+ expect(getListOffset()).toHaveLength(1);+ });+});diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/find-and-replace/findAndReplacePlugin.ts b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/find-and-replace/findAndReplacePlugin.tsindex 3d88415709..05ffe3ab03 100644--- a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/find-and-replace/findAndReplacePlugin.ts+++ b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/find-and-replace/findAndReplacePlugin.ts@@ -34,31 +34,15 @@ export interface SplitOffset {isReplace: boolean;}-interface HighlightNodes {- node: LexicalNode;- text: string;- length: number;- startIndex: number;-}-interface ReplacePayload {replaceString: string;nodeKeyOffsetList: NodekeyOffset[];}-interface StringLocation {- searchStart: number;- searchLength: number;-}--interface MatchedAndLocations {- matched: string;- location: number;-}--interface NoneBreakNode {- node: LexicalNode;- realLocations: MatchedAndLocations[];+interface TextSegment {+ key: NodeKey;+ start: number;+ end: number;}type FindAndReplaceProps = {@@ -66,10 +50,6 @@ type FindAndReplaceProps = {setListOffset: (listOffset: NodekeyOffset[]) => void;};-function isBetween(target: number, a: number, b: number) {- return (a - target) * (b - target) <= 0;-}-function shouldIgnoreNodeType(type: string) {// Document mentions (task chips, doc/channel refs, …) stay searchable via// getTextContent() so Ctrl+F matches their visible titles. Replace still@@ -88,364 +68,91 @@ function notReallyInlineTypes(type: string) {return false;}-function getPlusLength(child: LexicalNode): number {- let plus = 0;- if ($isElementNode(child)) {- plus = 1;- } else {- if (!child.getNextSibling()) {- plus = 1;- }- }- return plus;-}--function ciIndexOf(string: string, subString: string) {- return string.toUpperCase().indexOf(subString.toUpperCase());-}--function ciLastIndexOf(string: string, subString: string) {- return string.toUpperCase().lastIndexOf(subString.toUpperCase());-}--function preg_quote(str: string, delimiter: string) {- return (str + '').replace(- new RegExp(- '[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\' + (delimiter || '') + '-]',- 'g'- ),- '\\$&'- );-}--function locations(- searchStr: string,- str: string,- caseSensitive: boolean = false-) {- var searchStrLen = searchStr.length;- if (searchStrLen === 0) {- return [];- }- var startIndex = 0,- index: number = -1,- indices: number[] = [];- if (!caseSensitive) {- str = str.toLowerCase();- searchStr = searchStr.toLowerCase();- }- index = str.indexOf(searchStr, startIndex);- while (index > -1) {- indices.push(index);- startIndex = index + searchStrLen;- index = str.indexOf(searchStr, startIndex);- }- return indices;+function escapeRegExp(str: string) {+ return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');}-function traverseNodeGetText(root: ElementNode | LexicalNode): string {- if (!$isElementNode(root)) {- if (shouldIgnoreNodeType(root.__type)) {- return '';- }- return root.getTextContent();- }- let textContent = '';- const children = root.getChildren();- const childrenLength = children.length;- for (let i = 0; i < childrenLength; i++) {- const child = children[i];- if ($isElementNode(child)) {- textContent += traverseNodeGetText(child);- if (i !== childrenLength - 1 && !child.isInline()) {- textContent += '\n\n';- }- } else {- if (shouldIgnoreNodeType(child.__type)) {- continue;- }- textContent += child.getTextContent();- if (notReallyInlineTypes(child.__type)) {- textContent += '\n\n';- }+/**+ * Flattens the tree into one string and records where each leaf node's text+ * sits in it. Blocks, including ones nested after inline text such as a+ * sublist, are separated by a newline that belongs to no node, so a+ * match can never span two blocks.+ */+function collectTextSegments(root: ElementNode): {+ text: string;+ segments: TextSegment[];+} {+ let text = '';+ const segments: TextSegment[] = [];+ const visit = (node: LexicalNode) => {+ if (shouldIgnoreNodeType(node.getType())) return;+ if ($isElementNode(node)) {+ const isBlock = !node.isInline();+ if (isBlock && text.length > 0 && !text.endsWith('\n')) text += '\n';+ for (const child of node.getChildren()) visit(child);+ if (isBlock) text += '\n';+ return;}- }- return textContent;-}--function highlightMultipleNode(- targetNode: LexicalNode,- searchString: string,- params: { startIndex: number; totalLength: number }-): HighlightNodes[] {- const results: HighlightNodes[] = [];- if (shouldIgnoreNodeType(targetNode.__type)) {- return results;- }-- if ($isElementNode(targetNode)) {- let children = targetNode.getChildren();- for (let i = 0; i < children.length; i++) {- let child = children[i];- if (shouldIgnoreNodeType(child.__type)) {- continue;- }- if ($isElementNode(child)) {- const subNodes = highlightMultipleNode(child, searchString, params);- results.push(...subNodes);- } else {- results.push({- node: child,- text: child.getTextContent(),- length: child.getTextContent().length,- startIndex: params.startIndex,- });- params.startIndex += child.getTextContent().length;- }+ const content = node.getTextContent();+ if (content.length > 0) {+ segments.push({+ key: node.getKey(),+ start: text.length,+ end: text.length + content.length,+ });+ text += content;}- } else {- results.push({- node: targetNode,- text: targetNode.getTextContent(),- length: targetNode.getTextContent().length,- startIndex: params.startIndex,- });- params.startIndex += targetNode.getTextContent().length;- }- return results;+ if (notReallyInlineTypes(node.getType())) text += '\n';+ };+ visit(root);+ return { text, segments };}-function getNoneBreakNodes(+/**+ * Case-insensitive, non-overlapping matches mapped onto the nodes they cover.+ * The first piece of each match is the one a replace rewrites; the remaining+ * pieces of a match split across nodes are deleted.+ */+function findMatchOffsets(root: ElementNode,- searchString: string,- indexes: Array<number>,- paramsObj: { currentMatchIndex: number; previousLength: number }-): NoneBreakNode[] {- const nodes: NoneBreakNode[] = [];- let child: LexicalNode | null = root.getFirstChild();- const endPartial: string[] = [];- const beginPartial: string[] = [];- let consummedSearchLength = 0;- let skipLineBreak = false;- let plus = 0;- let isFirstChild = child !== null;- while (- child !== null &&- (paramsObj.currentMatchIndex < indexes.length || endPartial.length > 0)- ) {- if (child && shouldIgnoreNodeType(child.__type)) {- child = child.getNextSibling();- isFirstChild = false;- continue;- }-- const childTxt: string = traverseNodeGetText(child);- if (childTxt) {- const match = /\r|\n/.exec(childTxt);- if (!match) {- let containedIndexes: number[] = [];- if (endPartial.length > 0) {- const beginMatched = searchString.substring(- endPartial[0].length + consummedSearchLength,- searchString.length- );- if (- ciIndexOf(childTxt, beginMatched) === 0 ||- ciIndexOf(beginMatched, childTxt) === 0- ) {- if (beginMatched.length === childTxt.length) {- endPartial.push(beginMatched);- consummedSearchLength += endPartial[0].length;- } else if (beginMatched.length > childTxt.length) {- endPartial.push(childTxt);- consummedSearchLength += endPartial[0].length;- } else {- if (beginMatched.length > 0) {- beginPartial.push(beginMatched);- }- consummedSearchLength = 0;- }- } else {- consummedSearchLength = 0;- }- endPartial.splice(0, 1);- } else {- consummedSearchLength = 0;- }- while (- isBetween(- indexes[paramsObj.currentMatchIndex],- paramsObj.previousLength,- paramsObj.previousLength + childTxt.length - 1- )- ) {- consummedSearchLength = 0;- if (- isBetween(- indexes[paramsObj.currentMatchIndex] + searchString.length - 1,- paramsObj.previousLength,- paramsObj.previousLength + childTxt.length - 1- )- ) {- containedIndexes.push(paramsObj.currentMatchIndex);- } else {- const matchedPart = childTxt.substring(- indexes[paramsObj.currentMatchIndex] - paramsObj.previousLength,- childTxt.length- );- endPartial.push(matchedPart);- }- paramsObj.currentMatchIndex += 1;- }- if (- containedIndexes.length > 0 ||- endPartial.length > 0 ||- beginPartial.length > 0- ) {- const matchedAndLocations: MatchedAndLocations[] = [];- locations(searchString, childTxt).map(function (idx) {- matchedAndLocations.push({ matched: searchString, location: idx });- });- endPartial.map(function (mStr) {- const lastIdx = ciLastIndexOf(childTxt, mStr);- matchedAndLocations.push({ matched: mStr, location: lastIdx });- });- beginPartial.map(function (mStr) {- const firstIdx = ciIndexOf(childTxt, mStr);- matchedAndLocations.push({ matched: mStr, location: firstIdx });- });- beginPartial.splice(0, beginPartial.length);- nodes.push({ node: child, realLocations: matchedAndLocations });- }- plus = getPlusLength(child);- paramsObj.previousLength += childTxt.length + plus;- skipLineBreak = false;- } else {- if ($isElementNode(child)) {- const subChildrenNodes = getNoneBreakNodes(- child,- searchString,- indexes,- paramsObj- );- nodes.push(...subChildrenNodes);- } else {- skipLineBreak =- skipLineBreak || isFirstChild || paramsObj.previousLength === 0;- if (!skipLineBreak) {- paramsObj.previousLength += 1;- skipLineBreak = true;- }- }- consummedSearchLength = 0;- }- } else {- consummedSearchLength = 0;- }- child = child.getNextSibling();- isFirstChild = false;- }- return nodes;-}--function updatedNodeOffset(- node: HighlightNodes,- stringLocations: StringLocation[],- nodeOffsets: SplitOffset[],- additionLocations: StringLocation[],- mainIndex: null | { currentIndex: number } = null-) {- const relativeStart = node.startIndex;- const relativeEnd = node.startIndex + node.length - 1;-- if (mainIndex === null) {- const relativeSearchStringStart = stringLocations[0].searchStart;- const relativeSearchStringEnd =- relativeSearchStringStart + stringLocations[0].searchLength - 1;- if (isBetween(relativeSearchStringStart, relativeStart, relativeEnd)) {- const realStart = relativeSearchStringStart - relativeStart;- let realEnd: number;- if (!isBetween(relativeSearchStringEnd, relativeStart, relativeEnd)) {- const newLocation = relativeEnd + 1;- const remainSearchLength = relativeSearchStringEnd - relativeEnd;- additionLocations.push({- searchStart: newLocation,- searchLength: remainSearchLength,- });- realEnd =- realStart + stringLocations[0].searchLength - remainSearchLength;- } else {- realEnd = realStart + stringLocations[0].searchLength;- }- additionLocations.splice(0, 1);- nodeOffsets.push({ start: realStart, end: realEnd, isReplace: false });- }- } else {+ searchString: string+): NodekeyOffset[] {+ const { text, segments } = collectTextSegments(root);+ const regex = new RegExp(escapeRegExp(searchString), 'gi');+ const offsets: NodekeyOffset[] = [];+ let segmentIndex = 0;+ let pairKey = 0;+ for (const found of text.matchAll(regex)) {+ const matchStart = found.index;+ const matchEnd = matchStart + found[0].length;+ if (matchEnd === matchStart) break;+ pairKey += 1;while (- mainIndex.currentIndex < stringLocations.length &&- isBetween(- stringLocations[mainIndex.currentIndex].searchStart,- relativeStart,- relativeEnd- )+ segmentIndex < segments.length &&+ segments[segmentIndex].end <= matchStart) {- const relativeSearchStringStart =- stringLocations[mainIndex.currentIndex].searchStart;- const realStart = relativeSearchStringStart - relativeStart;- const relativeSearchStringEnd =- relativeSearchStringStart +- stringLocations[mainIndex.currentIndex].searchLength -- 1;- if (!isBetween(relativeSearchStringEnd, relativeStart, relativeEnd)) {- const remainSearchLength = relativeSearchStringEnd - relativeEnd;- const realEnd =- realStart +- stringLocations[mainIndex.currentIndex].searchLength -- remainSearchLength;- nodeOffsets.push({ start: realStart, end: realEnd, isReplace: true });- const newLocation = relativeEnd + 1;- additionLocations.push({- searchStart: newLocation,- searchLength: remainSearchLength,- });- mainIndex.currentIndex += 1;- return;- } else {- const realEnd = realStart + stringLocations[0].searchLength;- nodeOffsets.push({ start: realStart, end: realEnd, isReplace: true });- }- mainIndex.currentIndex += 1;+ segmentIndex += 1;}- }-}--function buildNodeKeyOffsetList(- hNode: HighlightNodes,- additionLocations: StringLocation[],- originalLocations: StringLocation[],- mainIndex: { currentIndex: number },- nodeKeyOffsetList: NodekeyOffset[]-) {- const nodeOffsets: SplitOffset[] = [];- if (additionLocations.length > 0) {- updatedNodeOffset(hNode, additionLocations, nodeOffsets, additionLocations);- }- updatedNodeOffset(- hNode,- originalLocations,- nodeOffsets,- additionLocations,- mainIndex- );- if (nodeOffsets.length > 0) {- nodeOffsets.map((offset) => {- nodeKeyOffsetList.push({- key: hNode.node.__key,- offset: offset,- pairKey: undefined,+ let isReplace = true;+ for (+ let i = segmentIndex;+ i < segments.length && segments[i].start < matchEnd;+ i+++ ) {+ const segment = segments[i];+ offsets.push({+ key: segment.key,+ offset: {+ start: Math.max(matchStart, segment.start) - segment.start,+ end: Math.min(matchEnd, segment.end) - segment.start,+ isReplace,+ },+ pairKey,});- });+ isReplace = false;+ }}+ return offsets;}function selectNextKey(key: NodeKey) {@@ -586,58 +293,7 @@ function registerFindAndReplacePlugin(editor.registerCommand(DO_SEARCH_COMMAND,(searchString: string) => {- const root = $getRoot();- const regex = new RegExp(preg_quote(searchString, '/'), 'gi');- var rootText = traverseNodeGetText(root);- rootText = rootText.replace(/\n+/g, '\n').replace(/^(\n)+/, '');-- const indexes: number[] = [];- let found = regex.exec(rootText);- while (found) {- indexes.push(found.index);- found = regex.exec(rootText);- }- const nodes = getNoneBreakNodes(root, searchString, indexes, {- currentMatchIndex: 0,- previousLength: 0,- });-- const nodeKeyOffsetList: NodekeyOffset[] = [];- nodes.map((node) => {- const hightlightTarget = highlightMultipleNode(- node.node,- searchString,- { startIndex: 0, totalLength: node.node.getTextContent().length }- );- const originalLocations: StringLocation[] = node.realLocations.map(- (rLocation) => {- return {- searchStart: rLocation.location,- searchLength: rLocation.matched.length,- };- }- );- const additionLocations: StringLocation[] = [];- const mainIndex = { currentIndex: 0 };- hightlightTarget.map((hNode) => {- buildNodeKeyOffsetList(- hNode,- additionLocations,- originalLocations,- mainIndex,- nodeKeyOffsetList- );- });- });- let previousPairKey = 0;- nodeKeyOffsetList.map(function (item) {- if (item.offset.isReplace) {- previousPairKey += 1;- item.pairKey = previousPairKey;- } else {- item.pairKey = previousPairKey;- }- });+ const nodeKeyOffsetList = findMatchOffsets($getRoot(), searchString);updateListOffset(nodeKeyOffsetList);return true;},