@@ -6,14 +6,19 @@ import ts from 'typescript';
6
6
import { SHIKI_LANGUAGE_MAP , escape , normalizeSlugify , smart_quotes , transform } from './utils' ;
7
7
import type { Declaration , TypeElement , Modules } from './index' ;
8
8
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
+
11
15
type TwoslashBanner = (
12
16
filename : string ,
13
17
content : string ,
14
18
language : string ,
15
19
options : SnippetOptions
16
20
) => string ;
21
+
17
22
interface RenderContentOptions {
18
23
twoslashBanner ?: TwoslashBanner ;
19
24
modules ?: Modules ;
@@ -159,7 +164,10 @@ export async function render_content_markdown(
159
164
let html = '<div class="code-block"><div class="controls">' ;
160
165
161
166
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>` ;
163
171
}
164
172
165
173
if ( converted ) {
@@ -945,19 +953,31 @@ function create_type_links(
945
953
function parse_options ( source : string ) {
946
954
METADATA_REGEX . lastIndex = 0 ;
947
955
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 = '' ;
949
959
950
- let copy_value = 'true' ;
951
960
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 } ` ) ;
954
975
}
955
- options [ key as MetadataKeys ] = value ;
976
+
956
977
return '' ;
957
978
} ) ;
958
979
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' ) ;
961
981
962
982
return { source, options } ;
963
983
}
0 commit comments