Skip to content

Commit

Permalink
Handle too many nested quotes (#14633)
Browse files Browse the repository at this point in the history
* Don't render block quotes after 6 levels deep

* Update snapshpots
  • Loading branch information
MarcoPolo committed Nov 9, 2018
1 parent 6ed078d commit d5ab723
Show file tree
Hide file tree
Showing 3 changed files with 673 additions and 3 deletions.
1 change: 1 addition & 0 deletions shared/common-adapters/markdown/index.stories.js
Expand Up @@ -102,6 +102,7 @@ something unrelated

> Separate paragraph
`,
'Quotes super nested': `> > > > > > > > > foo bar`,
'Quotes 3': `> _foo_ and *bar*! \`\`\`
a = 1
\`\`\`
Expand Down
12 changes: 9 additions & 3 deletions shared/common-adapters/markdown/shared.js
Expand Up @@ -278,11 +278,17 @@ const rules = {
// e.g. https://regex101.com/r/ZiDBsO/8
parse: (capture, parse, state) => {
const content = capture[0].replace(/^ *> */gm, '')
const blockQuoteRecursionLevel = state.blockQuoteRecursionLevel || 0
const nextState = {...state, blockQuoteRecursionLevel: blockQuoteRecursionLevel + 1}

return {
content: parse(content, state),
content: parse(content, nextState),
}
},
match: (source, state, lookbehind) => {
if (state.blockQuoteRecursionLevel > 6) {
return null
}
const regex = /^( *>(?:[^\n](?!```))+\n?)+/
// make sure the look behind is empty
const emptyLookbehind = /^$|\n *$/
Expand Down Expand Up @@ -426,7 +432,7 @@ class SimpleMarkdownComponent extends PureComponent<MarkdownProps, {hasError: bo
render() {
if (this.state.hasError) {
return (
<Text type="Body" style={styles.rootWrapper}>
<Text type="Body" style={Styles.collapseStyles([styles.rootWrapper, markdownStyles.wrapStyle])}>
{this.props.children || ''}
</Text>
)
Expand All @@ -452,7 +458,7 @@ class SimpleMarkdownComponent extends PureComponent<MarkdownProps, {hasError: bo
logger.error('Error parsing markdown')
logger.debug('Error parsing markdown', e)
return (
<Text type="Body" style={styles.rootWrapper}>
<Text type="Body" style={Styles.collapseStyles([styles.rootWrapper, markdownStyles.wrapStyle])}>
{this.props.children || ''}
</Text>
)
Expand Down

0 comments on commit d5ab723

Please sign in to comment.