This repository has been archived by the owner on Jun 3, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 144
Markdown dedent, and remove containerProps #569
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
24bdf29
markdown: bind highlightCode
alexcjohnson ebc6856
remove Markdown containerProps
alexcjohnson f17e0fa
add Markdown dedent prop and enable it by default
alexcjohnson d207da3
uncomment the bulk of test_integration
alexcjohnson df5192d
changelog for markdown highlighting (and syntaxhighlighter removal) a…
alexcjohnson 6eeb96a
fix indentation in test_integration
alexcjohnson b0f22e2
remove SyntaxHighlighter from tests
alexcjohnson a1c36d3
do not highlight inline code, and fix code highlight test
alexcjohnson f8f44c6
Merge branch 'master' into markdown-dedent
alexcjohnson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import React, {Component} from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import {omit, propOr, type} from 'ramda'; | ||
import {type} from 'ramda'; | ||
import Markdown from 'react-markdown'; | ||
import './css/highlight.css'; | ||
|
||
|
@@ -11,6 +11,12 @@ import './css/highlight.css'; | |
* [react-markdown](https://rexxars.github.io/react-markdown/) under the hood. | ||
*/ | ||
class DashMarkdown extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.highlightCode = this.highlightCode.bind(this); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. apparently not needed given our current usage, but makes me nervous to have it there unbound... |
||
this.dedent = this.dedent.bind(this); | ||
} | ||
|
||
componentDidMount() { | ||
this.highlightCode(); | ||
} | ||
|
@@ -25,14 +31,48 @@ class DashMarkdown extends Component { | |
return; | ||
} | ||
if (this.mdContainer) { | ||
const nodes = this.mdContainer.getElementsByTagName('code'); | ||
const nodes = this.mdContainer.querySelectorAll('pre code'); | ||
|
||
for (let i = 0; i < nodes.length; i++) { | ||
window.hljs.highlightBlock(nodes[i]); | ||
} | ||
} | ||
} | ||
|
||
dedent(text) { | ||
const lines = text.split(/\r\n|\r|\n/); | ||
let commonPrefix = null; | ||
for (const line of lines) { | ||
const preMatch = line && line.match(/^\s*(?=\S)/); | ||
if (preMatch) { | ||
const prefix = preMatch[0]; | ||
if (commonPrefix !== null) { | ||
for (let i = 0; i < commonPrefix.length; i++) { | ||
// Like Python's textwrap.dedent, we'll remove both | ||
// space and tab characters, but only if they match | ||
if (prefix[i] !== commonPrefix[i]) { | ||
commonPrefix = commonPrefix.substr(0, i); | ||
break; | ||
} | ||
} | ||
} else { | ||
commonPrefix = prefix; | ||
} | ||
|
||
if (!commonPrefix) { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
const commonLen = commonPrefix ? commonPrefix.length : 0; | ||
return lines | ||
.map(line => { | ||
return line.match(/\S/) ? line.substr(commonLen) : ''; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
}) | ||
.join('\n'); | ||
} | ||
|
||
render() { | ||
const { | ||
id, | ||
|
@@ -41,11 +81,14 @@ class DashMarkdown extends Component { | |
highlight_config, | ||
loading_state, | ||
dangerously_allow_html, | ||
children, | ||
dedent, | ||
} = this.props; | ||
|
||
if (type(this.props.children) === 'Array') { | ||
this.props.children = this.props.children.join('\n'); | ||
} | ||
const textProp = | ||
type(children) === 'Array' ? children.join('\n') : children; | ||
const displayText = | ||
dedent && textProp ? this.dedent(textProp) : textProp; | ||
|
||
return ( | ||
<div | ||
|
@@ -68,12 +111,10 @@ class DashMarkdown extends Component { | |
data-dash-is-loading={ | ||
(loading_state && loading_state.is_loading) || undefined | ||
} | ||
{...propOr({}, 'containerProps', this.props)} | ||
> | ||
<Markdown | ||
source={this.props.children} | ||
source={displayText} | ||
escapeHtml={!dangerously_allow_html} | ||
{...omit(['containerProps'], this.props)} | ||
/> | ||
</div> | ||
); | ||
|
@@ -92,12 +133,6 @@ DashMarkdown.propTypes = { | |
*/ | ||
className: PropTypes.string, | ||
|
||
/** | ||
* An object containing custom element props to put on the container | ||
* element such as id or style | ||
*/ | ||
containerProps: PropTypes.object, | ||
|
||
/** | ||
* A boolean to control raw HTML escaping. | ||
* Setting HTML from code is risky because it's easy to | ||
|
@@ -114,10 +149,21 @@ DashMarkdown.propTypes = { | |
PropTypes.arrayOf(PropTypes.string), | ||
]), | ||
|
||
/** | ||
* Remove matching leading whitespace from all lines. | ||
* Lines that are empty, or contain *only* whitespace, are ignored. | ||
* Both spaces and tab characters are removed, but only if they match; | ||
* we will not convert tabs to spaces or vice versa. | ||
*/ | ||
dedent: PropTypes.bool, | ||
|
||
/** | ||
* Config options for syntax highlighting. | ||
*/ | ||
highlight_config: PropTypes.exact({ | ||
/** | ||
* Color scheme; default 'light' | ||
*/ | ||
theme: PropTypes.oneOf(['dark', 'light']), | ||
}), | ||
|
||
|
@@ -148,6 +194,7 @@ DashMarkdown.propTypes = { | |
DashMarkdown.defaultProps = { | ||
dangerously_allow_html: false, | ||
highlight_config: {}, | ||
dedent: true, | ||
}; | ||
|
||
export default DashMarkdown; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add that
Markdown
will attempt to auto-detect the language (viahighlight.js
) without specifying the language nameThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also maybe mention the
theme
prop, but this could be in the docs fordcc.Markdown
as well/instead