Add the ability to define a default language (#206)

* Add the ability to define a default language

I don't want to have to add the language i'm working on to every single block nor do I want to rely on a default heuristic to find the language.

This adds the ability to write

```
highlight: { defaultLang: 'xxxx' }
```

in `siteConfig.js` to force the language when not specified.

I tested it without a a highlight block, without a defaultLang attribute and with one and with a wrong one. All of them work as expected.

* Move comment around
This commit is contained in:
Christopher Chedeau 2017-11-01 12:16:15 -07:00 committed by Joel Marcey
parent ab169640d4
commit e9c5cef664

View file

@ -42,21 +42,24 @@ class Remarkable extends React.Component {
renderMarkdown(source) {
if (!this.md) {
const siteConfig = require(CWD + "/siteConfig.js");
this.md = new Markdown({
// Highlight.js expects hljs css classes on the code element.
// This results in <pre><code class="hljs css javascript">
langPrefix: 'hljs css ',
highlight: function (str, lang) {
lang = lang || (siteConfig.highlight && siteConfig.highlight.defaultLang);
if (lang && hljs.getLanguage(lang)) {
try {
return hljs.highlight(lang, str).value;
} catch (err) {}
}
try {
return hljs.highlightAuto(str).value;
} catch (err) {}
return '';
}
});
@ -65,7 +68,6 @@ class Remarkable extends React.Component {
this.md.use(anchors);
// Allow client sites to register their own plugins
const siteConfig = require(CWD + "/siteConfig.js");
if (siteConfig.markdownPlugins) {
siteConfig.markdownPlugins.forEach(function(plugin) {
this.md.use(plugin);
@ -96,4 +98,4 @@ Remarkable.defaultProps = {
options: {},
};
module.exports = Remarkable;
module.exports = Remarkable;