-
Notifications
You must be signed in to change notification settings - Fork 103
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
Custom highlight function doesn't respect whitespace in code blocks #212
Comments
I wonder why this is happening. Will look into it. |
@michaeloliverx thanks for sharing your code snippet, it gave me a nice starting point. Sharing my config here as I am consuming the // mdsvex.config.js
import shiki from "shiki";
import { escapeSvelte } from "mdsvex";
const config = {
"extensions": [".svelte.md", ".md", ".svx"],
highlight: {
highlighter: async (code, lang = "text") => {
const highlighter = await shiki.getHighlighter({ theme: "github-dark" });
const highlightedCode = escapeSvelte(highlighter.codeToHtml(code, lang));
return `{@html \`${highlightedCode}\` }`;
},
},
"smartypants": {
"dashes": "oldschool",
},
"remarkPlugins": [],
"rehypePlugins": [],
};
export default config; |
@jthegedus I am glad it helped! Since then I have been using Take the following markdown: ```ts twoslash
type Post = {
title: string;
description: string;
};
function getPosts(): Array<Post>{
return []
}
const posts = getPosts();
``` Generates code samples with typescript hints on mouse hover: You can also highlight lines with the following syntax: ```ts twoslash
```ts twoslash {1-4}
type Post = {
title: string;
description: string;
};
function getPosts(): Array<Post>{
return []
}
const posts = getPosts();
```
``` It works on non JS/TS code samples too just omit the ```ts {1-4} filename="some-file.ts"
type Post = {
title: string;
description: string;
};
...
``` They will be added to the Anyway enough shilling here is my config file: // mdsvex.config.js
/**
* Full MDsveX Options Documentation:
* https://mdsvex.com/docs#options
*/
import { lex, parse } from "fenceparser";
import { renderCodeToHTML, runTwoSlash, createShikiHighlighter } from "shiki-twoslash";
/** @type {Parameters<typeof import("mdsvex").mdsvex>[0]} */
export const config = {
extensions: [".svx"],
highlight: {
async highlighter(code, lang, meta) {
// Adapted from the `remark-shiki-twoslash` repo
// See: https://github.com/shikijs/twoslash/blob/fbf061261fcda90c46e946ce1e2e9357d465c145/packages/remark-shiki-twoslash/src/index.ts#L172-L215
let fence;
try {
fence = parse(lex([lang, meta].filter(Boolean).join(" ")));
} catch (error) {
throw new Error(`Could not parse the codefence for this code sample \n${code}`);
}
let twoslash;
if (fence.twoslash === true) {
twoslash = runTwoSlash(code, lang);
}
const highlighter = await createShikiHighlighter({ theme: "github-dark" });
const html = renderCodeToHTML(code, lang, fence, {}, highlighter, twoslash);
return `{@html \`${html}\` }`;
},
},
}; You will also need some additional CSS, you can find a base under point 3 here: https://github.com/shikijs/twoslash/tree/main/packages/remark-shiki-twoslash#plugin-setup |
@michaeloliverx Thanks for pointing me to twoslash, very cool project 🙏 |
I can't reproduce this. Is it still an issue? |
HTML Example
Python Example
---
UPDATE
As a workaround I got
shiki
working with the following snippet:It gives me what I want:
I don't know the stance MDsveX should take on code blocks.. having flexibility is king and I love being able to customise code output but I think there needs to be more information in the docs about this.
The text was updated successfully, but these errors were encountered: