From 286677578d7e16f42b8c207f168b2076cee95673 Mon Sep 17 00:00:00 2001 From: Polina Novak Date: Sun, 19 Apr 2026 03:12:10 +0300 Subject: [PATCH 1/3] Added support for nested lists in markdown messages --- .changeset/real-ties-unite.md | 8 +++ .../room/composer/messageBox/MessageBox.tsx | 6 ++ .../markup/blocks/OrderedListBlock.tsx | 18 +++-- .../markup/blocks/UnorderedListBlock.tsx | 18 +++-- .../gazzodown/src/blocks/OrderedListBlock.tsx | 12 +++- .../src/blocks/UnorderedListBlock.tsx | 10 ++- packages/message-parser/src/definitions.ts | 1 + packages/message-parser/src/grammar.pegjs | 68 +++++++++++++++++-- packages/message-parser/tests/helpers.ts | 3 +- 9 files changed, 123 insertions(+), 21 deletions(-) create mode 100644 .changeset/real-ties-unite.md diff --git a/.changeset/real-ties-unite.md b/.changeset/real-ties-unite.md new file mode 100644 index 0000000000000..dea3a42ad27ab --- /dev/null +++ b/.changeset/real-ties-unite.md @@ -0,0 +1,8 @@ +--- +'@rocket.chat/message-parser': minor +'@rocket.chat/pdf-worker': minor +'@rocket.chat/gazzodown': minor +'@rocket.chat/meteor': minor +--- + +Add support for nested/multi-level lists in markdown diff --git a/apps/meteor/client/views/room/composer/messageBox/MessageBox.tsx b/apps/meteor/client/views/room/composer/messageBox/MessageBox.tsx index c653b0a132e55..a0f5caf847515 100644 --- a/apps/meteor/client/views/room/composer/messageBox/MessageBox.tsx +++ b/apps/meteor/client/views/room/composer/messageBox/MessageBox.tsx @@ -227,6 +227,12 @@ const MessageBox = ({ } switch (event.key) { + case 'Tab': { + event.preventDefault(); + chat.composer?.insertText(' '); + return; + } + case 'Escape': { closeEditing(event); if (!input.value.trim()) onEscape?.(); diff --git a/ee/packages/pdf-worker/src/templates/ChatTranscript/markup/blocks/OrderedListBlock.tsx b/ee/packages/pdf-worker/src/templates/ChatTranscript/markup/blocks/OrderedListBlock.tsx index 9bdcc0553f223..b57c2a08c70df 100644 --- a/ee/packages/pdf-worker/src/templates/ChatTranscript/markup/blocks/OrderedListBlock.tsx +++ b/ee/packages/pdf-worker/src/templates/ChatTranscript/markup/blocks/OrderedListBlock.tsx @@ -14,6 +14,9 @@ const styles = StyleSheet.create({ fontWeight: 700, marginHorizontal: 4, }, + nested: { + marginLeft: 16, + }, }); type OrderedListBlockProps = { @@ -22,10 +25,17 @@ type OrderedListBlockProps = { const OrderedListBlock = ({ items }: OrderedListBlockProps) => ( - {items.map(({ value, number }, index) => ( - - {number}. {value} - + {items.map(({ value, number, children }, index) => ( + + + {number}. {value} + + {children?.length && ( + + + + )} + ))} ); diff --git a/ee/packages/pdf-worker/src/templates/ChatTranscript/markup/blocks/UnorderedListBlock.tsx b/ee/packages/pdf-worker/src/templates/ChatTranscript/markup/blocks/UnorderedListBlock.tsx index 964c9023520f0..fbf8b38d9531e 100644 --- a/ee/packages/pdf-worker/src/templates/ChatTranscript/markup/blocks/UnorderedListBlock.tsx +++ b/ee/packages/pdf-worker/src/templates/ChatTranscript/markup/blocks/UnorderedListBlock.tsx @@ -14,6 +14,9 @@ const styles = StyleSheet.create({ bullet: { marginHorizontal: 4, }, + nested: { + marginLeft: 16, + }, }); type UnorderedListBlockProps = { @@ -21,10 +24,17 @@ type UnorderedListBlockProps = { }; const UnorderedListBlock = ({ items }: UnorderedListBlockProps) => ( - {items.map(({ value }, index) => ( - - • - {value} + {items.map(({ value, children }, index) => ( + + + • + {value} + + {children?.length && ( + + + + )} ))} diff --git a/packages/gazzodown/src/blocks/OrderedListBlock.tsx b/packages/gazzodown/src/blocks/OrderedListBlock.tsx index 6c6fc2734e34b..8981f5323fa57 100644 --- a/packages/gazzodown/src/blocks/OrderedListBlock.tsx +++ b/packages/gazzodown/src/blocks/OrderedListBlock.tsx @@ -5,13 +5,19 @@ import InlineElements from '../elements/InlineElements'; type OrderedListBlockProps = { items: MessageParser.ListItem[]; + nested?: boolean; }; -const OrderedListBlock = ({ items }: OrderedListBlockProps): ReactElement => ( -
    - {items.map(({ value, number }, index) => ( +const nestedListStyle = { + paddingInlineStart: '1.5rem', +} as const; + +const OrderedListBlock = ({ items, nested = false }: OrderedListBlockProps): ReactElement => ( +
      + {items.map(({ value, number, children }, index) => (
    1. {value} + {children?.length && }
    2. ))}
    diff --git a/packages/gazzodown/src/blocks/UnorderedListBlock.tsx b/packages/gazzodown/src/blocks/UnorderedListBlock.tsx index 500e525011107..9979d86fc7014 100644 --- a/packages/gazzodown/src/blocks/UnorderedListBlock.tsx +++ b/packages/gazzodown/src/blocks/UnorderedListBlock.tsx @@ -5,13 +5,19 @@ import InlineElements from '../elements/InlineElements'; type UnorderedListBlockProps = { items: MessageParser.ListItem[]; + nested?: boolean; }; -const UnorderedListBlock = ({ items }: UnorderedListBlockProps): ReactElement => ( -
      +const nestedListStyle = { + paddingInlineStart: '1.5rem', +} as const; + +const UnorderedListBlock = ({ items, nested = false }: UnorderedListBlockProps): ReactElement => ( +
        {items.map((item, index) => (
      • {item.value} + {item.children?.length && }
      • ))}
      diff --git a/packages/message-parser/src/definitions.ts b/packages/message-parser/src/definitions.ts index fdc7179b7c6b7..fe9e7777d1d09 100644 --- a/packages/message-parser/src/definitions.ts +++ b/packages/message-parser/src/definitions.ts @@ -17,6 +17,7 @@ export type ListItem = { type: 'LIST_ITEM'; value: Inlines[]; number?: number; + children?: ListItem[]; }; export type Tasks = { diff --git a/packages/message-parser/src/grammar.pegjs b/packages/message-parser/src/grammar.pegjs index 871c661e51a48..774edefc58da7 100644 --- a/packages/message-parser/src/grammar.pegjs +++ b/packages/message-parser/src/grammar.pegjs @@ -42,6 +42,48 @@ let skipBold = false; let skipItalic = false; let skipStrikethrough = false; let skipReferences = false; + +function buildNestedList(items) { + if (!items || !items.length) return items; + + const root = []; + const stack = [{ children: root, indent: -1 }]; + + for (const item of items) { + const indent = item.indent || 0; + + while (stack.length > 1 && stack[stack.length - 1].indent >= indent) { + stack.pop(); + } + + const cleanItem = { + type: item.type, + value: item.value, + ...(item.number !== undefined && { number: item.number }), + }; + + const parent = stack[stack.length - 1]; + parent.children.push(cleanItem); + + cleanItem.children = []; + + stack.push({ children: cleanItem.children, indent }); + } + + function cleanEmptyChildren(items) { + for (const item of items) { + if (item.children && !item.children.length) { + delete item.children; + } else if (item.children) { + cleanEmptyChildren(item.children); + } + } + } + + cleanEmptyChildren(root); + + return root; +} }} Start @@ -174,9 +216,13 @@ TaskFlag = "x" { return true; } / " " { return false; } * 3. Item Three * */ -OrderedList = items:OrderedListItem+ { return orderedList(items); } +OrderedList = items:OrderedListItem+ { return orderedList(buildNestedList(items)); } -OrderedListItem = number:Digits "." [ \t]+ text:Inline { return listItem(text, parseInt(number, 10)); } +OrderedListItem = indent:$[ \t]* number:Digits "." [ \t]+ text:Inline { + const item = listItem(text, parseInt(number, 10)); + item.indent = indent.length; + return item; +} /** * @@ -188,11 +234,19 @@ OrderedListItem = number:Digits "." [ \t]+ text:Inline { return listItem(text, p * * Item Four * */ -UnorderedList = items:(UnorderedListHyphenItem+ / UnorderedListAsteriskItem+) { return unorderedList(items); } - -UnorderedListHyphenItem = "-" [ \t]+ text:Inline { return listItem(text); } - -UnorderedListAsteriskItem = "*" [ \t]+ text:UnorderedListItemContent { return listItem(text); } +UnorderedList = items:(UnorderedListHyphenItem+ / UnorderedListAsteriskItem+) { return unorderedList(buildNestedList(items)); } + +UnorderedListHyphenItem = indent:$[ \t]* "-" [ \t]+ text:Inline { + const item = listItem(text); + item.indent = indent.length; + return item; +} + +UnorderedListAsteriskItem = indent:$[ \t]* "*" [ \t]+ text:UnorderedListItemContent { + const item = listItem(text); + item.indent = indent.length; + return item; +} UnorderedListItemContent = value:UnorderedListItemContentItem+ !"*" EndOfLine? { return reducePlainTexts(value); } diff --git a/packages/message-parser/tests/helpers.ts b/packages/message-parser/tests/helpers.ts index 8c8bf448f08a5..0f1e64f1b7b1c 100644 --- a/packages/message-parser/tests/helpers.ts +++ b/packages/message-parser/tests/helpers.ts @@ -57,10 +57,11 @@ export const mentionChannel = (value: string) => ({ export const orderedList = generate('ORDERED_LIST'); export const unorderedList = generate('UNORDERED_LIST'); -export const listItem = (value: unknown[], number?: number) => ({ +export const listItem = (value: unknown[], number?: number, children?: unknown[]) => ({ type: 'LIST_ITEM' as const, value, ...(number !== undefined ? { number } : {}), + ...(children !== undefined && children.length > 0 ? { children } : {}), }); export const mentionUser = (value: string) => ({ From 19e9d5eaf7cc52430a2d85b8b558682e97a33500 Mon Sep 17 00:00:00 2001 From: Polina Novak Date: Wed, 22 Apr 2026 19:42:44 +0300 Subject: [PATCH 2/3] Added support for nested lists in markdown messages --- .../client/views/room/composer/messageBox/MessageBox.tsx | 6 ------ 1 file changed, 6 deletions(-) diff --git a/apps/meteor/client/views/room/composer/messageBox/MessageBox.tsx b/apps/meteor/client/views/room/composer/messageBox/MessageBox.tsx index a0f5caf847515..c653b0a132e55 100644 --- a/apps/meteor/client/views/room/composer/messageBox/MessageBox.tsx +++ b/apps/meteor/client/views/room/composer/messageBox/MessageBox.tsx @@ -227,12 +227,6 @@ const MessageBox = ({ } switch (event.key) { - case 'Tab': { - event.preventDefault(); - chat.composer?.insertText(' '); - return; - } - case 'Escape': { closeEditing(event); if (!input.value.trim()) onEscape?.(); From 1b8f0f0c7c02b13f7102ab0eedc9348a9e81cdb0 Mon Sep 17 00:00:00 2001 From: Polina N Date: Sun, 11 Oct 2026 20:16:21 +0300 Subject: [PATCH 3/3] Added changes --- .../room/composer/messageBox/MessageBox.tsx | 24 ++++ .../markup/blocks/ListBlocks.tsx | 69 ++++++++++ .../markup/blocks/OrderedListBlock.tsx | 44 +------ .../markup/blocks/UnorderedListBlock.tsx | 44 +------ .../markup/blocks/splitListItemRuns.ts | 21 +++ packages/gazzodown/src/blocks/ListBlocks.tsx | 48 +++++++ .../gazzodown/src/blocks/OrderedListBlock.tsx | 27 +--- .../src/blocks/UnorderedListBlock.tsx | 27 +--- .../src/blocks/splitListItemRuns.spec.ts | 27 ++++ .../gazzodown/src/blocks/splitListItemRuns.ts | 21 +++ packages/message-parser/src/grammar.pegjs | 97 +++++++++----- .../message-parser/tests/orderedList.test.ts | 42 +++++- .../tests/unorderedList.test.ts | 121 ++++++++++++++---- 13 files changed, 417 insertions(+), 195 deletions(-) create mode 100644 ee/packages/pdf-worker/src/templates/ChatTranscript/markup/blocks/ListBlocks.tsx create mode 100644 ee/packages/pdf-worker/src/templates/ChatTranscript/markup/blocks/splitListItemRuns.ts create mode 100644 packages/gazzodown/src/blocks/ListBlocks.tsx create mode 100644 packages/gazzodown/src/blocks/splitListItemRuns.spec.ts create mode 100644 packages/gazzodown/src/blocks/splitListItemRuns.ts diff --git a/apps/meteor/client/views/room/composer/messageBox/MessageBox.tsx b/apps/meteor/client/views/room/composer/messageBox/MessageBox.tsx index c653b0a132e55..ea03ac9d081a0 100644 --- a/apps/meteor/client/views/room/composer/messageBox/MessageBox.tsx +++ b/apps/meteor/client/views/room/composer/messageBox/MessageBox.tsx @@ -227,6 +227,30 @@ const MessageBox = ({ } switch (event.key) { + case 'Tab': { + const { value, selectionStart, selectionEnd } = input; + if (selectionStart !== selectionEnd) { + return; + } + + const lineStart = value.lastIndexOf('\n', Math.max(0, selectionStart - 1)) + 1; + const lineEnd = value.indexOf('\n', selectionStart); + const currentLine = value.slice(lineStart, lineEnd === -1 ? value.length : lineEnd); + const isListLine = /^[ \t]*(?:[-*]|[0-9]+\.)[ \t]/.test(currentLine); + + if (!isListLine) { + return; + } + + event.preventDefault(); + const cursorOffset = selectionStart - lineStart; + input.setSelectionRange(lineStart, lineStart); + chat.composer?.insertText(' '); + const nextCursor = lineStart + 2 + cursorOffset; + input.setSelectionRange(nextCursor, nextCursor); + return; + } + case 'Escape': { closeEditing(event); if (!input.value.trim()) onEscape?.(); diff --git a/ee/packages/pdf-worker/src/templates/ChatTranscript/markup/blocks/ListBlocks.tsx b/ee/packages/pdf-worker/src/templates/ChatTranscript/markup/blocks/ListBlocks.tsx new file mode 100644 index 0000000000000..e72f7ec92f814 --- /dev/null +++ b/ee/packages/pdf-worker/src/templates/ChatTranscript/markup/blocks/ListBlocks.tsx @@ -0,0 +1,69 @@ +import { StyleSheet, Text, View } from '@react-pdf/renderer'; +import type * as MessageParser from '@rocket.chat/message-parser'; + +import { isOrderedListRun, splitListItemRuns } from './splitListItemRuns'; +import InlineElements from '../elements/InlineElements'; + +const styles = StyleSheet.create({ + wrapper: { + marginTop: 4, + }, + list: { + flexDirection: 'row', + }, + unorderedList: { + display: 'flex', + flexDirection: 'row', + }, + number: { + fontWeight: 700, + marginHorizontal: 4, + }, + bullet: { + marginHorizontal: 4, + }, + nested: { + marginLeft: 16, + }, +}); + +type ListBlockProps = { + items: MessageParser.ListItem[]; +}; + +const NestedLists = ({ items }: { items: MessageParser.ListItem[] }) => ( + <> + {splitListItemRuns(items).map((run, index) => ( + + {isOrderedListRun(run) ? : } + + ))} + +); + +export const OrderedListBlock = ({ items }: ListBlockProps) => ( + + {items.map(({ value, number, children }, index) => ( + + + {number}. {value} + + {children?.length ? : null} + + ))} + +); + +export const UnorderedListBlock = ({ items }: ListBlockProps) => ( + + {items.map(({ value, children }, index) => ( + + + • + {value} + + {children?.length ? : null} + + ))} + +); diff --git a/ee/packages/pdf-worker/src/templates/ChatTranscript/markup/blocks/OrderedListBlock.tsx b/ee/packages/pdf-worker/src/templates/ChatTranscript/markup/blocks/OrderedListBlock.tsx index b57c2a08c70df..de5a7360566af 100644 --- a/ee/packages/pdf-worker/src/templates/ChatTranscript/markup/blocks/OrderedListBlock.tsx +++ b/ee/packages/pdf-worker/src/templates/ChatTranscript/markup/blocks/OrderedListBlock.tsx @@ -1,43 +1 @@ -import { StyleSheet, Text, View } from '@react-pdf/renderer'; -import type * as MessageParser from '@rocket.chat/message-parser'; - -import InlineElements from '../elements/InlineElements'; - -const styles = StyleSheet.create({ - wrapper: { - marginTop: 4, - }, - list: { - flexDirection: 'row', - }, - number: { - fontWeight: 700, - marginHorizontal: 4, - }, - nested: { - marginLeft: 16, - }, -}); - -type OrderedListBlockProps = { - items: MessageParser.ListItem[]; -}; - -const OrderedListBlock = ({ items }: OrderedListBlockProps) => ( - - {items.map(({ value, number, children }, index) => ( - - - {number}. {value} - - {children?.length && ( - - - - )} - - ))} - -); - -export default OrderedListBlock; +export { OrderedListBlock as default } from './ListBlocks'; diff --git a/ee/packages/pdf-worker/src/templates/ChatTranscript/markup/blocks/UnorderedListBlock.tsx b/ee/packages/pdf-worker/src/templates/ChatTranscript/markup/blocks/UnorderedListBlock.tsx index fbf8b38d9531e..9a4aa6c15431f 100644 --- a/ee/packages/pdf-worker/src/templates/ChatTranscript/markup/blocks/UnorderedListBlock.tsx +++ b/ee/packages/pdf-worker/src/templates/ChatTranscript/markup/blocks/UnorderedListBlock.tsx @@ -1,43 +1 @@ -import { StyleSheet, Text, View } from '@react-pdf/renderer'; -import type * as MessageParser from '@rocket.chat/message-parser'; - -import InlineElements from '../elements/InlineElements'; - -const styles = StyleSheet.create({ - wrapper: { - marginTop: 4, - }, - list: { - display: 'flex', - flexDirection: 'row', - }, - bullet: { - marginHorizontal: 4, - }, - nested: { - marginLeft: 16, - }, -}); - -type UnorderedListBlockProps = { - items: MessageParser.ListItem[]; -}; -const UnorderedListBlock = ({ items }: UnorderedListBlockProps) => ( - - {items.map(({ value, children }, index) => ( - - - • - {value} - - {children?.length && ( - - - - )} - - ))} - -); - -export default UnorderedListBlock; +export { UnorderedListBlock as default } from './ListBlocks'; diff --git a/ee/packages/pdf-worker/src/templates/ChatTranscript/markup/blocks/splitListItemRuns.ts b/ee/packages/pdf-worker/src/templates/ChatTranscript/markup/blocks/splitListItemRuns.ts new file mode 100644 index 0000000000000..5c8ddd46945f4 --- /dev/null +++ b/ee/packages/pdf-worker/src/templates/ChatTranscript/markup/blocks/splitListItemRuns.ts @@ -0,0 +1,21 @@ +import type * as MessageParser from '@rocket.chat/message-parser'; + +const isOrderedItem = (item: MessageParser.ListItem): boolean => item.number !== undefined; + +export const splitListItemRuns = (items: MessageParser.ListItem[]): MessageParser.ListItem[][] => { + const runs: MessageParser.ListItem[][] = []; + + for (const item of items) { + const lastRun = runs[runs.length - 1]; + if (!lastRun || isOrderedItem(lastRun[0]) !== isOrderedItem(item)) { + runs.push([item]); + continue; + } + + lastRun.push(item); + } + + return runs; +}; + +export const isOrderedListRun = (items: MessageParser.ListItem[]): boolean => isOrderedItem(items[0]); diff --git a/packages/gazzodown/src/blocks/ListBlocks.tsx b/packages/gazzodown/src/blocks/ListBlocks.tsx new file mode 100644 index 0000000000000..84fd45184699c --- /dev/null +++ b/packages/gazzodown/src/blocks/ListBlocks.tsx @@ -0,0 +1,48 @@ +import type * as MessageParser from '@rocket.chat/message-parser'; +import type { ReactElement } from 'react'; + +import { isOrderedListRun, splitListItemRuns } from './splitListItemRuns'; +import InlineElements from '../elements/InlineElements'; + +type ListBlockProps = { + items: MessageParser.ListItem[]; + nested?: boolean; +}; + +const nestedListStyle = { + paddingInlineStart: '1.5rem', +} as const; + +const NestedLists = ({ items }: { items: MessageParser.ListItem[] }): ReactElement => ( + <> + {splitListItemRuns(items).map((run, index) => + isOrderedListRun(run) ? ( + + ) : ( + + ), + )} + +); + +export const OrderedListBlock = ({ items, nested = false }: ListBlockProps): ReactElement => ( +
        + {items.map(({ value, number, children }, index) => ( +
      1. + {value} + {children?.length ? : null} +
      2. + ))} +
      +); + +export const UnorderedListBlock = ({ items, nested = false }: ListBlockProps): ReactElement => ( +
        + {items.map((item, index) => ( +
      • + {item.value} + {item.children?.length ? : null} +
      • + ))} +
      +); diff --git a/packages/gazzodown/src/blocks/OrderedListBlock.tsx b/packages/gazzodown/src/blocks/OrderedListBlock.tsx index 8981f5323fa57..de5a7360566af 100644 --- a/packages/gazzodown/src/blocks/OrderedListBlock.tsx +++ b/packages/gazzodown/src/blocks/OrderedListBlock.tsx @@ -1,26 +1 @@ -import type * as MessageParser from '@rocket.chat/message-parser'; -import type { ReactElement } from 'react'; - -import InlineElements from '../elements/InlineElements'; - -type OrderedListBlockProps = { - items: MessageParser.ListItem[]; - nested?: boolean; -}; - -const nestedListStyle = { - paddingInlineStart: '1.5rem', -} as const; - -const OrderedListBlock = ({ items, nested = false }: OrderedListBlockProps): ReactElement => ( -
        - {items.map(({ value, number, children }, index) => ( -
      1. - {value} - {children?.length && } -
      2. - ))} -
      -); - -export default OrderedListBlock; +export { OrderedListBlock as default } from './ListBlocks'; diff --git a/packages/gazzodown/src/blocks/UnorderedListBlock.tsx b/packages/gazzodown/src/blocks/UnorderedListBlock.tsx index 9979d86fc7014..9a4aa6c15431f 100644 --- a/packages/gazzodown/src/blocks/UnorderedListBlock.tsx +++ b/packages/gazzodown/src/blocks/UnorderedListBlock.tsx @@ -1,26 +1 @@ -import type * as MessageParser from '@rocket.chat/message-parser'; -import type { ReactElement } from 'react'; - -import InlineElements from '../elements/InlineElements'; - -type UnorderedListBlockProps = { - items: MessageParser.ListItem[]; - nested?: boolean; -}; - -const nestedListStyle = { - paddingInlineStart: '1.5rem', -} as const; - -const UnorderedListBlock = ({ items, nested = false }: UnorderedListBlockProps): ReactElement => ( -
        - {items.map((item, index) => ( -
      • - {item.value} - {item.children?.length && } -
      • - ))} -
      -); - -export default UnorderedListBlock; +export { UnorderedListBlock as default } from './ListBlocks'; diff --git a/packages/gazzodown/src/blocks/splitListItemRuns.spec.ts b/packages/gazzodown/src/blocks/splitListItemRuns.spec.ts new file mode 100644 index 0000000000000..cabd04bc44582 --- /dev/null +++ b/packages/gazzodown/src/blocks/splitListItemRuns.spec.ts @@ -0,0 +1,27 @@ +import { isOrderedListRun, splitListItemRuns } from './splitListItemRuns'; + +const item = (number?: number) => + ({ + type: 'LIST_ITEM' as const, + value: [], + ...(number !== undefined ? { number } : {}), + }) as const; + +describe('splitListItemRuns', () => { + it('keeps a homogeneous ordered run together', () => { + const runs = splitListItemRuns([item(1), item(2)]); + expect(runs).toHaveLength(1); + expect(isOrderedListRun(runs[0])).toBe(true); + }); + + it('keeps a homogeneous unordered run together', () => { + const runs = splitListItemRuns([item(), item()]); + expect(runs).toHaveLength(1); + expect(isOrderedListRun(runs[0])).toBe(false); + }); + + it('splits mixed ordered/unordered siblings into separate runs', () => { + const runs = splitListItemRuns([item(1), item(), item(2), item(3)]); + expect(runs).toEqual([[item(1)], [item()], [item(2), item(3)]]); + }); +}); diff --git a/packages/gazzodown/src/blocks/splitListItemRuns.ts b/packages/gazzodown/src/blocks/splitListItemRuns.ts new file mode 100644 index 0000000000000..5c8ddd46945f4 --- /dev/null +++ b/packages/gazzodown/src/blocks/splitListItemRuns.ts @@ -0,0 +1,21 @@ +import type * as MessageParser from '@rocket.chat/message-parser'; + +const isOrderedItem = (item: MessageParser.ListItem): boolean => item.number !== undefined; + +export const splitListItemRuns = (items: MessageParser.ListItem[]): MessageParser.ListItem[][] => { + const runs: MessageParser.ListItem[][] = []; + + for (const item of items) { + const lastRun = runs[runs.length - 1]; + if (!lastRun || isOrderedItem(lastRun[0]) !== isOrderedItem(item)) { + runs.push([item]); + continue; + } + + lastRun.push(item); + } + + return runs; +}; + +export const isOrderedListRun = (items: MessageParser.ListItem[]): boolean => isOrderedItem(items[0]); diff --git a/packages/message-parser/src/grammar.pegjs b/packages/message-parser/src/grammar.pegjs index 774edefc58da7..3377128c9060a 100644 --- a/packages/message-parser/src/grammar.pegjs +++ b/packages/message-parser/src/grammar.pegjs @@ -45,33 +45,33 @@ let skipReferences = false; function buildNestedList(items) { if (!items || !items.length) return items; - + const root = []; const stack = [{ children: root, indent: -1 }]; - + for (const item of items) { const indent = item.indent || 0; - + while (stack.length > 1 && stack[stack.length - 1].indent >= indent) { stack.pop(); } - + const cleanItem = { type: item.type, value: item.value, ...(item.number !== undefined && { number: item.number }), }; - + const parent = stack[stack.length - 1]; parent.children.push(cleanItem); - + cleanItem.children = []; - + stack.push({ children: cleanItem.children, indent }); } - - function cleanEmptyChildren(items) { - for (const item of items) { + + function cleanEmptyChildren(listItems) { + for (const item of listItems) { if (item.children && !item.children.length) { delete item.children; } else if (item.children) { @@ -79,11 +79,40 @@ function buildNestedList(items) { } } } - + cleanEmptyChildren(root); - + return root; } + +let listSegmentMarker = null; +let listSegmentBaseIndent = null; + +function resetListSegment() { + listSegmentMarker = null; + listSegmentBaseIndent = null; +} + +function acceptsListItem(item) { + const indent = item.indent || 0; + const marker = item.marker; + + if (listSegmentMarker === null) { + listSegmentMarker = marker; + listSegmentBaseIndent = indent; + return true; + } + + if (indent > listSegmentBaseIndent) { + return true; + } + + if (indent === listSegmentBaseIndent && marker === listSegmentMarker) { + return true; + } + + return false; +} }} Start @@ -101,8 +130,7 @@ Blocks / Code / Heading / Tasks - / OrderedList - / UnorderedList + / List / Katex / LineBreak @@ -209,42 +237,47 @@ TaskFlag = "x" { return true; } / " " { return false; } /** * - * Ordered List + * Lists (ordered / unordered), including nested and mixed nested markers. * e.g: * 1. Item One + * - Nested unordered * 2. Item Two - * 3. Item Three + * + * - Item One + * 1. Nested ordered + * - Item Two + * + * Same-level marker changes (`-` then `*`, or `-` then `1.`) start a new list. * */ -OrderedList = items:OrderedListItem+ { return orderedList(buildNestedList(items)); } +List = &{ resetListSegment(); return true; } items:ListItemInSegment+ { + resetListSegment(); + const nested = buildNestedList(items); + return items[0].number !== undefined ? orderedList(nested) : unorderedList(nested); +} + +ListItemInSegment = item:AnyListItem &{ return acceptsListItem(item); } { return item; } -OrderedListItem = indent:$[ \t]* number:Digits "." [ \t]+ text:Inline { +AnyListItem = OrderedListItem / UnorderedListHyphenItem / UnorderedListAsteriskItem + +OrderedListItem = indent:$[ \t]* number:Digits "." [ \t]+ text:Inline { const item = listItem(text, parseInt(number, 10)); item.indent = indent.length; + item.marker = 'ordered'; return item; } -/** - * - * Unordered List - * e.g: - * - Item One - * - Item Two - * * Item Three - * * Item Four - * - */ -UnorderedList = items:(UnorderedListHyphenItem+ / UnorderedListAsteriskItem+) { return unorderedList(buildNestedList(items)); } - -UnorderedListHyphenItem = indent:$[ \t]* "-" [ \t]+ text:Inline { +UnorderedListHyphenItem = indent:$[ \t]* "-" [ \t]+ text:Inline { const item = listItem(text); item.indent = indent.length; + item.marker = '-'; return item; } -UnorderedListAsteriskItem = indent:$[ \t]* "*" [ \t]+ text:UnorderedListItemContent { +UnorderedListAsteriskItem = indent:$[ \t]* "*" [ \t]+ text:UnorderedListItemContent { const item = listItem(text); item.indent = indent.length; + item.marker = '*'; return item; } diff --git a/packages/message-parser/tests/orderedList.test.ts b/packages/message-parser/tests/orderedList.test.ts index 9f1ed0fbde291..7d550fca9efdd 100644 --- a/packages/message-parser/tests/orderedList.test.ts +++ b/packages/message-parser/tests/orderedList.test.ts @@ -1,5 +1,5 @@ import { parse } from '../src'; -import { bold, plain, orderedList, listItem, emoji } from './helpers'; +import { bold, plain, orderedList, listItem, emoji, unorderedList } from './helpers'; test.each([ [ @@ -24,6 +24,46 @@ test.each([ ]), ], ], + [ + ` +1. First item +2. Second item + 1. Nested item + 2. Nested item +3. Third item +`.trim(), + [ + orderedList([ + listItem([plain('First item')], 1), + listItem([plain('Second item')], 2, [ + listItem([plain('Nested item')], 1), + listItem([plain('Nested item')], 2), + ]), + listItem([plain('Third item')], 3), + ]), + ], + ], + [ + ` +1. Parent + - Nested unordered + - Another +2. Sibling +`.trim(), + [ + orderedList([ + listItem([plain('Parent')], 1, [listItem([plain('Nested unordered')]), listItem([plain('Another')])]), + listItem([plain('Sibling')], 2), + ]), + ], + ], + [ + ` +1. Ordered +- Unordered sibling +`.trim(), + [orderedList([listItem([plain('Ordered')], 1)]), unorderedList([listItem([plain('Unordered sibling')])])], + ], ])('parses %p', (input, output) => { expect(parse(input)).toEqual(output); }); diff --git a/packages/message-parser/tests/unorderedList.test.ts b/packages/message-parser/tests/unorderedList.test.ts index c7fbd1182767a..3dfb7a905aed9 100644 --- a/packages/message-parser/tests/unorderedList.test.ts +++ b/packages/message-parser/tests/unorderedList.test.ts @@ -1,5 +1,5 @@ import { parse } from '../src'; -import { unorderedList, plain, listItem, bold, emoji } from './helpers'; +import { unorderedList, plain, listItem, bold, emoji, orderedList } from './helpers'; test.each([ [ @@ -36,7 +36,6 @@ test.each([ ]), ], ], - [ ` - First item @@ -49,28 +48,102 @@ test.each([ unorderedList([listItem([plain('Second item')]), listItem([plain('Third item')]), listItem([bold([plain('Fourth item')])])]), ], ], - // [ - // ` - // * First item - // * Second item - // * Third item - // * Indented item - // * Indented item - // * Fourth item - // `.trim(), - // [paragraph([])], - // ], - // [ - // ` - // - First item - // - Second item - // - Third item - // - Indented item - // - Indented item - // - Fourth item - // `.trim(), - // [paragraph([])], - // ], + [ + ` +* First item +* Second item +* Third item + * Indented item + * Indented item +* Fourth item +`.trim(), + [ + unorderedList([ + listItem([plain('First item')]), + listItem([plain('Second item')]), + listItem([plain('Third item')], undefined, [ + listItem([plain('Indented item')]), + listItem([plain('Indented item')]), + ]), + listItem([plain('Fourth item')]), + ]), + ], + ], + [ + ` +- First item +- Second item +- Third item + - Indented item + - Indented item +- Fourth item +`.trim(), + [ + unorderedList([ + listItem([plain('First item')]), + listItem([plain('Second item')]), + listItem([plain('Third item')], undefined, [ + listItem([plain('Indented item')]), + listItem([plain('Indented item')]), + ]), + listItem([plain('Fourth item')]), + ]), + ], + ], + [ + ` +- Level 1 + - Level 2 + - Level 3 +`.trim(), + [ + unorderedList([ + listItem([plain('Level 1')], undefined, [ + listItem([plain('Level 2')], undefined, [listItem([plain('Level 3')])]), + ]), + ]), + ], + ], + [ + ` +- Parent + 1. Nested ordered + 2. Another nested +- Sibling +`.trim(), + [ + unorderedList([ + listItem([plain('Parent')], undefined, [ + listItem([plain('Nested ordered')], 1), + listItem([plain('Another nested')], 2), + ]), + listItem([plain('Sibling')]), + ]), + ], + ], + [ + ` +- Unordered +1. Ordered sibling +`.trim(), + [unorderedList([listItem([plain('Unordered')])]), orderedList([listItem([plain('Ordered sibling')], 1)])], + ], + [ + ` +- Parent + 1. Ordered child + - Unordered child +`.trim(), + [ + unorderedList([ + listItem([plain('Parent')], undefined, [ + listItem([plain('Ordered child')], 1), + listItem([plain('Unordered child')]), + ]), + ]), + ], + ], ])('parses %p', (input, output) => { expect(parse(input)).toEqual(output); }); +