Skip to content

Commit

Permalink
MM-53107 Add limit to Markdown nodes (#7528) (#7535)
Browse files Browse the repository at this point in the history
* MM-53107 Add limit to Markdown nodes

* Update commonmark to published version

* Rename field to camel case for consistency

* Update Markdown tests for removed children

(cherry picked from commit 2651dd3)

Co-authored-by: Harrison Healey <[email protected]>
  • Loading branch information
mattermost-build and hmhealey authored Sep 7, 2023
1 parent 3d92a61 commit 8b58fdb
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 46 deletions.
6 changes: 4 additions & 2 deletions app/components/markdown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {withDatabase} from '@nozbe/watermelondb/DatabaseProvider';
import withObservables from '@nozbe/with-observables';
import React from 'react';

import {observeConfigBooleanValue} from '@queries/servers/system';
import {observeConfigBooleanValue, observeConfigIntValue} from '@queries/servers/system';

import Markdown from './markdown';

Expand All @@ -14,11 +14,13 @@ import type {WithDatabaseArgs} from '@typings/database/database';
const enhanced = withObservables([], ({database}: WithDatabaseArgs) => {
const enableLatex = observeConfigBooleanValue(database, 'EnableLatex');
const enableInlineLatex = observeConfigBooleanValue(database, 'EnableInlineLatex');
const maxNodes = observeConfigIntValue(database, 'MaxMarkdownNodes');

return {
enableLatex,
enableInlineLatex,
maxNodes,
};
});

export default React.memo(withDatabase(enhanced(Markdown)));
export default withDatabase(enhanced(React.memo(Markdown)));
25 changes: 22 additions & 3 deletions app/components/markdown/markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,15 @@ type MarkdownProps = {
layoutHeight?: number;
layoutWidth?: number;
location: string;
maxNodes: number;
mentionKeys?: UserMentionKey[];
minimumHashtagLength?: number;
onPostPress?: (event: GestureResponderEvent) => void;
postId?: string;
searchPatterns?: SearchPattern[];
textStyles?: MarkdownTextStyles;
theme: Theme;
value?: string | number;
value?: string;
}

const getStyleSheet = makeStyleSheetFromTheme((theme) => {
Expand All @@ -94,6 +95,9 @@ const getStyleSheet = makeStyleSheetFromTheme((theme) => {
color: editedColor,
opacity: editedOpacity,
},
maxNodesWarning: {
color: theme.errorTextColor,
},
atMentionOpacity: {
opacity: 1,
},
Expand Down Expand Up @@ -126,7 +130,7 @@ const Markdown = ({
autolinkedUrlSchemes, baseTextStyle, blockStyles, channelId, channelMentions,
disableAtChannelMentionHighlight, disableAtMentions, disableBlockQuote, disableChannelLink,
disableCodeBlock, disableGallery, disableHashtags, disableHeading, disableTables,
enableInlineLatex, enableLatex,
enableInlineLatex, enableLatex, maxNodes,
imagesMetadata, isEdited, isReplyPost, isSearchResult, layoutHeight, layoutWidth,
location, mentionKeys, minimumHashtagLength = 3, onPostPress, postId, searchPatterns,
textStyles = {}, theme, value = '', baseParagraphStyle,
Expand Down Expand Up @@ -520,6 +524,19 @@ const Markdown = ({
);
};

const renderMaxNodesWarning = () => {
const styles = [baseTextStyle, style.maxNodesWarning];

return (
<FormattedText
id='markdown.max_nodes.error'
defaultMessage='This message is too long to by shown fully on a mobile device. Please view it on desktop or contact an admin to increase this limit.'
style={styles}
testID='max_nodes_warning'
/>
);
};

const createRenderer = () => {
const renderers: any = {
text: renderText,
Expand All @@ -534,7 +551,7 @@ const Markdown = ({
channelLink: renderChannelLink,
emoji: renderEmoji,
hashtag: renderHashtag,
latexinline: renderLatexInline,
latexInline: renderLatexInline,

paragraph: renderParagraph,
heading: renderHeading,
Expand All @@ -560,11 +577,13 @@ const Markdown = ({
checkbox: renderCheckbox,

editedIndicator: renderEditedIndicator,
maxNodesWarning: renderMaxNodesWarning,
};

return new Renderer({
renderers,
renderParagraphsInLists: true,
maxNodes,
getExtraPropsForNode,
allowedTypes: Object.keys(renderers),
});
Expand Down
8 changes: 5 additions & 3 deletions app/components/markdown/markdown_table_row/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ const MarkdownTableRow = ({isFirstRow, isLastRow, children}: MarkdownTableRowPro
// Add an extra prop to the last cell so that it knows not to render a right border since the container
// will handle that
const renderChildren = React.Children.toArray(children) as ReactElement[];
renderChildren[renderChildren.length - 1] = React.cloneElement(renderChildren[renderChildren.length - 1], {
isLastCell: true,
});
if (renderChildren.length > 0) {
renderChildren[renderChildren.length - 1] = React.cloneElement(renderChildren[renderChildren.length - 1], {
isLastCell: true,
});
}

return (
<View
Expand Down
20 changes: 0 additions & 20 deletions app/components/markdown/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2131,10 +2131,6 @@ describe('Components.Markdown.transform', () => {
}, {
type: 'at_mention',
_mentionName: 'user',
children: [{
type: 'text',
literal: '@user',
}],
}],
}],
},
Expand All @@ -2154,10 +2150,6 @@ describe('Components.Markdown.transform', () => {
children: [{
type: 'at_mention',
_mentionName: 'words',
children: [{
type: 'text',
literal: '@words',
}],
}],
}],
}],
Expand All @@ -2178,10 +2170,6 @@ describe('Components.Markdown.transform', () => {
children: [{
type: 'at_mention',
_mentionName: 'words',
children: [{
type: 'text',
literal: '@words',
}],
}],
}],
}],
Expand Down Expand Up @@ -2579,10 +2567,6 @@ describe('Components.Markdown.transform', () => {
children: [{
type: 'at_mention',
_mentionName: 'channel.',
children: [{
type: 'text',
literal: '@channel.',
}],
}],
}],
}],
Expand All @@ -2601,10 +2585,6 @@ describe('Components.Markdown.transform', () => {
}, {
type: 'at_mention',
_mentionName: 'Gvn.',
children: [{
type: 'text',
literal: '@Gvn.',
}],
}],
}],
},
Expand Down
1 change: 1 addition & 0 deletions assets/base/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@
"login.signingIn": "Logging In",
"login.username": "Username",
"markdown.latex.error": "Latex render error",
"markdown.max_nodes.error": "This message is too long to by shown fully on a mobile device. Please view it on desktop or contact an admin to increase this limit.",
"mentions.empty.paragraph": "You'll see messages here when someone mentions you or uses terms you're monitoring.",
"mentions.empty.title": "No Mentions yet",
"mobile.about.appVersion": "App Version: {version} (Build {number})",
Expand Down
26 changes: 13 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
"@stream-io/flat-list-mvcp": "0.10.3",
"@tsconfig/react-native": "3.0.2",
"base-64": "1.0.0",
"commonmark": "npm:@mattermost/[email protected]0",
"commonmark-react-renderer": "github:mattermost/commonmark-react-renderer#2c660491041f7595f6ce5a05f6dc2e30ca769d3a",
"commonmark": "npm:@mattermost/[email protected]1",
"commonmark-react-renderer": "github:mattermost/commonmark-react-renderer#235bc817bcade503fb81fa51bbbe3c84f958ed12",
"deep-equal": "2.2.2",
"deepmerge": "4.3.1",
"emoji-regex": "10.2.1",
Expand Down
7 changes: 4 additions & 3 deletions patches/@types+commonmark-react-renderer+4.3.1.patch
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
diff --git a/node_modules/@types/commonmark-react-renderer/index.d.ts b/node_modules/@types/commonmark-react-renderer/index.d.ts
index 9ee5664..44d9a20 100755
index 9ee5664..7ada0a0 100755
--- a/node_modules/@types/commonmark-react-renderer/index.d.ts
+++ b/node_modules/@types/commonmark-react-renderer/index.d.ts
@@ -88,6 +88,8 @@ declare namespace ReactRenderer {
@@ -88,6 +88,9 @@ declare namespace ReactRenderer {
transformLinkUri?: ((uri: string) => string) | null | undefined;
transformImageUri?: ((uri: string) => string) | null | undefined;
linkTarget?: string | undefined;
+ renderParagraphsInLists?: boolean;
+ maxNodes?: number;
+ getExtraPropsForNode?: (node: any) => Record<string, any>;
}

interface Renderer {
@@ -113,6 +115,7 @@ interface ReactRenderer {
@@ -113,6 +116,7 @@ interface ReactRenderer {
uriTransformer: (uri: string) => string;
types: string[];
renderers: ReactRenderer.Renderers;
Expand Down
1 change: 1 addition & 0 deletions types/api/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ interface ClientConfig {
LdapPositionAttributeSet: string;
LockTeammateNameDisplay: string;
MaxFileSize: string;
MaxMarkdownNodes: string;
MaxNotificationsPerChannel: string;
MaxPostSize: string;
MinimumHashtagLength: string;
Expand Down

0 comments on commit 8b58fdb

Please sign in to comment.