GithubHelp home page GithubHelp logo

SVG renderer about shiki HOT 5 CLOSED

shikijs avatar shikijs commented on July 18, 2024
SVG renderer

from shiki.

Comments (5)

iamtekeste avatar iamtekeste commented on July 18, 2024

I would love to see this happen.
What can I do to make this a reality?

from shiki.

canibanoglu avatar canibanoglu commented on July 18, 2024

So I played a bit with this recently and I'm afraid it doesn't look straightforward or maybe I'm missing something very obvious.

One of the first problems with SVG is that it really isn't similar HTML in how it works. Working with text content is definitely harder, for example you have to manually position every text element etc.

First obvious attempt was to use dom-to-image by passing the HTML output from shiki to the .toSvg metho but that didn't go well at all:

  • Line breaks don't work and there are some strange characters appearing sometimes (See the image below)
  • The generated svg is HUGE. This is happening because dom-to-image inlines all computed styles to every single span.

Screenshot 2020-01-15 at 17 40 55

I think this can still be done though as long as we don't care about text wrapping as it has to be done manually for SVG. I will update this as I start working on it later.

from shiki.

canibanoglu avatar canibanoglu commented on July 18, 2024

I covered some more ground and I have something like the following. I'm not confident in my SVG-foo though and I don't know if this would be the best way to generate an SVG filled with text. I would appreciate it if someone could take a look.

@octref I can prepare a PR if you're OK with it?

Generated SVG
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%">
    <style>
        .s-c-1 {
            fill: #FFFFFF;
        }
        
        .s-c-2 {
            fill: #292D3E;
        }
        
        .s-c-3 {
            fill: #676E95;
        }
        
        .s-c-4 {
            fill: #F78C6C;
        }
        
        .s-c-5 {
            fill: #89DDFF;
        }
        
        .s-c-6 {
            fill: #C3E88D;
        }
        
        .s-c-7 {
            fill: #FFCB6B;
        }
        
        .s-c-8 {
            fill: #A6ACCD;
        }
        
        .s-c-9 {
            fill: #82AAFF;
        }
        
        .s-c-10 {
            fill: #FF5370;
        }
        
        .s-c-11 {
            fill: #F07178;
        }
        
        .s-c-12 {
            fill: #C792EA;
        }
        
        .s-c-13 {
            fill: #A6ACCD90;
        }
        
        .s-c-14 {
            fill: #4E5579;
        }
        
        .s-c-15 {
            fill: #B2CCD6;
        }
        
        .s-c-16 {
            fill: #C17E70;
        }
    </style>
    <rect width="100%" height="100%" fill="#292D3E" />
    <text x="10" y="16">
        <tspan>
            <tspan class="s-c-5">import</tspan>
            <tspan class="s-c-5">{</tspan>
            <tspan class="s-c-8">renderToHtml</tspan>
            <tspan class="s-c-5">}</tspan>
            <tspan class="s-c-5">from</tspan>
            <tspan class="s-c-5">'</tspan>
            <tspan class="s-c-6">./renderer</tspan>
            <tspan class="s-c-5">'</tspan>
        </tspan>
    </text>
    <text x="10" y="48">
        <tspan>
            <tspan class="s-c-5">import</tspan>
            <tspan class="s-c-5">{</tspan>
            <tspan class="s-c-8">getTheme</tspan>
            <tspan class="s-c-5">,</tspan>
            <tspan class="s-c-8">TTheme</tspan>
            <tspan class="s-c-5">,</tspan>
            <tspan class="s-c-8">IShikiTheme</tspan>
            <tspan class="s-c-5">}</tspan>
            <tspan class="s-c-5">from</tspan>
            <tspan class="s-c-5">'</tspan>
            <tspan class="s-c-6">shiki-themes</tspan>
            <tspan class="s-c-5">'</tspan>
        </tspan>
    </text>
    <text x="10" y="80">
        <tspan>
            <tspan class="s-c-5">export</tspan>
            <tspan class="s-c-12">interface</tspan>
            <tspan class="s-c-7">HighlighterOptions</tspan>
            <tspan class="s-c-5">{</tspan>
        </tspan>
    </text>
    <text x="20" y="96">
        <tspan>
            <tspan class="s-c-8">theme</tspan>
            <tspan class="s-c-12">:</tspan>
            <tspan class="s-c-7">TTheme</tspan>
            <tspan class="s-c-12">|</tspan>
            <tspan class="s-c-7">IShikiTheme</tspan>
        </tspan>
    </text>
    <text x="20" y="112">
        <tspan>
            <tspan class="s-c-8">langs</tspan>
            <tspan class="s-c-12">?:</tspan>
            <tspan class="s-c-7">TLang</tspan>
            <tspan class="s-c-1">[]</tspan>
        </tspan>
    </text>
    <text x="10" y="128">
        <tspan>
            <tspan class="s-c-5">}</tspan>
        </tspan>
    </text>
    <text x="10" y="160">
        <tspan>
            <tspan class="s-c-5">export</tspan>
            <tspan class="s-c-12">async</tspan>
            <tspan class="s-c-12">function</tspan>
            <tspan class="s-c-9">getHighlighter</tspan>
            <tspan class="s-c-5">(</tspan>
            <tspan class="s-c-10">options</tspan>
            <tspan class="s-c-12">:</tspan>
            <tspan class="s-c-7">HighlighterOptions</tspan>
            <tspan class="s-c-5">)</tspan>
            <tspan class="s-c-5">{</tspan>
        </tspan>
    </text>
    <text x="20" y="176">
        <tspan>
            <tspan class="s-c-12">let</tspan>
            <tspan class="s-c-8">t</tspan>
            <tspan class="s-c-12">:</tspan>
            <tspan class="s-c-7">IShikiTheme</tspan>
        </tspan>
    </text>
    <text x="20" y="192">
        <tspan>
            <tspan class="s-c-5">if</tspan>
            <tspan class="s-c-1"> (</tspan>
            <tspan class="s-c-12">typeof</tspan>
            <tspan class="s-c-8">options</tspan>
            <tspan class="s-c-5">.</tspan>
            <tspan class="s-c-8">theme</tspan>
            <tspan class="s-c-12">===</tspan>
            <tspan class="s-c-5">'</tspan>
            <tspan class="s-c-6">string</tspan>
            <tspan class="s-c-5">'</tspan>
            <tspan class="s-c-1">) </tspan>
            <tspan class="s-c-5">{</tspan>
        </tspan>
    </text>
    <text x="30" y="208">
        <tspan>
            <tspan class="s-c-8">t</tspan>
            <tspan class="s-c-12">=</tspan>
            <tspan class="s-c-9">getTheme</tspan>
            <tspan class="s-c-1">(</tspan>
            <tspan class="s-c-8">options</tspan>
            <tspan class="s-c-5">.</tspan>
            <tspan class="s-c-8">theme</tspan>
            <tspan class="s-c-1">)</tspan>
        </tspan>
    </text>
    <text x="20" y="224">
        <tspan>
            <tspan class="s-c-5">}</tspan>
            <tspan class="s-c-5">else</tspan>
            <tspan class="s-c-5">if</tspan>
            <tspan class="s-c-1"> (</tspan>
            <tspan class="s-c-8">options</tspan>
            <tspan class="s-c-5">.</tspan>
            <tspan class="s-c-8">theme</tspan>
            <tspan class="s-c-5">.</tspan>
            <tspan class="s-c-1">name) </tspan>
            <tspan class="s-c-5">{</tspan>
        </tspan>
    </text>
    <text x="30" y="240">
        <tspan>
            <tspan class="s-c-8">t</tspan>
            <tspan class="s-c-12">=</tspan>
            <tspan class="s-c-8">options</tspan>
            <tspan class="s-c-5">.</tspan>
            <tspan class="s-c-8">theme</tspan>
        </tspan>
    </text>
    <text x="20" y="256">
        <tspan>
            <tspan class="s-c-5">}</tspan>
            <tspan class="s-c-5">else</tspan>
            <tspan class="s-c-5">{</tspan>
        </tspan>
    </text>
    <text x="30" y="272">
        <tspan>
            <tspan class="s-c-8">t</tspan>
            <tspan class="s-c-12">=</tspan>
            <tspan class="s-c-9">getTheme</tspan>
            <tspan class="s-c-1">(</tspan>
            <tspan class="s-c-5">'</tspan>
            <tspan class="s-c-6">nord</tspan>
            <tspan class="s-c-5">'</tspan>
            <tspan class="s-c-1">)</tspan>
        </tspan>
    </text>
    <text x="20" y="288">
        <tspan>
            <tspan class="s-c-5">}</tspan>
        </tspan>
    </text>
    <text x="20" y="320">
        <tspan>
            <tspan class="s-c-12">let</tspan>
            <tspan class="s-c-8">langs</tspan>
            <tspan class="s-c-12">:</tspan>
            <tspan class="s-c-7">TLang</tspan>
            <tspan class="s-c-1">[] </tspan>
            <tspan class="s-c-12">=</tspan>
            <tspan class="s-c-1"> [</tspan>
            <tspan class="s-c-12">...</tspan>
            <tspan class="s-c-8">commonLangIds</tspan>
            <tspan class="s-c-5">,</tspan>
            <tspan class="s-c-12">...</tspan>
            <tspan class="s-c-8">commonLangAliases</tspan>
            <tspan class="s-c-1">]</tspan>
        </tspan>
    </text>
    <text x="20" y="352">
        <tspan>
            <tspan class="s-c-5">if</tspan>
            <tspan class="s-c-1"> (</tspan>
            <tspan class="s-c-8">options</tspan>
            <tspan class="s-c-5">.</tspan>
            <tspan class="s-c-8">langs</tspan>
            <tspan class="s-c-1">) </tspan>
            <tspan class="s-c-5">{</tspan>
        </tspan>
    </text>
    <text x="30" y="368">
        <tspan>
            <tspan class="s-c-8">langs</tspan>
            <tspan class="s-c-12">=</tspan>
            <tspan class="s-c-8">options</tspan>
            <tspan class="s-c-5">.</tspan>
            <tspan class="s-c-8">langs</tspan>
        </tspan>
    </text>
    <text x="20" y="384">
        <tspan>
            <tspan class="s-c-5">}</tspan>
        </tspan>
    </text>
    <text x="20" y="416">
        <tspan>
            <tspan class="s-c-12">const</tspan>
            <tspan class="s-c-8">langRegistrations</tspan>
            <tspan class="s-c-12">=</tspan>
            <tspan class="s-c-9">getLangRegistrations</tspan>
            <tspan class="s-c-1">(</tspan>
            <tspan class="s-c-8">langs</tspan>
            <tspan class="s-c-1">)</tspan>
        </tspan>
    </text>
    <text x="20" y="448">
        <tspan>
            <tspan class="s-c-12">const</tspan>
            <tspan class="s-c-8">s</tspan>
            <tspan class="s-c-12">=</tspan>
            <tspan class="s-c-12">new</tspan>
            <tspan class="s-c-7">Shiki</tspan>
            <tspan class="s-c-1">(</tspan>
            <tspan class="s-c-8">t</tspan>
            <tspan class="s-c-5">,</tspan>
            <tspan class="s-c-8">langRegistrations</tspan>
            <tspan class="s-c-1">)</tspan>
        </tspan>
    </text>
    <text x="20" y="464">
        <tspan>
            <tspan class="s-c-5">return</tspan>
            <tspan class="s-c-5">await</tspan>
            <tspan class="s-c-8">s</tspan>
            <tspan class="s-c-5">.</tspan>
            <tspan class="s-c-9">getHighlighter</tspan>
            <tspan class="s-c-1">()</tspan>
        </tspan>
    </text>
    <text x="10" y="480">
        <tspan>
            <tspan class="s-c-5">}</tspan>
        </tspan>
    </text>
</svg>

from shiki.

canibanoglu avatar canibanoglu commented on July 18, 2024

Aaand I just realized that there is a svg branch already on this repo right here...

from shiki.

octref avatar octref commented on July 18, 2024

@canibanoglu You run into the same issue I have: There's no one SVG that would work consistently between design tools such as Figma/AI/Sketch.
I'd be interested in looking this myself as well. If you already find a good solution, maybe send that as a WIP PR and we can take a look together.

from shiki.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.