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

Commit 39cfdfc

Browse files
author
Shammamah Hossain
authored
Refactor markdown syntax highlighting. (#720)
* Refactor markdown syntax highlighting. * Disable eslint error. * Fix formatting. * Fix naming and file locations. * Change MarkdownHighlighter to be an object. * Update CHANGELOG. * Add plugins for async functions. * Remove github stylesheet. * Move highlight stylesheet import. * Update hljs lazy loading. * Add markdown highlighting test. * Move stylesheet import to highlight.js. * Add percy snapshot to test and fix window override test. * Change assert to wait_for_text_to_equal. * Use click to trigger md render in test.
1 parent 4dc909f commit 39cfdfc

File tree

11 files changed

+365
-19
lines changed

11 files changed

+365
-19
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1717
- [#706](https://github.com/plotly/dash-core-components/pull/706)
1818
- Upgraded plotly.js to [1.51.3](https://github.com/plotly/plotly.js/releases/tag/v1.51.3)
1919

20+
### Changed
21+
- [#720](https://github.com/plotly/dash-core-components/pull/720)
22+
- `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.
23+
2024
## [1.6.0] - 2019-11-27
2125
### Updated
2226
- Upgraded plotly.js to 1.51.2 [#708](https://github.com/plotly/dash-core-components/pull/708)

babel.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ const presets = [
44
];
55

66
const plugins = [
7-
'@babel/plugin-syntax-dynamic-import'
7+
'@babel/plugin-syntax-dynamic-import',
8+
'@babel/plugin-transform-async-to-generator',
9+
'@babel/plugin-transform-runtime'
810
];
911

1012
// eslint-disable-next-line no-process-env

dash_core_components_base/__init__.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
'datepicker',
4444
'dropdown',
4545
'graph',
46+
'highlight',
4647
'markdown',
4748
'upload'
4849
]
@@ -70,10 +71,6 @@
7071
} for async_resource in async_resources])
7172

7273
_js_dist.extend([
73-
{
74-
'relative_package_path': 'highlight.pack.js',
75-
'namespace': 'dash_core_components'
76-
},
7774
{
7875
'relative_package_path': '{}.min.js'.format(__name__),
7976
'external_url': (

dash_core_components_base/highlight.pack.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

package-lock.json

Lines changed: 240 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"dependencies": {
3838
"color": "^3.1.0",
3939
"fast-isnumeric": "^1.1.3",
40+
"highlight.js": "^9.17.1",
4041
"moment": "^2.20.1",
4142
"plotly.js": "1.51.3",
4243
"prop-types": "^15.6.0",
@@ -57,6 +58,8 @@
5758
"@babel/core": "^7.4.0",
5859
"@babel/plugin-proposal-object-rest-spread": "^7.4.0",
5960
"@babel/plugin-syntax-dynamic-import": "^7.2.0",
61+
"@babel/plugin-transform-async-to-generator": "^7.7.4",
62+
"@babel/plugin-transform-runtime": "^7.7.6",
6063
"@babel/preset-env": "^7.4.1",
6164
"@babel/preset-react": "^7.0.0",
6265
"@plotly/dash-component-plugins": "^1.0.2",

src/fragments/Markdown.react.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@ import React, {Component} from 'react';
22
import {type} from 'ramda';
33
import Markdown from 'react-markdown';
44

5+
import MarkdownHighlighter from '../utils/MarkdownHighlighter';
56
import {propTypes, defaultProps} from '../components/Markdown.react';
6-
import '../components/css/highlight.css';
77

88
export default class DashMarkdown extends Component {
99
constructor(props) {
1010
super(props);
11+
12+
if (MarkdownHighlighter.isReady !== true) {
13+
MarkdownHighlighter.isReady.then(() => {
14+
this.setState({});
15+
});
16+
}
1117
this.highlightCode = this.highlightCode.bind(this);
1218
this.dedent = this.dedent.bind(this);
1319
}
@@ -21,15 +27,15 @@ export default class DashMarkdown extends Component {
2127
}
2228

2329
highlightCode() {
24-
if (!window.hljs) {
25-
// skip highlighting if highlight.js isn't found
26-
return;
27-
}
2830
if (this.mdContainer) {
2931
const nodes = this.mdContainer.querySelectorAll('pre code');
3032

31-
for (let i = 0; i < nodes.length; i++) {
32-
window.hljs.highlightBlock(nodes[i]);
33+
if (MarkdownHighlighter.hljs) {
34+
for (let i = 0; i < nodes.length; i++) {
35+
MarkdownHighlighter.hljs.highlightBlock(nodes[i]);
36+
}
37+
} else {
38+
MarkdownHighlighter.loadhljs();
3339
}
3440
}
3541
}

0 commit comments

Comments
 (0)