Skip to content

Commit f72e1dc

Browse files
committed
toggle file extensions
1 parent a9bd5e2 commit f72e1dc

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

packages/site-kit/src/lib/components/Text.svelte

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
width: 100%;
6666
z-index: 2;
6767
justify-content: end;
68+
box-sizing: border-box;
6869
6970
&:has(.filename) {
7071
position: relative;
@@ -80,6 +81,16 @@
8081
font-weight: 400;
8182
padding: 0 1rem;
8283
color: var(--sk-text-2);
84+
85+
&::after {
86+
content: attr(data-ext);
87+
}
88+
}
89+
90+
&:has(.ts-toggle:checked) {
91+
.filename[data-ext='.js']::after {
92+
content: '.ts';
93+
}
8394
}
8495
8596
.ts-toggle {

packages/site-kit/src/lib/markdown/renderer.ts

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,19 @@ import ts from 'typescript';
66
import { SHIKI_LANGUAGE_MAP, escape, normalizeSlugify, smart_quotes, transform } from './utils';
77
import type { Declaration, TypeElement, Modules } from './index';
88

9-
type MetadataKeys = 'file' | 'link' | 'copy';
10-
type SnippetOptions = Record<MetadataKeys, string | boolean | number | null>;
9+
interface SnippetOptions {
10+
file: string | null;
11+
link: boolean;
12+
copy: boolean;
13+
}
14+
1115
type TwoslashBanner = (
1216
filename: string,
1317
content: string,
1418
language: string,
1519
options: SnippetOptions
1620
) => string;
21+
1722
interface RenderContentOptions {
1823
twoslashBanner?: TwoslashBanner;
1924
modules?: Modules;
@@ -159,7 +164,10 @@ export async function render_content_markdown(
159164
let html = '<div class="code-block"><div class="controls">';
160165

161166
if (options.file) {
162-
html += `<span class="filename">${options.file}</span>`;
167+
const ext = options.file.slice(options.file.lastIndexOf('.'));
168+
if (!ext) throw new Error(`Missing file extension: ${options.file}`);
169+
170+
html += `<span class="filename" data-ext="${ext}">${options.file.slice(0, -ext.length)}</span>`;
163171
}
164172

165173
if (converted) {
@@ -945,19 +953,31 @@ function create_type_links(
945953
function parse_options(source: string) {
946954
METADATA_REGEX.lastIndex = 0;
947955

948-
const options: SnippetOptions = { file: null, link: null, copy: true };
956+
const options: SnippetOptions = { file: null, link: false, copy: true };
957+
958+
let copy_value = '';
949959

950-
let copy_value = 'true';
951960
source = source.replace(METADATA_REGEX, (_, key, value) => {
952-
if (key === 'copy') {
953-
copy_value = value;
961+
switch (key) {
962+
case 'file':
963+
options.file = value;
964+
break;
965+
966+
case 'link':
967+
options.link = value === 'true';
968+
969+
case 'copy':
970+
copy_value = value;
971+
break;
972+
973+
default:
974+
throw new Error(`Unrecognised option ${key}`);
954975
}
955-
options[key as MetadataKeys] = value;
976+
956977
return '';
957978
});
958979

959-
options.link = options.link === 'true';
960-
options.copy = copy_value === 'true' || (options.file && copy_value !== 'false');
980+
options.copy = copy_value === 'true' || (options.file !== null && copy_value !== 'false');
961981

962982
return { source, options };
963983
}

0 commit comments

Comments
 (0)