metonym / svelte-highlight

Syntax Highlighting for Svelte using highlight.js
https://svhe.onrender.com
MIT License
262 stars 14 forks source link

Highlighting data returned from fetch #236

Closed 3goats closed 1 year ago

3goats commented 2 years ago

So I need to highlight some date retuned from a remote endpoint using fetch. Could you show and example for hi-lighting the JSON returned in this REPL example:

https://svelte.dev/repl/a74f1ed8e3eb4aec82cb743e13443ee4?version=3.48.0

metonym commented 2 years ago
<script>
  import Highlight from "svelte-highlight";
  import json from "svelte-highlight/languages/json";
  import github from "svelte-highlight/styles/github";

  let url = "https://www.swapi.tech/api/people/1";

  import fetchStore from "./fetch.js";
  const [data, loading, error, get] = fetchStore(url);
</script>

<button on:click={get}> Fetch again </button>

{#if $loading}
  Loading: {$loading}
{:else if $error}
  Error: {$error}
{:else}
  <Highlight language={json} code={JSON.stringify($data, null, 2)} />
{/if}

<svelte:head>
  {@html github}
</svelte:head>
3goats commented 2 years ago

Thanks very much. I thought I tried this, but must have been doing something stupid. If I want to highlight an editable div before sending it as the fetch POST body/data. Is there a best way to do this ? I'm still trying to learn Svelte, so some of the concepts are new to me.

metonym commented 2 years ago

If you're seeking a proper code editor, I would recommend CodeMirror, which is what I believe the Svelte REPL uses. I don't think this library serves as an appropriate text editor.

Another example is sveld, which uses both CodeMirror and Svelte Highlight. The editable content on the left is powered by CodeMirror while the static text on the right is highlighted by this library.

sveld UI

3goats commented 2 years ago

Thanks for the direction here. I took a look at this. I couldn't seem to find any examples for sveld. Also these might be an overkill for my use case at this stage. I will be repopulating the content for the POST body and only need to change a couple of the values, so really just looking for something super easy for now - Especially since I'm still trying to learn svelt.

Really just to hook a button up to the div content and have it sent to fetch.

3goats commented 2 years ago

Arh - I can see the screenshot is an example. I will take a look now.

metonym commented 2 years ago

Code editor: https://github.com/carbon-design-system/sveld/blob/main/playground/CodeEditor.svelte Code highlighter: https://github.com/carbon-design-system/sveld/blob/main/playground/CodeHighlighter.svelte

3goats commented 2 years ago

Excellent. Thanks again. Is there a way to easily hook these up to the input and output of fetch ? i.e. Have the content of the editor sent as a POST and then hi-light the response.

3goats commented 2 years ago

Is there a way to use/access svelte-highlight from within the Githubissues.

  • Githubissues is a development platform for aggregating issues.