Skip to content
This repository has been 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

Refactor markdown syntax highlighting. #720

merged 17 commits into from
Dec 20, 2019

Conversation

shammamah-zz
Copy link
Contributor

@shammamah-zz shammamah-zz commented Dec 17, 2019

Closes #713

Related to plotly/dash-table#606

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

export default class DashMarkdown extends Component {
constructor(props) {
super(props);
/* eslint-disable no-new */
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Disabling this error as we really only do want the side-effects of creating a new Highlight object. An alternative is wrapping all of the Highlight code with a function instead of a class.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you explain what's happening here?

static hljsResolve() {}

constructor() {
if(!Highlight.isReady) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Put this here to handle the case in which there are multiple dcc.Markdown elements that need syntax highlighting; we won't create new promises for each of them.

import highlightjs from 'highlight.js/lib/highlight';
import 'highlight.js/styles/github.css';

import bash from 'highlight.js/lib/languages/bash';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using all of the languages that are defined in the old highlight.pack.js file. We might want to standardize this set of languages so that dash-table and dash-core-components support the same languages.

Copy link
Contributor

@Marc-Andre-Rivet Marc-Andre-Rivet left a comment

Choose a reason for hiding this comment

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

Needs a changelog entry

{
'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.

🎉

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.

@Marc-Andre-Rivet
Copy link
Contributor

Snapshots involving dcc.Markdown are not consistent with how they were before -- they lack styling. This may be a timing issue or a new bug: https://percy.io/plotly/dash-core-components/builds/3530615/view/215175776/1280?mode=diff&browser=chrome&snapshot=215175776

Would like to see a test similar to what was done with the table. Behavior with and without highlighting override.

@shammamah-zz
Copy link
Contributor Author

@Marc-Andre-Rivet It looks like the Percy diffs are because I am importing a stylesheet from highlight.js that overrides the highlight.css stylesheet we have in this repo, which has styles taken from the arduino-light and monokai stylesheets from the library. (https://github.com/plotly/dash-core-components/blob/dev/src/components/css/highlight.css)

cc @wbrgss as this might be relevant to DDK. For now I've just removed the stylesheet import and it should look the same as before.

@wbrgss
Copy link
Contributor

wbrgss commented Dec 19, 2019

@shammamah I'm actually theming the highlight.css based on the DDK theme. It's in
override-highlight-syntax.css.js (private repo).

Does that make sense? I wonder if I have to increase the specificity of those files' rules given your change.

@shammamah-zz
Copy link
Contributor Author

@wbrgss That makes sense! I have no problem leaving out the highlight.js stylesheet. It should work exactly as before.

@@ -2,12 +2,19 @@ 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';
Copy link
Contributor

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

Choose a reason for hiding this comment

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

If we're to keep this custom css, might as well import it dynamically when we request hljs instead of getting it dynamically with the markdown component.

@@ -0,0 +1,18 @@
import lazyhljs from './LazyLoader/hljs';
import '../components/css/highlight.css';
Copy link
Contributor

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

Choose a reason for hiding this comment

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

Better, than it the Markdown component, but I would push it even further down the stack, right down to src/third-party/highlight.js, that is, load it only if we require highlighting.

app.layout = html.Div(dcc.Markdown(md_text))
dash_dcc.start_server(app)

dash_dcc.driver.execute_script('window.hljs = {highlightBlock: (block) => {block.innerHTML="hljs override"}};')
Copy link
Contributor

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

Choose a reason for hiding this comment

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

Here we assign window.hljs but nothing in this test demonstrate that it is being used / overrides the dcc behavior.
Could we add a dash_dcc.percy_snapshot('<name of snapshot>') or similar to see that the result is what was forced.

dash_dcc.driver.execute_script('window.hljs = {highlightBlock: (block) => {block.innerHTML="hljs override"}};')

assert dash_dcc.find_element('code').text == 'hljs override'
dash_dcc.percy_snapshot('md_code_highlight_override')
Copy link
Contributor

Choose a reason for hiding this comment

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

🎉

Copy link
Contributor

@Marc-Andre-Rivet Marc-Andre-Rivet left a comment

Choose a reason for hiding this comment

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

:shipit: 💃

@shammamah-zz shammamah-zz merged commit 39cfdfc into dev Dec 20, 2019
@shammamah-zz shammamah-zz deleted the 713-highlightjs branch December 20, 2019 16:44
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bundle highlight.js into dcc
3 participants