Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable using Prism for syntax highlighting #735

Merged
merged 7 commits into from
Jun 9, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
28 changes: 28 additions & 0 deletions docs/api-doc-markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,31 @@ While Highlight.js provides support for [many popular languages out of the box](
}
}
```

### Using Prism.js as additional syntax highlighter

While highlight.js support a lot of languages, you can opt to use prism.js to syntax highlight certain languages available on [Prism](https://github.com/PrismJS/prism/tree/master/components). Include those languages in `usePrism` field in your [siteConfig.js](api-site-config.md)

Example:
```
// siteConfig.js
usePrism: ['jsx']
```

Notice that below code blocks use JSX syntax highlighting from PrismJS

```jsx
class Example extends React.Component {
render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Docusaurus</Text>
<Button
title="Click me"
onPress={() => this.props.navigation.push('Docusaurus')}
/>
</View>
);
}
}
```
2 changes: 2 additions & 0 deletions docs/api-site-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ h1 {

`users` - The `users` array mentioned earlier.

`usePrism` - Array of languages to use prism.js syntax highlighter. Refer to [Using PrismJS as additional syntax highlighter](api-doc-markdown.md#using-prismjs-as-additional-syntax-highlighter). Set it to `true` to use PrismJS on all languages.

`wrapPagesHTML` - Boolean flag to indicate whether `html` files in `/pages` should be wrapped with Docusaurus site styles, header and footer. This feature is experimental and relies on the files being `html` fragments instead of complete pages. It inserts the contents of your `html` file with no extra processing. Defaults to `false`.

Users can also add their own custom fields if they wish to provide some data across different files.
Expand Down
25 changes: 22 additions & 3 deletions lib/core/renderMarkdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,44 @@

const hljs = require('highlight.js');
const Markdown = require('remarkable');
const prismjs = require('prismjs');

const anchors = require('./anchors.js');

const CWD = process.cwd();

const alias = {
js: 'jsx',
};

class MarkdownRenderer {
constructor() {
const siteConfig = require(CWD + '/siteConfig.js');

const 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 ',
langPrefix: 'hljs css languages- ',
highlight: function(str, lang) {
lang =
lang || (siteConfig.highlight && siteConfig.highlight.defaultLang);
if (lang && hljs.getLanguage(lang)) {
if (lang) {
try {
return hljs.highlight(lang, str).value;
if (
siteConfig.usePrism === true ||
(siteConfig.usePrism && siteConfig.usePrism.indexOf(lang) !== -1)
Copy link
Contributor

Choose a reason for hiding this comment

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

Use siteConfig.usePrism.length > 0 to be safer.

) {
try {
const language = alias[lang] || lang;
// Currently people using prismjs on Node have to individually require()
// every single language (https://github.com/PrismJS/prism/issues/593)
require('prismjs/components/prism-' + language + '.min');
return prismjs.highlight(str, prismjs.languages[language]);
} catch (err) {}
}
if (hljs.getLanguage(lang)) {
return hljs.highlight(lang, str).value;
}
} catch (err) {}
}

Expand Down
131 changes: 131 additions & 0 deletions lib/static/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -1871,3 +1871,134 @@ footer .social {
flex-shrink: 0;
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

This increases the CSS payload to all Docusaurus users, even though not all of them will use it. Is it possible to lazy load the CSS if usePrism is true?

/**
* prism.js default theme for JavaScript, CSS and HTML
* Based on dabblet (http://dabblet.com)
* @author Lea Verou
*/

code[class*="language-"],
pre[class*="language-"] {
color: black;
background: none;
text-shadow: 0 1px white;
font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;
text-align: left;
white-space: pre;
word-spacing: normal;
word-break: normal;
word-wrap: normal;
line-height: 1.5;

-moz-tab-size: 4;
-o-tab-size: 4;
tab-size: 4;

-webkit-hyphens: none;
-moz-hyphens: none;
-ms-hyphens: none;
hyphens: none;
}

pre[class*="language-"]::-moz-selection, pre[class*="language-"] ::-moz-selection,
code[class*="language-"]::-moz-selection, code[class*="language-"] ::-moz-selection {
text-shadow: none;
}

pre[class*="language-"]::selection, pre[class*="language-"] ::selection,
code[class*="language-"]::selection, code[class*="language-"] ::selection {
text-shadow: none;
}

@media print {
code[class*="language-"],
pre[class*="language-"] {
text-shadow: none;
}
}

/* Code blocks */
pre[class*="language-"] {
padding: 1em;
margin: .5em 0;
overflow: auto;
}

/* Inline code */
:not(pre) > code[class*="language-"] {
padding: .1em;
border-radius: .3em;
white-space: normal;
}

.token.comment,
.token.prolog,
.token.doctype,
.token.cdata {
color: slategray;
}

.token.punctuation {
color: #999;
}

.namespace {
opacity: .7;
}

.token.property,
.token.tag,
.token.boolean,
.token.constant,
.token.symbol,
.token.deleted {
color: #905;
}

.token.selector,
.token.number,
.token.attr-name,
.token.string,
.token.char,
.token.builtin,
.token.inserted {
color: #690;
}

.token.operator,
.token.entity,
.token.url,
.language-css .token.string,
.style .token.string {
color: #9a6e3a;
}

.token.atrule,
.token.attr-value,
.token.keyword {
color: #07a;
}

.token.function,
.token.class-name {
color: #DD4A68;
}

.token.regex,
.token.important,
.token.variable {
color: #e90;
}

.token.important,
.token.bold {
font-weight: bold;
}
.token.italic {
font-style: italic;
}

.token.entity {
cursor: help;
}
Loading