pngwn / MDsveX

A markdown preprocessor for Svelte.
https://mdsvex.pngwn.io
MIT License
2.36k stars 102 forks source link

Custom syntax highlighter is never called #99

Closed illright closed 4 years ago

illright commented 4 years ago

Steps to reproduce:

// ...
import { mdsvex } from 'mdsvex';

export default {
  // ...
  plugins: [
    // ...
    svelte({
      extensions: ['.svelte', '.svx'],
      preprocess: mdsvex({
        highlight(code, _lang) {
          console.log('called');
          return `<pre>${code}</pre>`;
        }
      }),
    }),
// ...
```js
alert()


* Import the new file into `src/App.svelte`

## Expected

`called` will be printed anywhere signifying that the highlighter was called

## Actual

Nothing printed, the default highlighter is used

Also I'm wondering if there's any simple way of stripping the initial newline that is rendered in the `<pre>`
pngwn commented 4 years ago

Looks like I didn't update the docs. Highlight should be an object that accepts a highlighter property:

// ...
import { mdsvex } from 'mdsvex';

function highlighter(code, _lang) {
  console.log('called');
  return `<pre>${code}</pre>`;
}

export default {
  // ...
  plugins: [
    // ...
    svelte({
      extensions: ['.svelte', '.svx'],
      preprocess: mdsvex({
        highlight: { highlighter }
      }),
    }),
// ...

I'll update the documentation.

Can you give an example for the initial newline you mentioned?

illright commented 4 years ago

The example for the issue would be the default configuration. Follow the same reproduction steps, but don't override the highlighter. Then the code from test.svx will be rendered in a <pre> element spanning two lines, the first line being just a linefeed and the second containing actual code

TheComputerM commented 4 years ago

It looks like this: image

```js console.log('hello there') console.log('general kenobi') ```

The default settings were used

https://svelte-materialify.netlify.app/components/buttons/ The SVX file

TheComputerM commented 4 years ago

And the docs are still not updated

pngwn commented 4 years ago

Fixed in #110.

Thanks @TheComputerM!