Comments (5)
I would love to see this happen.
What can I do to make this a reality?
from shiki.
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.
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.
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.
Aaand I just realized that there is a svg
branch already on this repo right here...
from shiki.
@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)
- `@shikijs/vitepress-twoslash`: Use twoslash in the case of using imported code snippets.
- Row of `language-*` on a single `code` element HOT 1
- Vue 当没有写 `template` 时无法正常高亮 HOT 1
- @shikijs/markdown-it: code block is wrong when entering a non-existent language HOT 2
- Twoslash node query blocks (^?) no longer display on separate lines since 0.x
- `remove-notation-escape`: notation escape syntax’s normalize transformer HOT 2
- Rehype: default language
- "TypeError: onigBinding.UTF8ToString is not a function" after some time / parallel requests in Next.js HOT 10
- Inconsistent behavior between `@shikijs/twoslash` and `@shikijs/vitepress-twoslash`
- Diff syntax highlighting doesn't work with css variables theme
- `light-dark()` CSS function for dual themes HOT 3
- how to use codeToHtml + transformer (meta) ? HOT 5
- Detect Notation Transformers dynamically
- Properly disposing of highlighters HOT 1
- Describe the bug here HOT 2
- `@shikijs/rehype`: support `langAlias` options
- Optimization suggestions for the 'hast' type in Shiki.js HOT 2
- `@shikijs/rehype` - "30 instances have been created" HOT 6
- Return the grammar state after tokenizing HOT 2
- shiki/wasm dependency not found HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from shiki.