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

added syntax highlighting for markdown codes #716

Merged
merged 23 commits into from
Nov 15, 2017
Merged
Show file tree
Hide file tree
Changes from 13 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
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[submodule "_sass/bootstrap"]
path = _sass/bootstrap
url = https://github.com/twbs/bootstrap.git

[submodule "_js/highlight"]
path = _js/highlight
url = https://github.com/isagalaev/highlight.js.git
1 change: 1 addition & 0 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ exclude:
- .sass-cache
- node_modules
- js/src
- "_js"

include:
- _redirects
Expand Down
1 change: 1 addition & 0 deletions _js/highlight
Submodule highlight added at cf4b46
1 change: 0 additions & 1 deletion _layouts/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
<link rel="stylesheet" href="https://cdn.jsdelivr.net/fontawesome/4.7.0/css/font-awesome.min.css" />
<link rel="stylesheet" href="{{ "/css/main.css" | prepend: site.baseurl }}?t={{ site.time | date_to_xmlschema }}">
<script src="https://cdn.polyfill.io/v2/polyfill.js?unknown=polyfill&excludes=Element"></script>

<meta name="google-site-verification" content="DIcCyEkVaGHm864NWzItnt2n6Gg7hz3l47RBIRyxvcQ" />
</head>
<body>
Expand Down
3 changes: 2 additions & 1 deletion _layouts/pages/install.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
{% highlight sh %}

{% endhighlight %}
<div class="language-sh highlighter-rouge"><pre class="rougeHighlight"><code>yarn --version</code></pre>
<div class="language-sh highlighter-rouge">
<pre class="rougeHighlight"><code>yarn --version</code></pre>
</div>

<h3>{{i18n.docs_nightly}}</h3>
Expand Down
147 changes: 71 additions & 76 deletions _sass/_code.scss
Original file line number Diff line number Diff line change
@@ -1,72 +1,74 @@
.rougeHighlight, pre { background-color: $code-bg; color: darken(#93a1a1, 15%) }
.rougeHighlight .c { color: #586e75 } /* Comment */
.rougeHighlight .err { color: darken(#93a1a1, 15%) } /* Error */
.rougeHighlight .g { color: darken(#93a1a1, 15%) } /* Generic */
.rougeHighlight .k { color: #859900 } /* Keyword */
.rougeHighlight .l { color: darken(#93a1a1, 15%) } /* Literal */
.rougeHighlight .n { color: darken(#93a1a1, 15%) } /* Name */
.rougeHighlight .o { color: #859900 } /* Operator */
.rougeHighlight .x { color: #cb4b16 } /* Other */
.rougeHighlight .p { color: darken(#93a1a1, 15%) } /* Punctuation */
.rougeHighlight .cm { color: #586e75 } /* Comment.Multiline */
.rougeHighlight .cp { color: #859900 } /* Comment.Preproc */
.rougeHighlight .c1 { color: #72c02c; } /* Comment.Single */
.rougeHighlight .cs { color: #859900 } /* Comment.Special */
.rougeHighlight .gd { color: #2aa198 } /* Generic.Deleted */
.rougeHighlight .ge { color: darken(#93a1a1, 15%); font-style: italic } /* Generic.Emph */
.rougeHighlight .gr { color: #dc322f } /* Generic.Error */
.rougeHighlight .gh { color: #cb4b16 } /* Generic.Heading */
.rougeHighlight .gi { color: #859900 } /* Generic.Inserted */
.rougeHighlight .go { color: darken(#93a1a1, 15%) } /* Generic.Output */
.rougeHighlight .gp { color: darken(#93a1a1, 15%) } /* Generic.Prompt */
.rougeHighlight .gs { color: darken(#93a1a1, 15%); font-weight: bold } /* Generic.Strong */
.rougeHighlight .gu { color: #cb4b16 } /* Generic.Subheading */
.rougeHighlight .gt { color: darken(#93a1a1, 15%) } /* Generic.Traceback */
.rougeHighlight .kc { color: #cb4b16 } /* Keyword.Constant */
.rougeHighlight .kd { color: #268bd2 } /* Keyword.Declaration */
.rougeHighlight .kn { color: #859900 } /* Keyword.Namespace */
.rougeHighlight .kp { color: #859900 } /* Keyword.Pseudo */
.rougeHighlight .kr { color: #268bd2 } /* Keyword.Reserved */
.rougeHighlight .kt { color: #dc322f } /* Keyword.Type */
.rougeHighlight .ld { color: darken(#93a1a1, 15%) } /* Literal.Date */
.rougeHighlight .m { color: #2aa198 } /* Literal.Number */
.rougeHighlight .s { color: #2aa198 } /* Literal.String */
.rougeHighlight .na { color: darken(#93a1a1, 15%) } /* Name.Attribute */
.rougeHighlight .nb { color: #B58900 } /* Name.Builtin */
.rougeHighlight .nc { color: #268bd2 } /* Name.Class */
.rougeHighlight .no { color: #cb4b16 } /* Name.Constant */
.rougeHighlight .nd { color: #268bd2 } /* Name.Decorator */
.rougeHighlight .ni { color: #cb4b16 } /* Name.Entity */
.rougeHighlight .ne { color: #cb4b16 } /* Name.Exception */
.rougeHighlight .nf { color: #268bd2 } /* Name.Function */
.rougeHighlight .nl { color: darken(#93a1a1, 15%) } /* Name.Label */
.rougeHighlight .nn { color: darken(#93a1a1, 15%) } /* Name.Namespace */
.rougeHighlight .nx { color: darken(#93a1a1, 15%) } /* Name.Other */
.rougeHighlight .py { color: darken(#93a1a1, 15%) } /* Name.Property */
.rougeHighlight .nt { color: #268bd2 } /* Name.Tag */
.rougeHighlight .nv { color: #268bd2 } /* Name.Variable */
.rougeHighlight .ow { color: #859900 } /* Operator.Word */
.rougeHighlight .w { color: darken(#93a1a1, 15%) } /* Text.Whitespace */
.rougeHighlight .mf { color: #2aa198 } /* Literal.Number.Float */
.rougeHighlight .mh { color: #2aa198 } /* Literal.Number.Hex */
.rougeHighlight .mi { color: #2aa198 } /* Literal.Number.Integer */
.rougeHighlight .mo { color: #2aa198 } /* Literal.Number.Oct */
.rougeHighlight .sb { color: #586e75 } /* Literal.String.Backtick */
.rougeHighlight .sc { color: #2aa198 } /* Literal.String.Char */
.rougeHighlight .sd { color: darken(#93a1a1, 15%) } /* Literal.String.Doc */
.rougeHighlight .s2 { color: #2aa198 } /* Literal.String.Double */
.rougeHighlight .se { color: #cb4b16 } /* Literal.String.Escape */
.rougeHighlight .sh { color: darken(#93a1a1, 15%) } /* Literal.String.Heredoc */
.rougeHighlight .si { color: #2aa198 } /* Literal.String.Interpol */
.rougeHighlight .sx { color: #2aa198 } /* Literal.String.Other */
.rougeHighlight .sr { color: #dc322f } /* Literal.String.Regex */
.rougeHighlight .s1 { color: #2aa198 } /* Literal.String.Single */
.rougeHighlight .ss { color: #2aa198 } /* Literal.String.Symbol */
.rougeHighlight .bp { color: #268bd2 } /* Name.Builtin.Pseudo */
.rougeHighlight .vc { color: #268bd2 } /* Name.Variable.Class */
.rougeHighlight .vg { color: #268bd2 } /* Name.Variable.Global */
.rougeHighlight .vi { color: #268bd2 } /* Name.Variable.Instance */
.rougeHighlight .il { color: #2aa198 } /* Literal.Number.Integer.Long */
.rougeHighlight pre,
.rougeHighlight, pre {

background: $gray-dark;
-webkit-font-smoothing: antialiased;
border-left: 4px solid $yarn-blue;
color: #ddd;
}
.rougeHighlight .hll { background-color: #272822; }
.rougeHighlight .c { color: #75715e } /* Comment */
.rougeHighlight .err { color: #960050; background-color: #1e0010 } /* Error */
.rougeHighlight .k { color: #66d9ef } /* Keyword */
.rougeHighlight .l { color: #ae81ff } /* Literal */
.rougeHighlight .n { color: #f8f8f2 } /* Name */
.rougeHighlight .o { color: #f92672 } /* Operator */
.rougeHighlight .p { color: #f8f8f2 } /* Punctuation */
.rougeHighlight .cm { color: #75715e } /* Comment.Multiline */
.rougeHighlight .cp { color: #75715e } /* Comment.Preproc */
.rougeHighlight .c1 { color: #75715e } /* Comment.Single */
.rougeHighlight .cs { color: #75715e } /* Comment.Special */
.rougeHighlight .ge { font-style: italic } /* Generic.Emph */
.rougeHighlight .gs { font-weight: bold } /* Generic.Strong */
.rougeHighlight .kc { color: #66d9ef } /* Keyword.Constant */
.rougeHighlight .kd { color: #66d9ef } /* Keyword.Declaration */
.rougeHighlight .kn { color: #f92672 } /* Keyword.Namespace */
.rougeHighlight .kp { color: #66d9ef } /* Keyword.Pseudo */
.rougeHighlight .kr { color: #66d9ef } /* Keyword.Reserved */
.rougeHighlight .kt { color: #66d9ef } /* Keyword.Type */
.rougeHighlight .ld { color: #e6db74 } /* Literal.Date */
.rougeHighlight .m { color: #ae81ff } /* Literal.Number */
.rougeHighlight .s { color: #e6db74 } /* Literal.String */
.rougeHighlight .na { color: #a6e22e } /* Name.Attribute */
.rougeHighlight .nb { color: #f8f8f2 } /* Name.Builtin */
.rougeHighlight .nc { color: #a6e22e } /* Name.Class */
.rougeHighlight .no { color: #66d9ef } /* Name.Constant */
.rougeHighlight .nd { color: #a6e22e } /* Name.Decorator */
.rougeHighlight .ni { color: #f8f8f2 } /* Name.Entity */
.rougeHighlight .ne { color: #a6e22e } /* Name.Exception */
.rougeHighlight .nf { color: #a6e22e } /* Name.Function */
.rougeHighlight .nl { color: #f8f8f2 } /* Name.Label */
.rougeHighlight .nn { color: #f8f8f2 } /* Name.Namespace */
.rougeHighlight .nx { color: #a6e22e } /* Name.Other */
.rougeHighlight .py { color: #f8f8f2 } /* Name.Property */
.rougeHighlight .nt { color: #f92672 } /* Name.Tag */
.rougeHighlight .nv { color: #f8f8f2 } /* Name.Variable */
.rougeHighlight .ow { color: #f92672 } /* Operator.Word */
.rougeHighlight .w { color: #f8f8f2 } /* Text.Whitespace */
.rougeHighlight .mf { color: #ae81ff } /* Literal.Number.Float */
.rougeHighlight .mh { color: #ae81ff } /* Literal.Number.Hex */
.rougeHighlight .mi { color: #ae81ff } /* Literal.Number.Integer */
.rougeHighlight .mo { color: #ae81ff } /* Literal.Number.Oct */
.rougeHighlight .sb { color: #e6db74 } /* Literal.String.Backtick */
.rougeHighlight .sc { color: #e6db74 } /* Literal.String.Char */
.rougeHighlight .sd { color: #e6db74 } /* Literal.String.Doc */
.rougeHighlight .s2 { color: #e6db74 } /* Literal.String.Double */
.rougeHighlight .se { color: #ae81ff } /* Literal.String.Escape */
.rougeHighlight .sh { color: #e6db74 } /* Literal.String.Heredoc */
.rougeHighlight .si { color: #e6db74 } /* Literal.String.Interpol */
.rougeHighlight .sx { color: #e6db74 } /* Literal.String.Other */
.rougeHighlight .sr { color: #e6db74 } /* Literal.String.Regex */
.rougeHighlight .s1 { color: #e6db74 } /* Literal.String.Single */
.rougeHighlight .ss { color: #e6db74 } /* Literal.String.Symbol */
.rougeHighlight .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */
.rougeHighlight .vc { color: #f8f8f2 } /* Name.Variable.Class */
.rougeHighlight .vg { color: #f8f8f2 } /* Name.Variable.Global */
.rougeHighlight .vi { color: #f8f8f2 } /* Name.Variable.Instance */
.rougeHighlight .il { color: #ae81ff } /* Literal.Number.Integer.Long */

.rougeHighlight .gu { color: #75715e; } /* Generic.Subheading & Diff Unified/Comment? */
.rougeHighlight .gd { color: #f92672; } /* Generic.Deleted & Diff Deleted */
.rougeHighlight .gi { color: #a6e22e; } /* Generic.Inserted & Diff Inserted */

.language-diff .gi {
color: $brand-success;
Expand All @@ -80,13 +82,6 @@
display: block;
}

.language-sh pre {
background: $gray-dark;
color: white;
-webkit-font-smoothing: antialiased;
border-left: 4px solid $yarn-blue;
}

pre {
max-width: 100%;
margin-bottom: 1.2rem;
Expand Down
76 changes: 76 additions & 0 deletions _sass/_highlight.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Monokai style - ported by Luigi Maselli - http://grigio.org
*/

.hljs {
background: #292b2c;
-webkit-font-smoothing: antialiased;
border-left: 4px solid #2C8EBB;
color: #ddd;
max-width: 100%;
margin-bottom: 1.2rem;
border-radius: 3px;
padding: .6rem 1.2rem;
overflow-x: auto;
-webkit-overflow-scrolling: touch;
}

.hljs-tag,
.hljs-keyword,
.hljs-selector-tag,
.hljs-literal,
.hljs-strong,
.hljs-name {
color: #f92672;
}

.hljs-code {
color: #66d9ef;
}

.hljs-class .hljs-title {
color: white;
}

.hljs-attribute,
.hljs-symbol,
.hljs-regexp,
.hljs-link {
color: #bf79db;
}

.hljs-string,
.hljs-bullet,
.hljs-subst,
.hljs-title,
.hljs-section,
.hljs-emphasis,
.hljs-type,
.hljs-built_in,
.hljs-builtin-name,
.hljs-selector-attr,
.hljs-selector-pseudo,
.hljs-addition,
.hljs-variable,
.hljs-template-tag,
.hljs-template-variable {
color: #a6e22e;
}

.hljs-comment,
.hljs-quote,
.hljs-deletion,
.hljs-meta {
color: #75715e;
}

.hljs-keyword,
.hljs-selector-tag,
.hljs-literal,
.hljs-doctag,
.hljs-title,
.hljs-section,
.hljs-type,
.hljs-selector-id {
font-weight: bold;
}
1 change: 1 addition & 0 deletions css/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ $npm-red: #C12127;
@import "_details.scss";
@import "_readmore.scss";
@import "_copyable.scss";
@import "_highlight.scss";

// should come from bootstrap but it doesn't
.justify-items-between {
Expand Down
26 changes: 23 additions & 3 deletions js/src/lib/Details/Markdown.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import marked from 'marked';
import xss from 'xss';
import hljs from '../../../../_js/highlight/build/highlight.pack';
Copy link
Member

Choose a reason for hiding this comment

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

It's probably simpler if we just include this file, WDYT @Daniel15 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll try to split the code later, but I didn't understand really why to do that


import { prefixURL } from '../util';

Expand Down Expand Up @@ -69,9 +70,28 @@ const renderAndEscapeMarkdown = ({ source, githubRepo }) => {
};
}

const html = marked(source, { renderer, mangle: false });
const escaped = xss(html);
return escaped;
renderer.code = function(code, lang) {
if (lang && hljs.getLanguage(lang)) {
try {
const prepared = hljs.highlight(lang, code);
return `<pre><code class="${prepared.language}">${prepared.value}</code></pre>`;
} catch (err) {}
}

try {
const prepared = hljs.highlightAuto(code);
return `<pre><code class="${prepared.language}">${prepared.value}</code></pre>`;
} catch (err) {}

return `<pre><code>${code}</code></pre>`;
};

return xss(marked(source, { renderer, mangle: false }), {
whiteList: Object.assign({}, xss.getDefaultWhiteList(), {
Copy link
Member

Choose a reason for hiding this comment

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

you can use spreading here if you want, it's supported by our babel config 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! just changed.

code: ['class'],
span: ['class'],
}),
});
};

const Markdown = ({ source, githubRepo }) => (
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
"scripts": {
"build": "webpack",
"build:sitemaps": "./scripts/sitemaps.js",
"build:production": "npm run build && npm run build:sitemaps",
"start": "webpack --watch",
"build:highlight.js": "./scripts/highlight.sh",
"build:production": "npm run build:highlight.js && npm run build && npm run build:sitemaps",
"start": "npm run build:highlight.js && webpack --watch",
"lint:staged": "lint-staged"
},
"lint-staged": {
Expand Down
10 changes: 10 additions & 0 deletions scripts/highlight.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

ROOT_PATH=$(python -c "import os,sys;print(os.path.dirname(os.path.realpath(sys.argv[1])))" $0)
ROOT_PATH=$(dirname $ROOT_PATH)
npm --prefix $ROOT_PATH/_js/highlight install
OLD_CWD=$(pwd)
cd $ROOT_PATH/_js/highlight
rm -rf build/ &> /dev/null
node tools/build.js css javascript markdown bash shell xml typescript scss
cd $OLD_CWD
Loading