-
-
Notifications
You must be signed in to change notification settings - Fork 145
Refactor markdown syntax highlighting. #720
Changes from 2 commits
9d9aa6e
103fcb5
f679951
cc70aed
6c98c28
f80bf28
b2893ed
b679a6a
dff9f8c
6bad06d
37d34eb
3ca7aa2
a3fb701
3ebf656
78aaeda
0fd28a0
a2ba931
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,18 @@ import React, {Component} from 'react'; | |
import {type} from 'ramda'; | ||
import Markdown from 'react-markdown'; | ||
|
||
import Highlight from '../utils/Highlight'; | ||
import {propTypes, defaultProps} from '../components/Markdown.react'; | ||
import '../components/css/highlight.css'; | ||
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. 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. |
||
|
||
export default class DashMarkdown extends Component { | ||
constructor(props) { | ||
super(props); | ||
/* eslint-disable no-new */ | ||
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. Disabling this error as we really only do want the side-effects of creating a new 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. Can you explain what's happening here? |
||
new Highlight(); | ||
if (Highlight.isReady !== true) { | ||
Highlight.isReady.then(() => { this.setState({}); }) | ||
} | ||
this.highlightCode = this.highlightCode.bind(this); | ||
this.dedent = this.dedent.bind(this); | ||
} | ||
|
@@ -21,16 +27,14 @@ 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(Highlight.hljs) { | ||
for (let i = 0; i < nodes.length; i++) { | ||
Highlight.hljs.highlightBlock(nodes[i]); | ||
} | ||
} else { Highlight.loadhljs(); } | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import LazyLoader from './LazyLoader'; | ||
|
||
export default class Highlight { | ||
|
||
static hljsResolve() {} | ||
|
||
constructor() { | ||
if(!Highlight.isReady) { | ||
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. Put this here to handle the case in which there are multiple |
||
Highlight.isReady = new Promise(resolve => { | ||
Highlight.hljsResolve = resolve; | ||
}); | ||
} | ||
} | ||
|
||
static async loadhljs() { | ||
Highlight.hljs = await LazyLoader.hljs(); | ||
Highlight.hljsResolve(); | ||
Highlight.isReady = true; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import highlightjs from 'highlight.js/lib/highlight'; | ||
import 'highlight.js/styles/github.css'; | ||
|
||
import bash from 'highlight.js/lib/languages/bash'; | ||
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. Using all of the languages that are defined in the old |
||
import css from 'highlight.js/lib/languages/css'; | ||
import http from 'highlight.js/lib/languages/http'; | ||
import javascript from 'highlight.js/lib/languages/javascript'; | ||
import json from 'highlight.js/lib/languages/json'; | ||
import markdown from 'highlight.js/lib/languages/markdown'; | ||
import python from 'highlight.js/lib/languages/python'; | ||
import r from 'highlight.js/lib/languages/r'; | ||
import ruby from 'highlight.js/lib/languages/ruby'; | ||
import shell from 'highlight.js/lib/languages/shell'; | ||
import sql from 'highlight.js/lib/languages/sql'; | ||
import xml from 'highlight.js/lib/languages/xml'; | ||
import yaml from 'highlight.js/lib/languages/yaml'; | ||
|
||
highlightjs.registerLanguage('bash', bash); | ||
highlightjs.registerLanguage('css', css); | ||
highlightjs.registerLanguage('http', http); | ||
highlightjs.registerLanguage('javascript', javascript); | ||
highlightjs.registerLanguage('json', json); | ||
highlightjs.registerLanguage('markdown', markdown); | ||
highlightjs.registerLanguage('python', python); | ||
highlightjs.registerLanguage('r', r); | ||
highlightjs.registerLanguage('ruby', ruby); | ||
highlightjs.registerLanguage('shell', shell); | ||
highlightjs.registerLanguage('sql', sql); | ||
highlightjs.registerLanguage('xml', xml); | ||
highlightjs.registerLanguage('yaml', yaml); | ||
|
||
export default highlightjs; |
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.
🎉