metonym / svelte-highlight

Syntax Highlighting for Svelte using highlight.js
https://svhe.onrender.com
MIT License
253 stars 13 forks source link

Auto new line #265

Closed nicholasmordecai closed 1 year ago

nicholasmordecai commented 1 year ago

not an issue - possible a feature request?

Not sure if this is even possible with highlight.js, but is there any way to automatically format code? For example:

const code = `const add = (a: number, b: number) => a + b;`;

Produces:

Screenshot 2023-01-24 at 12 46 18
const code = `const add = (a: number, b: number) => a + b; let foo: string = 'bar';`;

Produces:

Screenshot 2023-01-24 at 12 46 55

But I have a unique use case where it would be advantageous for it to look like this:

Screenshot 2023-01-24 at 12 47 58

I was able to achieve this by using a new line string:

const code = `const add = (a: number, b: number) => a + b; \nlet foo: string = 'bar';`;

But it's a little clunky.

Maybe I'm asking for the earth and this is far out of the scope of highlight.js / svelte-highlight but was wondering if you knew if this was possible?

Cheers

geerew commented 1 year ago

Im not the developer but I wouldn't recommend implmenting a feature like this as there are many scenarios whereby this would incorrectly split, for example

There would be other edge cases too

Just my 2 cents

metonym commented 1 year ago

I agree with @geerew. I think that code formatting is beyond the scope of this library.

I would recommend looking at Prettier to format code, which can run in both Node and the browser.

Note that using Prettier in the browser will increase the JS bundle size by a significant amount; I wrote a Svelte preprocessor that both formats and highlights code in Node.js to avoid the runtime costs of shipping both highlight.js and prettier.