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: You have a good example? #1

Closed
raguay opened this issue May 17, 2019 · 18 comments
Closed

Code Highlighting: You have a good example? #1

raguay opened this issue May 17, 2019 · 18 comments
Labels

Comments

@raguay
Copy link

raguay commented May 17, 2019

I'm using MDsveX to create the documentation for my Svelte webapp. But, I'm running into difficulties getting the code highlighting to work. I'm using the highlight.js module and I can see everything loading into the browser, but the code isn't being highlighted. If you have a working example, I can most likely figure where I went wrong from it. Thanks.

@pngwn
Copy link
Owner

pngwn commented May 17, 2019

All markdown-it options can be passed in as an option, so you would do this in your rollup or webpack config via the markdownOptions. This is the markdown-it example from their documentation and seems to work from my simple test:

preprocess: mdsvex({
  markdownOptions: {
    highlight: function (str, lang) {
      if (lang && hljs.getLanguage(lang)) {
        try {
          return hljs.highlight(lang, str).value;
        } catch (__) {}
      }
      return ''; // use external default escaping
    }
  }
})

Remember, this will only generate the correct HTML for the highlighting to work, you will still need to include the correct CSS files on the page to actually see the highlighting.

If this doesn't work for you, post your bundler config and I'll take a look. I'll try to get an example or two up over the weekend as well.

@raguay
Copy link
Author

raguay commented May 18, 2019

Okay, I have it halfway working now. When I run it in npm run dev it looks great except for not line wrapping. But, when I build it has the first line to the left and all other lines way to the right. Very weird. Both ways has no line wrapping. I would like to have line wrapping on the markdown examples. Is there a config for that? I'm figuring something isn't getting loaded on the full build or the highlightjs has some funny stuff when minimized (CodeMirror is similar issues with some of it's addon).

@pngwn
Copy link
Owner

pngwn commented May 18, 2019

Can you show me the HTML for the code blocks after they have been parsed?

@raguay
Copy link
Author

raguay commented May 18, 2019 via email

@pngwn
Copy link
Owner

pngwn commented May 18, 2019

I just did some investigation and this looks like a Svelte bug, it is adding whitespace when it generates the code.

sveltejs/svelte#2813

@raguay
Copy link
Author

raguay commented May 18, 2019 via email

@pngwn
Copy link
Owner

pngwn commented May 18, 2019

This is because svelte generates slightly different code for dev: true and dev:false. Even if you remove terser (which minimises the code) this still happens.

@raguay
Copy link
Author

raguay commented May 18, 2019 via email

@pngwn
Copy link
Owner

pngwn commented Jun 2, 2019

Just to update on this, if you set hydratable: true in your bundler config, you won't have this problem as it generates slightly different code.

Not a solution but a workaround until this is fixed in Svelte.

@raguay
Copy link
Author

raguay commented Jun 3, 2019 via email

@pngwn
Copy link
Owner

pngwn commented Jun 3, 2019

I'm not sure what you mean, sorry. Could you give an example?

@raguay
Copy link
Author

raguay commented Jun 3, 2019 via email

@pngwn
Copy link
Owner

pngwn commented Jun 4, 2019

Like I said earlier, MDsveX doesn't handle the actual markdown parsing, so I don't have control over how markdown-it or your chosen syntax highlighter parse and render the HTML. This is probably something that could be done with some kind of plugin at a guess, but I'm not entirely sure.

@raguay
Copy link
Author

raguay commented Jun 4, 2019 via email

@pngwn pngwn closed this as completed Jul 22, 2019
@louwers
Copy link

louwers commented Dec 22, 2019

Can this be re-opened? I was about to open a similar issue.

This piece of documentation is not very helpful!

        markdownOptions: {
          typographer: true,
          linkify: true,
          highlight: (str, lang) => whatever(str, lang), // this should be a real function if you want to highlight
        },

@pngwn
Copy link
Owner

pngwn commented Dec 22, 2019

New documentation will be published soon (with the new version). It will include improved highlighting documentation (the new version will also support automatic highlighting for most languages).

@pngwn pngwn reopened this Dec 22, 2019
@pngwn
Copy link
Owner

pngwn commented May 25, 2020

The latest mdsvex version highlights pretty much everything with no configuration and a custom highlighter can be used if you prefer.

This is documented at https://mdsvex.com

@pngwn pngwn closed this as completed May 25, 2020
@raguay
Copy link
Author

raguay commented May 26, 2020 via email

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

No branches or pull requests

3 participants