Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add shiki support for .tsx and .jsx and add graceful fallback (text display) #335

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions clients/web/src/components/sources/source-pane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useParams, useSearchParams } from "@solidjs/router";
import { decodeFileName, guessContentType } from "~/lib/sources/file-entries";
import CodeView from "./code-view";
import { Loader } from "~/components/loader";
import { HighlighterLang } from "~/lib/code-highlight";
import type { SupportedLanguages } from "~/lib/code-highlight";
import { Heading } from "../heading";
import { useLocation } from "@solidjs/router";

Expand All @@ -15,7 +15,7 @@ export function SourcePane() {
const [searchParams] = useSearchParams();

const contentType = () => guessContentType(filePath());
const sizeHint = () => parseInt(searchParams.sizeHint);
const sizeHint = () => parseInt(searchParams.sizeHint ?? "0");

const location = useLocation();

Expand Down Expand Up @@ -46,7 +46,7 @@ export function SourcePane() {
resolvedContentType().replace(
"code/",
"",
) as HighlighterLang
) as SupportedLanguages
}
highlightedLine={highlightedLine()}
/>
Expand Down
28 changes: 21 additions & 7 deletions clients/web/src/lib/code-highlight.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,40 @@
import {
BundledTheme,
CodeToHastOptions,
Highlighter,
getHighlighter,
Highlighter,
} from "shiki";
import { transformerCompactLineOptions } from "@shikijs/transformers";
import { type BundledLanguage } from "shiki/langs";
import { SourcesClient } from "~/lib/proto/sources.client";
import { getEntryBytes } from "~/lib/sources/file-entries";

type HighlighterCodeParamsForSources = Readonly<
[string, Highlighter, BundledLanguage, number?]
[string, Highlighter, SupportedLanguages, number?]
>;

type HighlighterCodeParamsForSpans = Readonly<{
lang: BundledLanguage;
lang: SupportedLanguages;
highlighter: Highlighter;
}>;

export type AvailableLanguages = BundledLanguage | "text";

export const SUPPORTED_LANGS = [
"js",
"jsx",
"ts",
"tsx",
"typescript",
"javascript",
"rust",
"toml",
"html",
"json",
"md",
"yaml",
] satisfies BundledLanguage[];
"text",
] satisfies AvailableLanguages[];

export type SupportedLanguages = (typeof SUPPORTED_LANGS)[number];

Expand All @@ -39,12 +47,15 @@ export function bytesToText(bytes: Uint8Array | undefined): string {
export function textToHtml(
text: string,
highlighter: Highlighter | undefined,
lang: BundledLanguage,
lang: SupportedLanguages,
highlightedLine?: number,
) {
if (!highlighter) return "";

const options: CodeToHastOptions<BundledLanguage, BundledTheme> = {
if (!SUPPORTED_LANGS.find((supportedLang) => supportedLang === lang))
lang = "text";

const options: CodeToHastOptions<AvailableLanguages, BundledTheme> = {
lang,
theme: "material-theme-ocean",
};
Expand Down Expand Up @@ -92,7 +103,10 @@ export function getHighlightedCode(
if ("lang" in arg) {
const { lang } = arg;
return (code: string) =>
highlighter.codeToHtml(code, { lang, theme: "material-theme-ocean" });
highlighter.codeToHtml(code, {
lang,
theme: "material-theme-ocean",
});
}

const [text, highlighter, lang, highlightedLine] = arg;
Expand Down
11 changes: 6 additions & 5 deletions clients/web/src/lib/sources/file-entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,17 @@ export function guessContentType(path: string): string | undefined {

return {
rs: "code/rust",
txt: "code/txt",
toml: "code/toml",
lock: "code/toml",
js: "code/javascript",
jsx: "code/javascript",
ts: "code/typescript",
tsx: "code/typescript",
js: "code/js",
jsx: "code/jsx",
ts: "code/ts",
tsx: "code/tsx",
json: "code/json",
html: "code/html",
css: "code/css",
md: "code/markdown",
md: "code/md",
yml: "code/yaml",
yaml: "code/yaml",

Expand Down
Loading