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 90d0a777a4d4d..928441a7ac6db 100644 --- a/apps/meteor/client/views/room/composer/messageBox/MessageBox.tsx +++ b/apps/meteor/client/views/room/composer/messageBox/MessageBox.tsx @@ -205,6 +205,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 3f7a2ce6e07f6..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,33 +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, - }, -}); - -export type OrderedListBlockProps = { - items: MessageParser.ListItem[]; -}; - -const OrderedListBlock = ({ items }: OrderedListBlockProps) => ( - - {items.map(({ value, number }, index) => ( - - {number}. {value} - - ))} - -); - -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 0e1d70ca70578..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,33 +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, - }, -}); - -export type UnorderedListBlockProps = { - items: MessageParser.ListItem[]; -}; -const UnorderedListBlock = ({ items }: UnorderedListBlockProps) => ( - - {items.map(({ value }, index) => ( - - • - {value} - - ))} - -); - -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-alt/src/ComposerList.tsx b/packages/gazzodown-alt/src/ComposerList.tsx index 54197a8b25485..7bb1dd6d66f6a 100644 --- a/packages/gazzodown-alt/src/ComposerList.tsx +++ b/packages/gazzodown-alt/src/ComposerList.tsx @@ -6,15 +6,19 @@ import ComposerInlineElements from './ComposerInlineElements'; type ComposerListProps = { items: MessageParser.ListItem[]; marker: (item: MessageParser.ListItem) => string; + depth?: number; }; -const ComposerList = ({ items, marker }: ComposerListProps): ReactElement => ( +const nestedMarker = (item: MessageParser.ListItem): string => (item.number !== undefined ? `${item.number}. ` : '- '); + +const ComposerList = ({ items, marker, depth = 0 }: ComposerListProps): ReactElement => ( {items.map((item, index) => ( - {marker(item)} + {marker(item)} {item.value} {'\n'} + {item.children?.length ? : null} ))} diff --git a/packages/gazzodown-alt/src/sourceLines.ts b/packages/gazzodown-alt/src/sourceLines.ts index 91e342419f9d0..230c7030ace56 100644 --- a/packages/gazzodown-alt/src/sourceLines.ts +++ b/packages/gazzodown-alt/src/sourceLines.ts @@ -4,15 +4,20 @@ import { blockSourceOf } from './sourceOf'; type Block = MessageParser.Root[number]; +const countListItems = (items: MessageParser.ListItem[]): number => + items.reduce((total, item) => total + 1 + (item.children?.length ? countListItems(item.children) : 0), 0); + // How many source lines a block covers, so the next block can be lined up with the line it started // on. const lineSpanOf = (block: Block, source: string): number => { switch (block.type) { case 'QUOTE': case 'TASKS': + return block.value.length; + case 'UNORDERED_LIST': case 'ORDERED_LIST': - return block.value.length; + return countListItems(block.value); case 'CODE': case 'SPOILER_BLOCK': 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 => ( + +); diff --git a/packages/gazzodown/src/blocks/OrderedListBlock.tsx b/packages/gazzodown/src/blocks/OrderedListBlock.tsx index b4fac42f50478..de5a7360566af 100644 --- a/packages/gazzodown/src/blocks/OrderedListBlock.tsx +++ b/packages/gazzodown/src/blocks/OrderedListBlock.tsx @@ -1,19 +1 @@ -import type * as MessageParser from '@rocket.chat/message-parser'; - -import InlineElements from '../elements/InlineElements'; - -export type OrderedListBlockProps = { - items: MessageParser.ListItem[]; -}; - -const OrderedListBlock = ({ items }: OrderedListBlockProps) => ( -
    - {items.map(({ value, number }, index) => ( -
  1. - {value} -
  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 52d8553c47a5b..9a4aa6c15431f 100644 --- a/packages/gazzodown/src/blocks/UnorderedListBlock.tsx +++ b/packages/gazzodown/src/blocks/UnorderedListBlock.tsx @@ -1,19 +1 @@ -import type * as MessageParser from '@rocket.chat/message-parser'; - -import InlineElements from '../elements/InlineElements'; - -export type UnorderedListBlockProps = { - items: MessageParser.ListItem[]; -}; - -const UnorderedListBlock = ({ items }: UnorderedListBlockProps) => ( - -); - -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/definitions.ts b/packages/message-parser/src/definitions.ts index 6af50ad9175fe..e50e33a16272d 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 0ac2f4a2525f3..2af276f637c7b 100644 --- a/packages/message-parser/src/grammar.pegjs +++ b/packages/message-parser/src/grammar.pegjs @@ -44,6 +44,77 @@ 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(listItems) { + for (const item of listItems) { + if (item.children && !item.children.length) { + delete item.children; + } else if (item.children) { + cleanEmptyChildren(item.children); + } + } + } + + 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 @@ -63,8 +134,7 @@ Blocks / Table / Heading / Tasks - / OrderedList - / UnorderedList + / List / Katex / LineBreak @@ -203,32 +273,49 @@ 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 * - */ -OrderedList = items:OrderedListItem+ { return orderedList(items); } - -OrderedListItem = number:Digits "." [ \t]+ text:Inline { return listItem(text, parseInt(number, 10)); } - -/** - * - * Unordered List - * e.g: * - Item One + * 1. Nested ordered * - Item Two - * * Item Three - * * Item Four + * + * Same-level marker changes (`-` then `*`, or `-` then `1.`) start a new list. * */ -UnorderedList = items:(UnorderedListHyphenItem+ / UnorderedListAsteriskItem+) { return unorderedList(items); } - -UnorderedListHyphenItem = "-" [ \t]+ text:Inline { return listItem(text); } - -UnorderedListAsteriskItem = "*" [ \t]+ text:UnorderedListItemContent { return listItem(text); } +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; } + +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; +} + +UnorderedListHyphenItem = indent:$[ \t]* "-" [ \t]+ text:Inline { + const item = listItem(text); + item.indent = indent.length; + item.marker = '-'; + return item; +} + +UnorderedListAsteriskItem = indent:$[ \t]* "*" [ \t]+ text:UnorderedListItemContent { + const item = listItem(text); + item.indent = indent.length; + item.marker = '*'; + 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 92f07b4da1864..edb6f90ad1e6c 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) => ({ 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); }); +