npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@novacbn/svelte-codejar

v0.1.2

Published

Svelte Binding for the embeddable code editor CodeJar

Downloads

180

Readme

svelte-codejar

Description

Svelte Binding for the embeddable code editor CodeJar

Demo

See a demo at novacbn.github.io/svelte-codejar/demo

Sample

<script>
    import {CodeJar} from "@novacbn/svelte-codejar";

    let value = `console.log("Hello World!");`;
</script>

<!--
    `CodeJar` options with their defaults
    See more information at: https://github.com/antonmedv/codejar#getting-started
-->

<!--
    **NOTE**: Syntax highlighting is optional and must be provided by you. See the
    sample below on how to use it

    **NOTE2**: When setting `CodeJar.tab`, if you're using escape characters such
    as `\t`, you need encapsulate it as a raw string (see below). Otherwise the Svelte
    compiler will escape the value when it parses your code
-->

<CodeJar addClosing={true} indentOn={/{$/} spellcheck={false} tab={"\t"} bind:value />

Syntax Highlighting

highlight.js

NOTE: The sample below uses highlight.js, see the link for more information.

<script context="module">
    // We need to configure highlight.js for Javascript, and then alias the
    // exports to match the function signatures that `CodeJar` Component expects
    import hljs from "highlight.js/lib/core";
    import javascript from "highlight.js/lib/languages/javascript";

    hljs.registerLanguage("javascript", javascript);

    // `highlight` takes the input code and returns the highlighted HTML markup
    const highlight = (code, syntax) =>
        hljs.highlight(code, {
            language: syntax,
        }).value;
</script>

<script>
    import {CodeJar} from "@novacbn/svelte-codejar";

    let value = `console.log("Hello World!");`;
</script>

<!--
    Now we pass `CodeJar` our syntax highlighting functions along with the
    language syntax used for highlighting

    We also need to pass the `hljs` class so highlight.js knows which element
    to style
-->

<CodeJar class="hljs" syntax="javascript" {highlight} {value} />

PrismJS

NOTE: The code is the same as above, but with PrismJS calls instead of highlight.js

<script context="module">
    import Prism from "prismjs";

    const highlight = (code, syntax) => Prism.highlight(code, Prism.languages[syntax], syntax);
</script>

<script>
    import {CodeJar} from "@novacbn/svelte-codejar";

    let value = `console.log("Hello World!");`;
</script>

<CodeJar syntax="javascript" {highlight} {value} />

FAQ

SvelteKit — ReferenceError: window is not defined

When using the library with SvelteKit with SSR (serverside rendering) enabled you might get this error:

[vite] Error when evaluating SSR module /node_modules/codejar/codejar.js?v=4f67a3d5:
ReferenceError: window is not defined

Nothing much can do about that, CodeJar makes a window assignment in its module scope. However you can do a workaround via onMount or other similar workflows:

<script>
    import {onMount} from "@novacbn/svelte-codejar";

    export let value = "";

    // **NOTE:** Since `onMount` is only called on the client, we can just
    // make our import there. And assign to our Component's scope
    let CodeJar;
    onMount(async () => {
        ({CodeJar} = await import("@novacbn/svelte-codejar"));
    });
</script>

{#if CodeJar}
<CodeJar bind:value />
{:else}
<!--
    **NOTE:** Normally the `CodeJar` Svelte handles fall through for us, and
    renders / syntax highlights without an editor during SSR / non-JS enabled clients
-->
<pre><code>{value}</code></pre>
{/if}

Only downside being you have to manually syntax highlight your code in the {:else} block for SSR / non-JS enabled clients.

Developer

Installation

Open your terminal and install via npm:

npm install @novacbn/svelte-codejar

Properties

| Name | Typing | Default | Description | | ---------- | ------------------------------------------- | ----------- | -------------------------------------------------------------------------------------------------------------------- | | addClosing | boolean | true | Sets whether the Editor automatically adds closing delimiters, like brackets, quotes, etc... | | indentOn | RegExp | /{$/ | Represents what expression is used to detect when the Editor needs to auto indent with the configured tab characters | | spellcheck | boolean | false | Sets whether to enable the Browser's spellcheck or not | | tab | string | \t | Sets the characters inserted whenever the end-user pressed the tab key | | highlight | (code: string, syntax?: string) => string | null | Callback is called to highlight the current code and return the rendered HTML markup | | syntax | string | undefined | Sets the current language mode of the Editor | | value | string | "" | Sets the current text of the Editor | | class | string | "" | Applies class="" to the <pre> container element | | style | string | undefined | Applies style="" to the <pre> container element |

Events

| Name | Typing | Description | | -------- | ------------------------------ | --------------------------------------------- | | change | CustomEvent<{value: string}> | Fires whenever the end-user changes the input |

API

  • Components

    • CodeJar