-
Notifications
You must be signed in to change notification settings - Fork 51
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
Feature Request: HighlightJs #60
Comments
You can just change the Code Renderer to render code blocks as you want. For example I use the CodeSnippet component from Svelte Carbon Components to do this with prismjs.
<script>
import { CodeSnippet } from "carbon-components-svelte";
import * as prism from "prismjs";
import 'prismjs/components/prism-json';
import 'prismjs/components/prism-bash';
import 'prismjs/components/prism-css';
import 'prismjs/components/prism-csv';
import 'prismjs/components/prism-git';
import 'prismjs/components/prism-javascript';
import 'prismjs/components/prism-jsx';
import 'prismjs/components/prism-markdown';
import 'prismjs/components/prism-powershell';
import 'prismjs/components/prism-scss';
export let lang;
export let text;
let formatted = "";
if (prism.languages[lang]) {
formatted = prism.highlight(text, prism.languages[lang], lang);
}
</script>
{#if formatted == ""}
<CodeSnippet code={text} type="multi">{text}</CodeSnippet>
{:else}
<CodeSnippet code={text} type="multi">{@html formatted}</CodeSnippet>
{/if}
<script>
import CodeRenderer from "$lib/components/CodeRenderer.svelte"
import SvelteMarkdown from "svelte-markdown"
</script>
<SvelteMarkdown {source} renderers={{code: CodeRenderer}} /> |
Finally I found it honestly we need better documentation for block level renderer: https://marked.js.org/using_pro#renderer for example May be I miss it, where I can found complete documentation about it? |
Got it working with markdown.svelte <script lang="ts">
import SvelteMarkdown from 'svelte-markdown';
import CodeMarkdown from './code-markdown.svelte';
import 'highlight.js/styles/github-dark.css';
export let source: string;
</script>
<SvelteMarkdown renderers={{ code: CodeMarkdown }} {source} /> code-markdown.svelte <script lang="ts">
import hljs from 'highlight.js';
export let text = '';
export let lang = '';
hljs.highlight(text, { language: lang || 'plaintext' }).value;
</script>
<pre class={`language-` + lang}>
<code class="hljs">{@html highlightedText}</code>
</pre> J |
A little correction |
This would be nice to have HighlightJS working with a copy code button!
J
The text was updated successfully, but these errors were encountered: