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

Code Highlighting Unreadable #26

Closed
CodeLenny opened this issue Jan 6, 2017 · 14 comments
Closed

Code Highlighting Unreadable #26

CodeLenny opened this issue Jan 6, 2017 · 14 comments

Comments

@CodeLenny
Copy link
Contributor

I tried to include a code snippet, but found that the colors chosen for highlighting render strings nearly invisible.

screenshot from 2017-01-06 16-08-04

And it's even harder to see at a normal resolution:
screenshot from 2017-01-06 16-07-43

Would it be possible to change the color scheme to make snippets more readable? It looks like the grey background is assigned by foundation to all code blocks, but hljs is using a monoki theme for the foreground colors.

The code block is .lang-js. Would it be possible to select code[class^="lang-"], code[class*=" lang-"] and set the background color to a monoki background, or have some other fix?

<div class="doc-copy">
                <section class="swagger-operation-description"> <pre><code class="lang-js">echo(<span class="hljs-string">"Hello World"</span>);
</code></pre>
                  <!-- text removed -->
                    </section>
              </div>

Thanks!

@CodeLenny
Copy link
Contributor Author

In addition, I just noticed that multi-line snippets aren't highlighted in a continuous block.

screenshot from 2017-01-06 16-38-25

@auscaster
Copy link
Member

Thanks for the bug report, I'm current not using code blocks in descriptions so haven't come across this issue yet. I'll try to find a solution when time permits, or would welcome a PR in the mean time.

@CodeLenny
Copy link
Contributor Author

CodeLenny commented Jan 8, 2017

Thanks! Would you have a more elegant solution than a new CSS rule that selects code[class^="lang-"], code[class*=" lang-"] and sets the background to Monokai's background? I could PR that CSS rule.

@auscaster
Copy link
Member

That would be an option, but I would prefer the flexibility of a CLI option so you could specify the highlight theme at compile time, such as c=monokai or similar. Also, a couple of CSS tweaks to stop foundation getting in the way might also be necessary. Feel free to have a go, or I'll try to find some time during the week.

@CodeLenny
Copy link
Contributor Author

Foundation is currently highlighting all <code> snippets in the light grey color, instead of using the code block theme. Would you like to keep that, or change all <code> snippets to be using the code highlighter's style? (my ugly lang- selector is only needed to select code snippets that have syntax highlighting, universally highlighting all blocks would be nice and simple)

I'm happy to add Monokai's background to the Monokai file and tweak Foundation's CSS, but might leave the CLI option to choose a highlighting style for you.

@auscaster
Copy link
Member

Sounds greeat, would welcome the PR :)

@auscaster
Copy link
Member

auscaster commented Jan 9, 2017

Just to clarify though, only code blocks inside pre tags should be hilighted - inline code blocks should retain simple grey markup

@CodeLenny
Copy link
Contributor Author

@auscaster Here's what I'm seeing:

  1. Inline code ("Hello World, this is a `test`.") produce
<p>Hello World, this is a <code>test</code>.</p>
  1. Code blocks without a language
\```
test
\```

Produce

<pre><code><span class="hljs-built_in">test</span>
</code></pre>
  1. Code blocks with a language
\```js
echo("Hello World");
\```

Produce

<pre><code class="lang-js">echo(<span class="hljs-string">"Hello World"</span>);
</code></pre>

So the CSS selector I proposed only targets multi-line code blocks that have selected a language for highlighting (case 3). All others will default to the standard Foundation styling.

Sounds good?

@CodeLenny
Copy link
Contributor Author

CodeLenny commented Jan 9, 2017

Oh, I just read through the Monoki docs, and found that there's already the following rule:

.hljs {
  display: block;
  overflow-x: auto;
  padding: 0.5em;
  background: #23241f;
}

Looks like the hljs class isn't getting set. Should the templates be modified to insert .hljs, or should the CSS be changed to match the existing code?

@auscaster
Copy link
Member

auscaster commented Jan 9, 2017 via email

@CodeLenny
Copy link
Contributor Author

@auscaster I'm less familiar with your JavaScript setup than quickly tweaking the CSS files. It might be easier for you to do it yourself, but I'm happy to see what I can do if I'm pointed in the right direction.

It looks like HighlightJS isn't inserting the hljs class. Should that be inserted into the output?

I found the following methods in common.js which look applicable. I would like to confirm that these should be edited before continuing, to make sure the changes meet your requirements.

var cheerio = require('cheerio');
var marked = require('marked');
var highlight = require('highlight.js');

var common = {
  highlight: function(code, name) {
      var highlighted;
      if (name) {
          highlighted = highlight.highlight(name, code).value;
      } else {
          highlighted = highlight.highlightAuto(code).value;
      }
      return highlight.fixMarkup(highlighted); //highlighted; //
  },

  /**
   * Render a markdown formatted text as HTML.
   * @param {string} `value` the markdown-formatted text
   * @param {boolean} `stripParagraph` the marked-md-renderer wraps generated HTML in a <p>-tag by default.
   *      If this options is set to true, the <p>-tag is stripped.
   * @returns {string} the markdown rendered as HTML.
   */
  markdown: function(value, stripParagraph) {
     if (!value) {
         return value;
     }
     var html = marked(value);
     // We strip the surrounding <p>-tag, if
     if (stripParagraph) {
         var $ = cheerio("<root>" + html + "</root>");
         // Only strip <p>-tags and only if there is just one of them.
         if ($.children().length === 1 && $.children('p').length === 1) {
             html = $.children('p').html();
         }
     }
     return html;
  },

Thanks!

@CodeLenny
Copy link
Contributor Author

Actually, after a little digging, I found someone else had added the hljs class by changing the Marked options. Would you like me to implement a similar change?

evancz/elm-markdown@ae1d137

@auscaster
Copy link
Member

auscaster commented Jan 9, 2017 via email

@CodeLenny
Copy link
Contributor Author

@auscaster I've made the change and PRed. I've tested it on my API docs, and it works for me.

alvaromoo pushed a commit to etsfactory/spectacle-docs-etsfactory that referenced this issue Jul 26, 2017
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

No branches or pull requests

2 participants