code2svg
コードスニペットを画像にするServerless Function
https://github.com/takker99/code2svg
https://code2svg.vercel.app/
API設計
⬜/
return swagger.json
未実装
https://code2svg.vercel.app/svg/:options?/:url
convert a source code got from url to a svg image
:urlにdata urlを入れられたら面白いかも
:optionsはSkypackのinternal apiやesb.deno.devと同じ書式をとる
table:options
name posibble values description
L(\d+)-(\d+) N/A render particular lines in the range
L(\d+) N/A render a particular line
light github (default) specify the light theme
dark github-dark (default) specify the dark theme
lang (auto detect when not specified) specify the language
nowrap true or false (default) whether wrap lines
width number the width of the image
height number the width of the image
blanks number 追加の空行
いずれ実装するもの
行番号
headerの付与
ファイル名やソースへのリンクを表示する
browser URLの自動変換?
github.comをraw.githubusercontent.comに自動変換するなど
変換を無効にするoptionnoconvertも実装する
HTMLファイルそのものを参照したい場合に必要
https://code2svg.vercel.app/list/languages
list languages available
application/json
https://code2svg.vercel.app/list/themes
list themes available
application/json
highlight.jsのテーマを全て使えるようにしておく
使い方は/villagepump/code2svgが詳しい
実装したいこと
swagger.jsonを作る
対応する(dark|light) themeを自動で探して適用する
code:ts
const detectCorrespondingTheme = (theme: ThemeName): ThemeName => {
if (theme.includes("-light")) {
const dark = theme.replace("-light", "-dark");
if (isTheme(dark)) return dark;
const normal = theme.replace("-light", "");
if (isTheme(normal)) return normal;
}
if (theme.includes("-dark")) {
const light = theme.replace("-dark", "-light");
if (isTheme(light)) return light;
const normal = theme.replace("-dark", "");
if (isTheme(normal)) return normal;
}
return theme;
};
const light, dark = lightTheme && !darkTheme
? lightTheme, detectCorrespondingTheme(lightTheme)
: !lightTheme && darkTheme
? detectCorrespondingTheme(darkTheme), lightTheme
: lightTheme && darkTheme
? lightTheme, darkTheme
: "github", "github-dark";
code2svg#63efb28c1280f000001afe7e
特にheaderと行番号は作りたい
raw.githubusercontents.comからgithub.comのリンクを取り出したい
行番号があると、範囲調節がしやすい
⬜行番号を入れる (code2svg)
/mrsekut-p/rem (単位)
defaultはrmではなくremがいい?
バグ
⬜theme CSSをescapeする
CSSのコメントでhtmlタグっぽいものを使うとsvgがエラーを出す
panda-syntax-lightで発生
全部hastで作ってtoHtmlをかければ解決するかな?
https://github.com/takker99/code2svg/pull/4
なぜかdeno.lockがhast-util-to-htmlでエラーを吐いてしまっていた
作り直してpushしたらなおった
✅code2svgでGitHub Actionsが動かない
⬜全角文字を半角文字と同じ文字幅として計算してしまっている
全角文字の判定をする処理を入れて、全角文字を2文字と数えてれば直るだろう
井戸端が初出
2023-02-25
18:01:28 おんなじ名前のプログラムがすでにあった!?
https://github.com/fuinorg/code2svg
Javaで書かれている
2023-02-18
19:00:55 production deployed
完成!
18:57:53 ciを設定した
18:49:14 vercel側のチェック終了
18:45:51 なぜかvercelだと/svg/https://~が/svg/https:/~にredirectされてしまう
routingでhttps?:/?にも対応できるようにした
18:44:49 deno taskを作った
glob patternにまだ対応していないようだったので、必要なファイルだけcheckするようにした
型定義の調整に手こずった
deno check --remoteは断念した
17:52:52 widthとheightを実装した
17:39:46 だいたい実装できた
まだ実装していないもの
✅nowrap
画像の大きさは外部から指定できた方がいい
15:26:39 repoを掘って動作確認中
なぜかvercel devがImporting "vercel-deno": Your application tried to access fs-extra, but it isn't declared in your dependencies; this makes the require call ambiguous and unsound.を吐いて機能しないので、serverにdeployして試している
vercel.jsonの設定は https://zenn.dev/kawarimidoll/articles/2659e69719b075#vercelの設定ファイルの追加 を参考に設定した
oak.Application.handle()の使い方
https://medium.com/deno-the-complete-reference/use-oak-as-request-handler-in-deno-e206e07c24e1
実装
oakでroutingする
lowlightでhighlightする
https://github.com/denoland/docland/blob/main/main.ts が参考になる
#code2svg.vercel.app
#2024-01-13 15:57:17
#2023-02-25 17:18:56
#2023-02-23 07:39:14
#2023-02-19 09:40:26
#2023-02-18 01:00:19
#2023-02-17 21:52:09
#2023-02-03 06:16:38