Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/real-ties-unite.md
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions apps/meteor/client/views/room/composer/messageBox/MessageBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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?.();
Expand Down
Original file line number Diff line number Diff line change
@@ -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) => (
<View key={index} style={styles.nested}>
{isOrderedListRun(run) ? <OrderedListBlock items={run} /> : <UnorderedListBlock items={run} />}
</View>
))}
</>
);

export const OrderedListBlock = ({ items }: ListBlockProps) => (
<View style={styles.wrapper} wrap>
{items.map(({ value, number, children }, index) => (
<View key={index}>
<Text style={styles.list}>
<Text style={styles.number}>{number}.</Text> <InlineElements>{value}</InlineElements>
</Text>
{children?.length ? <NestedLists items={children} /> : null}
</View>
))}
</View>
);

export const UnorderedListBlock = ({ items }: ListBlockProps) => (
<View style={styles.wrapper} wrap>
{items.map(({ value, children }, index) => (
<View key={index}>
<View style={styles.unorderedList}>
<Text style={styles.bullet}>•</Text>
<InlineElements>{value}</InlineElements>
</View>
{children?.length ? <NestedLists items={children} /> : null}
</View>
))}
</View>
);
Original file line number Diff line number Diff line change
@@ -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) => (
<View style={styles.wrapper} wrap>
{items.map(({ value, number }, index) => (
<Text style={styles.list} key={index}>
<Text style={styles.number}>{number}.</Text> <InlineElements>{value}</InlineElements>
</Text>
))}
</View>
);

export default OrderedListBlock;
export { OrderedListBlock as default } from './ListBlocks';
Original file line number Diff line number Diff line change
@@ -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) => (
<View style={styles.wrapper} wrap>
{items.map(({ value }, index) => (
<View style={styles.list} key={index}>
<Text style={styles.bullet}>•</Text>
<InlineElements>{value}</InlineElements>
</View>
))}
</View>
);

export default UnorderedListBlock;
export { UnorderedListBlock as default } from './ListBlocks';
Original file line number Diff line number Diff line change
@@ -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]);
8 changes: 6 additions & 2 deletions packages/gazzodown-alt/src/ComposerList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 => (
<span>
{items.map((item, index) => (
<span key={index}>
<span style={listMarkerStyle}>{marker(item)}</span>
<span style={{ ...listMarkerStyle, paddingInlineStart: `${0.5 + depth * 1.5}rem` }}>{marker(item)}</span>
<ComposerInlineElements>{item.value}</ComposerInlineElements>
{'\n'}
{item.children?.length ? <ComposerList items={item.children} marker={nestedMarker} depth={depth + 1} /> : null}
</span>
))}
</span>
Expand Down
7 changes: 6 additions & 1 deletion packages/gazzodown-alt/src/sourceLines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
48 changes: 48 additions & 0 deletions packages/gazzodown/src/blocks/ListBlocks.tsx
Original file line number Diff line number Diff line change
@@ -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) ? (
<OrderedListBlock key={index} items={run} nested />
) : (
<UnorderedListBlock key={index} items={run} nested />
),
)}
</>
);

export const OrderedListBlock = ({ items, nested = false }: ListBlockProps): ReactElement => (
<ol style={nested ? nestedListStyle : undefined}>
{items.map(({ value, number, children }, index) => (
<li key={index} value={number}>
<InlineElements>{value}</InlineElements>
{children?.length ? <NestedLists items={children} /> : null}
</li>
))}
</ol>
);

export const UnorderedListBlock = ({ items, nested = false }: ListBlockProps): ReactElement => (
<ul style={nested ? nestedListStyle : undefined}>
{items.map((item, index) => (
<li key={index}>
<InlineElements>{item.value}</InlineElements>
{item.children?.length ? <NestedLists items={item.children} /> : null}
</li>
))}
</ul>
);
20 changes: 1 addition & 19 deletions packages/gazzodown/src/blocks/OrderedListBlock.tsx
Original file line number Diff line number Diff line change
@@ -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) => (
<ol>
{items.map(({ value, number }, index) => (
<li key={index} value={number}>
<InlineElements>{value}</InlineElements>
</li>
))}
</ol>
);

export default OrderedListBlock;
export { OrderedListBlock as default } from './ListBlocks';
20 changes: 1 addition & 19 deletions packages/gazzodown/src/blocks/UnorderedListBlock.tsx
Original file line number Diff line number Diff line change
@@ -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) => (
<ul>
{items.map((item, index) => (
<li key={index}>
<InlineElements>{item.value}</InlineElements>
</li>
))}
</ul>
);

export default UnorderedListBlock;
export { UnorderedListBlock as default } from './ListBlocks';
27 changes: 27 additions & 0 deletions packages/gazzodown/src/blocks/splitListItemRuns.spec.ts
Original file line number Diff line number Diff line change
@@ -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)]]);
});
});
21 changes: 21 additions & 0 deletions packages/gazzodown/src/blocks/splitListItemRuns.ts
Original file line number Diff line number Diff line change
@@ -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]);
1 change: 1 addition & 0 deletions packages/message-parser/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type ListItem = {
type: 'LIST_ITEM';
value: Inlines[];
number?: number;
children?: ListItem[];
};

export type Tasks = {
Expand Down
Loading