Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Refactor markdown syntax highlighting. #720

Merged
merged 17 commits into from
Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from 15 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- [#706](https://github.com/plotly/dash-core-components/pull/706)
- Upgraded plotly.js to [1.51.3](https://github.com/plotly/plotly.js/releases/tag/v1.51.3)

### Changed
- [#720](https://github.com/plotly/dash-core-components/pull/720)
- `highlight.js` is now bundled into the package, and no longer sets the `window.hljs` variable. Similarly to how `plotly.js` is handled, it is overridden by a user-provided version if one exists.

## [1.6.0] - 2019-11-27
### Updated
- Upgraded plotly.js to 1.51.2 [#708](https://github.com/plotly/dash-core-components/pull/708)
Expand Down
4 changes: 3 additions & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ const presets = [
];

const plugins = [
'@babel/plugin-syntax-dynamic-import'
'@babel/plugin-syntax-dynamic-import',
'@babel/plugin-transform-async-to-generator',
'@babel/plugin-transform-runtime'
];

// eslint-disable-next-line no-process-env
Expand Down
5 changes: 1 addition & 4 deletions dash_core_components_base/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
'datepicker',
'dropdown',
'graph',
'highlight',
'markdown',
'upload'
]
Expand Down Expand Up @@ -70,10 +71,6 @@
} for async_resource in async_resources])

_js_dist.extend([
{
'relative_package_path': 'highlight.pack.js',
'namespace': 'dash_core_components'
},
Copy link
Contributor

@Marc-Andre-Rivet Marc-Andre-Rivet Dec 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

{
'relative_package_path': '{}.min.js'.format(__name__),
'external_url': (
Expand Down
2 changes: 0 additions & 2 deletions dash_core_components_base/highlight.pack.js

This file was deleted.

245 changes: 240 additions & 5 deletions package-lock.json

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

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"dependencies": {
"color": "^3.1.0",
"fast-isnumeric": "^1.1.3",
"highlight.js": "^9.17.1",
"moment": "^2.20.1",
"plotly.js": "1.51.3",
"prop-types": "^15.6.0",
Expand All @@ -57,6 +58,8 @@
"@babel/core": "^7.4.0",
"@babel/plugin-proposal-object-rest-spread": "^7.4.0",
"@babel/plugin-syntax-dynamic-import": "^7.2.0",
"@babel/plugin-transform-async-to-generator": "^7.7.4",
"@babel/plugin-transform-runtime": "^7.7.6",
"@babel/preset-env": "^7.4.1",
"@babel/preset-react": "^7.0.0",
"@plotly/dash-component-plugins": "^1.0.2",
Expand Down
20 changes: 13 additions & 7 deletions src/fragments/Markdown.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ import React, {Component} from 'react';
import {type} from 'ramda';
import Markdown from 'react-markdown';

import MarkdownHighlighter from '../utils/MarkdownHighlighter';
import {propTypes, defaultProps} from '../components/Markdown.react';
import '../components/css/highlight.css';

export default class DashMarkdown extends Component {
constructor(props) {
super(props);

if (MarkdownHighlighter.isReady !== true) {
MarkdownHighlighter.isReady.then(() => {
this.setState({});
});
}
this.highlightCode = this.highlightCode.bind(this);
this.dedent = this.dedent.bind(this);
}
Expand All @@ -21,15 +27,15 @@ export default class DashMarkdown extends Component {
}

highlightCode() {
if (!window.hljs) {
// skip highlighting if highlight.js isn't found
return;
}
if (this.mdContainer) {
const nodes = this.mdContainer.querySelectorAll('pre code');

for (let i = 0; i < nodes.length; i++) {
window.hljs.highlightBlock(nodes[i]);
if (MarkdownHighlighter.hljs) {
for (let i = 0; i < nodes.length; i++) {
MarkdownHighlighter.hljs.highlightBlock(nodes[i]);
}
} else {
MarkdownHighlighter.loadhljs();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the existing implementation does not take full advantage of the react-markdown ability to get its renderer overriden -- similarly to https://github.com/plotly/dash-core-components/pull/711/files#diff-5b56ed82805404e28302aef5147d04a2R129 it should be possible to hook up the highlight directly onto the code renderer and simplify this component / usage.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue created: #724, will not be addressed as part of this PR.

}
}
}
Expand Down
Loading