Open michaeloliverx opened 3 years ago
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 escapeSvelte
function from the mdsvex
library itself simplifying things a bit:
// 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 shiki-twoslash
its really nice!
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:
<img width="655" alt="image" src="https://user-images.githubusercontent.com/55017335/136346839-ef756509-d178-43f6-8925-f10a8d09d71a.png">
You can also highlight lines with the following syntax:
````markdown
```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 twoslash
directive. You can even add custom properties for example a title or filename:
```ts {1-4} filename="some-file.ts"
type Post = {
title: string;
description: string;
};
...
They will be added to the `pre` HTML element as attributes where you could choose to display them using CSS:
<img width="454" alt="image" src="https://user-images.githubusercontent.com/55017335/136347971-5f64ccef-2e56-444e-9db2-f2c62b6f30e3.png">
Anyway enough shilling here is my config file:
```js
// 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 :pray:
I can't reproduce this. Is it still an issue?
HTML Example
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.