-
Notifications
You must be signed in to change notification settings - Fork 996
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
Conversation
Deploy preview ready! Built with commit 9693228 |
Oh my awesome work! Thanks so much. I’ll review this tomorrow |
js/src/lib/Details/Markdown.js
Outdated
@@ -1,6 +1,7 @@ | |||
import React from 'react'; | |||
import marked from 'marked'; | |||
import xss from 'xss'; | |||
import hljs from 'highlight.js'; |
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.
How much does this increase the bundle?
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.
Where can I check this?
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.
highlight.js is 20K without minification. Probably it will add +10K at least to the current website.
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.
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.
I’m wondering if there could be a way to have this only in the bundle where needed 🤔
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.
I think you're loading Highlight.js twice now - Once in _layouts/default.html
and once here.
I also think that https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js
contains every language supported by Highlight.js. I went to the Highlight.js site (https://highlightjs.org/download/) and downloaded a build that only supports Markdown, and it's only 9.4 KB minified / 4.8 KB minified and gzipped. That might be the best thing to do here? I wonder if you could do that using the npm package. Otherwise, I think downloading a minimal Highlight.js and sticking it in the repo is fine.
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.
I think we can start with supporting JS and CSS for now?
I think it introduced some errors in parsing inline html:
the bundle increased by about 800k, so I’m hoping we find a solution with less overhead. Can we limit the languages included and make sure it’s only loaded as needed (maybe with a dynamic import or something webpackey) ? |
Wow, that was unexpected! Probably is because I had to switch the place of xss, I'll try something else. |
Oh and I can’t say enough that I’m really grateful for you helping out! |
js/src/lib/Details/Markdown.js
Outdated
@@ -1,6 +1,7 @@ | |||
import React from 'react'; | |||
import marked from 'marked'; | |||
import xss from 'xss'; | |||
import hljs from 'highlight.js'; |
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.
I think you're loading Highlight.js twice now - Once in _layouts/default.html
and once here.
I also think that https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js
contains every language supported by Highlight.js. I went to the Highlight.js site (https://highlightjs.org/download/) and downloaded a build that only supports Markdown, and it's only 9.4 KB minified / 4.8 KB minified and gzipped. That might be the best thing to do here? I wonder if you could do that using the npm package. Otherwise, I think downloading a minimal Highlight.js and sticking it in the repo is fine.
_config.yml
Outdated
@@ -53,15 +53,10 @@ gems: | |||
markdown: kramdown | |||
kramdown: | |||
input: GFM | |||
syntax_highlighter: rouge | |||
syntax_highlighter: none |
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.
I'd strongly recomment keeping rouge
enabled. This is highlighting the code on the site as part of the build, rather than client-side. This means that JS is not required at all. We only really need the JS version for the package details page, everything else can do build-time highlighting. Disabling this means that every page that displays code needs to load the JS highlighter.
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.
Sure!
Here's webpack's docs on "code splitting": https://webpack.js.org/guides/code-splitting/. It also talks about dynamic imports. |
Thanks! I will try to fix later today! |
js/src/lib/Details/Markdown.js
Outdated
return marked(source, { renderer, mangle: false, sanitize: true }); | ||
const html = marked(source, { renderer, mangle: false }); | ||
return xss(html, { | ||
whiteList: Object.assign({}, xss.getDefaultWhiteList(), { |
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.
you can use spreading here if you want, it's supported by our babel config 😄
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.
Thanks! just changed.
js/src/lib/Details/Markdown.js
Outdated
@@ -1,7 +1,7 @@ | |||
import React from 'react'; | |||
import marked from 'marked'; | |||
import xss from 'xss'; | |||
import hljs from 'highlight.js'; | |||
import hljs from '../../../../_js/highlight/build/highlight.pack'; |
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.
It's probably simpler if we just include this file, WDYT @Daniel15 ?
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.
I'll try to split the code later, but I didn't understand really why to do that
scripts/highlight.sh
Outdated
OLD_CWD=$(pwd) | ||
cd $ROOT_PATH/_js/highlight | ||
rm -rf build/ &> /dev/null | ||
npm install |
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.
you can use yarn
here if it's run on Netlify, or we can track the built version, since this will probably almost never change 🤔
|
@Haroenv could you help me with the build? To use a smaller version of highlight.js we need to build it from source, but i'm stuck with netlify |
so I get it to work locally, still not fully sure why it doesn't work on Netlify. Can you see the build logs? |
I'm wondering what causes laggy horizontal scroll on http://127.0.0.1:4000/lang/en/package/?redux for example |
_js/highlight/.travis.yml
Outdated
- | ||
- BROWSER=1 | ||
- BROWSER=1 NOCOMPRESS=1 | ||
script: |
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.
Maybe a better place for this might be scripts/highlight
, WDYT?
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.
you can hoist all the dependencies to devdep in the main one I think, or use yarn workspaces
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.
I'll try to use the built version without submodules. Seems that netlify build process can't run git commands. Maybe I'm wrong
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.
Can i see travis logs?
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.
the logs are available if you click on the error normally
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.
Aaannndd it's alive! |
This is awesome, build size (obviously) increased a little bit (page is now 1.2MB over 1MB earlier). Comment I made earlier about scrolling sideways not being super smooth is still valid, and (I'm not sure if it's still worth it now) some way to split the bundle out of the main common-hash.js may be a good idea too |
Ok! Just give me sometime to study a bit more and I can fix that. |
Scrolling seems to be ok here. Can you upload a gif please? |
funny, scrolling wasn't laggy while recording, I guess it's okay then. |
we have the static highlight.js file now (okay for me), but maybe for reference (you can just write this in this PR), how is it generated? |
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.
LGTM now, @Daniel15 can you also review this still?
Sure. Done! |
Congrats on doing this, I'm really excited. We can see later what some ways of improving further can be 🔥 |
Nice! Thanks! |
@mtxr, what's your twitter handle? I accidentally mentioned someone else here: https://twitter.com/haroenv/status/930848700901089281 |
I don't really use twitter ;(, I have an account just for development purposes. You can mention with you want, it's @mtxr_dev, but if you prefer, you can refer my github profile ;) Thanks! |
Resolves #591 #246.
Static file generate following this guide
http://highlightjs.readthedocs.io/en/latest/building-testing.html
node tools/build.js -t javascript typescript css xml bash shell
Hope you enjoy.