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

Highlight inline code such as \console.log("hello");{:js}\ #543

Open
Tracked by #588
jeremt opened this issue Sep 18, 2023 · 1 comment
Open
Tracked by #588

Highlight inline code such as \console.log("hello");{:js}\ #543

jeremt opened this issue Sep 18, 2023 · 1 comment
Labels
assigned Whether or not this bug has been assigned some to some other issues as a subtask or pre-req

Comments

@jeremt
Copy link

jeremt commented Sep 18, 2023

It would be nice to be able to apply highlighting to inline blocks. To do so, I tried two approaches, which only works for <pre> blocks:

image
  1. Using rehype-pretty-code plugin :
import adapter from '@sveltejs/adapter-auto';
import {mdsvex} from 'mdsvex';
import {vitePreprocess} from '@sveltejs/kit/vite';
import rehypePrettyCode from 'rehype-pretty-code';

/** @type {import('mdsvex').MdsvexOptions} */
const mdsvexOptions = {
    extensions: ['.mdx'],
    highlight: false,

    remarkPlugins: [],
    rehypePlugins: [
        [
            rehypePrettyCode,
            {
                keepBackground: false,
                theme: {
                    dark: 'dark-plus',
                    light: 'light-plus',
                },
            },
        ],
    ],
};

/** @type {import('@sveltejs/kit').Config} */
const config = {
    extensions: ['.svelte', '.mdx'],
    preprocess: [vitePreprocess(), mdsvex(mdsvexOptions)],

    kit: {
        adapter: adapter(),
    },
};

export default config;
  1. Using highlight option :
import shiki from 'shiki';
import adapter from '@sveltejs/adapter-auto';
import {mdsvex, escapeSvelte} from 'mdsvex';
import {vitePreprocess} from '@sveltejs/kit/vite';

/** @type {import('mdsvex').MdsvexOptions} */
const mdsvexOptions = {
    extensions: ['.mdx'],
    highlight: {
        highlighter: async (code, lang) => {
            console.log(code, lang);
            const highlighter = await shiki.getHighlighter({themes: ['dark-plus', 'light-plus']});
            const darkHtml = escapeSvelte(highlighter.codeToHtml(code, {lang}));
            const lightHtml = escapeSvelte(highlighter.codeToHtml(code, {lang, theme: 'light-plus'}));
            return `{@html \`${darkHtml}${lightHtml}\` }`;
        },
    },
};

/** @type {import('@sveltejs/kit').Config} */
const config = {
    extensions: ['.svelte', '.mdx'],
    preprocess: [vitePreprocess(), mdsvex(mdsvexOptions)],

    kit: {
        adapter: adapter(),
    },
};

export default config;

Is it impossible to do using MDsvex or am I missing something? 🤔

PS: it works nicely using parsers like unified or contentlayer with next:

unified()
    .use(remarkParse)
    .use(remarkRehype)
    .use(rehypePrettyCode, {
        keepBackground: false,
        theme: {
            dark: 'dark-plus',
            light: 'light-plus',
        },
    })
    .use(rehypeStringify);
@pngwn pngwn mentioned this issue Feb 23, 2024
13 tasks
@pngwn pngwn added the assigned Whether or not this bug has been assigned some to some other issues as a subtask or pre-req label Feb 24, 2024
@lubiah
Copy link

lubiah commented Mar 7, 2024

@jeremt I was able to do this with mdsvex and I used the same syntax as you did. It's possible

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
assigned Whether or not this bug has been assigned some to some other issues as a subtask or pre-req
Projects
None yet
Development

No branches or pull requests

3 participants