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

Conversation

mtxr
Copy link
Contributor

@mtxr mtxr commented Nov 11, 2017

Resolves #591 #246.

  • Added highlight.js lib for markdown syntax highlighting on details page.
    Static file generate following this guide http://highlightjs.readthedocs.io/en/latest/building-testing.html
    • Use command: node tools/build.js -t javascript typescript css xml bash shell
  • Added monokai (well know, highly recommend throught the web) theme as default theme for syntax highlighting

Hope you enjoy.

@Haroenv
Copy link
Member

Haroenv commented Nov 11, 2017

Deploy preview ready!

Built with commit 9693228

https://deploy-preview-716--yarnpkg.netlify.com

@mtxr
Copy link
Contributor Author

mtxr commented Nov 11, 2017

Syntax highlight is now working for the details page.

Also, I applied the same theme for rougeHighligther and highlight.js so all markdowns will be using the same style.

screen shot 2017-11-11 at 16 38 50

@Haroenv
Copy link
Member

Haroenv commented Nov 12, 2017

Oh my awesome work! Thanks so much. I’ll review this tomorrow

@@ -1,6 +1,7 @@
import React from 'react';
import marked from 'marked';
import xss from 'xss';
import hljs from 'highlight.js';
Copy link
Member

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?

Copy link
Contributor Author

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?

Copy link
Contributor Author

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.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

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 🤔

Copy link
Member

@Daniel15 Daniel15 Nov 13, 2017

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.

Copy link
Member

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?

@Haroenv
Copy link
Member

Haroenv commented Nov 12, 2017

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) ?

@mtxr
Copy link
Contributor Author

mtxr commented Nov 12, 2017

Wow, that was unexpected!

Probably is because I had to switch the place of xss, I'll try something else.
About the dynamic import, I need to study a bit more, React is not my fw :(

@Haroenv
Copy link
Member

Haroenv commented Nov 12, 2017

Oh and I can’t say enough that I’m really grateful for you helping out!

@@ -1,6 +1,7 @@
import React from 'react';
import marked from 'marked';
import xss from 'xss';
import hljs from 'highlight.js';
Copy link
Member

@Daniel15 Daniel15 Nov 13, 2017

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
Copy link
Member

@Daniel15 Daniel15 Nov 13, 2017

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure!

@Daniel15
Copy link
Member

Here's webpack's docs on "code splitting": https://webpack.js.org/guides/code-splitting/. It also talks about dynamic imports.

@mtxr
Copy link
Contributor Author

mtxr commented Nov 13, 2017

Thanks! I will try to fix later today!

return marked(source, { renderer, mangle: false, sanitize: true });
const html = marked(source, { renderer, mangle: false });
return xss(html, {
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.

@@ -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';
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

OLD_CWD=$(pwd)
cd $ROOT_PATH/_js/highlight
rm -rf build/ &> /dev/null
npm install
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 yarn here if it's run on Netlify, or we can track the built version, since this will probably almost never change 🤔

@Haroenv
Copy link
Member

Haroenv commented Nov 13, 2017

> ./scripts/highlight.sh
3:21:29 PM: module.js:471
    throw err;
    ^

Error: Cannot find module 'gear'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/opt/build/repo/_js/highlight/tools/build.js:65:32)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)

@mtxr
Copy link
Contributor Author

mtxr commented Nov 13, 2017

@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

@Haroenv
Copy link
Member

Haroenv commented Nov 13, 2017

so I get it to work locally, still not fully sure why it doesn't work on Netlify. Can you see the build logs?

@Haroenv
Copy link
Member

Haroenv commented Nov 13, 2017

I'm wondering what causes laggy horizontal scroll on http://127.0.0.1:4000/lang/en/package/?redux for example

-
- BROWSER=1
- BROWSER=1 NOCOMPRESS=1
script:
Copy link
Member

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?

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 hoist all the dependencies to devdep in the main one I think, or use yarn workspaces

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 use the built version without submodules. Seems that netlify build process can't run git commands. Maybe I'm wrong

Copy link
Contributor Author

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?

Copy link
Member

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

Copy link
Member

Choose a reason for hiding this comment

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

@mtxr
Copy link
Contributor Author

mtxr commented Nov 13, 2017

Aaannndd it's alive!

@Haroenv
Copy link
Member

Haroenv commented Nov 13, 2017

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

@mtxr
Copy link
Contributor Author

mtxr commented Nov 13, 2017

Ok! Just give me sometime to study a bit more and I can fix that.

@mtxr
Copy link
Contributor Author

mtxr commented Nov 13, 2017

Scrolling seems to be ok here. Can you upload a gif please?

@Haroenv
Copy link
Member

Haroenv commented Nov 13, 2017

funny, scrolling wasn't laggy while recording, I guess it's okay then.

@Haroenv
Copy link
Member

Haroenv commented Nov 13, 2017

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?

Copy link
Member

@Haroenv Haroenv left a 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?

@mtxr
Copy link
Contributor Author

mtxr commented Nov 13, 2017

Sure.

Done!

@Haroenv Haroenv merged commit 3cd9aad into yarnpkg:master Nov 15, 2017
@Haroenv
Copy link
Member

Haroenv commented Nov 15, 2017

Congrats on doing this, I'm really excited. We can see later what some ways of improving further can be 🔥

@mtxr
Copy link
Contributor Author

mtxr commented Nov 15, 2017

Nice! Thanks!

@Haroenv
Copy link
Member

Haroenv commented Nov 15, 2017

@mtxr, what's your twitter handle? I accidentally mentioned someone else here: https://twitter.com/haroenv/status/930848700901089281

screen shot 2017-11-15 at 18 30 46

@mtxr
Copy link
Contributor Author

mtxr commented Nov 15, 2017

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants