fileUtils.ts

1import { marked } from 'marked'
2
3export function getFileExtension(filename: string): string {
4	return filename.slice(((filename.lastIndexOf('.') - 1) >>> 0) + 2)
5}
6
7export function getLanguageFromExtension(extension: string): string {
8	const languageMap: { [key: string]: string } = {
9		// JavaScript and related
10		js: 'javascript',
11		mjs: 'javascript',
12		cjs: 'javascript',
13		jsx: 'jsx',
14
15		// TypeScript
16		ts: 'typescript',
17		tsx: 'typescript',
18
19		// Python
20		py: 'python',
21		pyw: 'python',
22
23		// Ruby
24		rb: 'ruby',
25
26		// Java
27		java: 'java',
28		class: 'java',
29
30		// Kotlin
31		kt: 'kotlin',
32		kts: 'kotlin',
33
34		// C/C++ and headers
35		c: 'c',
36		h: 'cpp',
37		cc: 'cpp',
38		cpp: 'cpp',
39		cxx: 'cpp',
40		hpp: 'cpp',
41		hh: 'cpp',
42
43		// C#
44		cs: 'csharp',
45
46		// F#
47		fs: 'fsharp',
48		fsi: 'fsharp',
49
50		// Go
51		go: 'go',
52
53		// Rust
54		rs: 'rust',
55
56		// PHP
57		php: 'php',
58		phtml: 'php',
59
60		// Web technologies
61		html: 'html',
62		htm: 'html',
63		xhtml: 'html',
64		haml: 'haml',
65		css: 'css',
66		scss: 'scss',
67		sass: 'scss',
68		less: 'less',
69
70		// Markdown and related
71		md: 'markdown',
72		markdown: 'markdown',
73		mdx: 'markdown',
74
75		// JSON and YAML
76		json: 'json',
77		yaml: 'yaml',
78		yml: 'yaml',
79
80		// XML and derivatives
81		xml: 'xml',
82		svg: 'xml',
83
84		// Shell and scripts
85		sh: 'bash',
86		bash: 'bash',
87		zsh: 'bash',
88		ksh: 'bash',
89		bat: 'batch',
90		cmd: 'batch',
91
92		// SQL and database files
93		sql: 'sql',
94		db: 'sql',
95
96		// R and related
97		r: 'r',
98		rmd: 'r',
99
100		// Powershell
101		ps1: 'powershell',
102
103		// Visual Basic
104		vb: 'vbnet',
105		vbs: 'vbnet',
106
107		// Lua
108		lua: 'lua',
109
110		// Perl
111		pl: 'perl',
112		pm: 'perl',
113
114		// CoffeeScript
115		coffee: 'coffeescript',
116
117		// Scala
118		scala: 'scala',
119
120		// Swift
121		swift: 'swift',
122
123		// Configuration files
124		env: 'dotenv',
125		toml: 'toml',
126		ini: 'ini',
127		cfg: 'ini',
128		properties: 'ini',
129
130		// Vim
131		vim: 'vim',
132
133		// Haskell
134		hs: 'haskell',
135
136		// Erlang
137		erl: 'erlang',
138		hrl: 'erlang',
139
140		// Elixir
141		ex: 'elixir',
142		exs: 'elixir',
143
144		// Zig
145		zig: 'zig',
146
147		// Docker and related
148		dockerfile: 'dockerfile',
149		dockerignore: 'dockerfile',
150
151		// Makefiles and build tools
152		makefile: 'makefile',
153		mk: 'makefile',
154		gnumakefile: 'makefile',
155
156		// Nginx and Apache
157		nginx: 'nginx',
158		conf: 'nginx',
159
160		// Text files
161		txt: 'text',
162		log: 'text',
163
164		// Miscellaneous
165		rst: 'restructuredtext',
166		asciidoc: 'asciidoc',
167		adoc: 'asciidoc',
168		// Add more mappings as needed
169	}
170
171	return languageMap[extension.toLowerCase()] || 'text'
172}
173
174export function processMarkdownLinks(
175	markdownContent: string,
176	projectName: string,
177	branch: string,
178): string {
179	// Process relative links only
180	const processedContent = markdownContent.replace(
181		/(\[.*?\])\((\.?\/?)((?!\/\/|https?:\/\/|mailto:|tel:).*?)\)/g,
182		(_, linkText, slashes, path) => {
183			const cleanPath = path.replace(/^\.?\//, '')
184			return `${linkText}(/projects/${projectName}/${branch}/~/${cleanPath})`
185		},
186	)
187
188	// Use marked.parse() for synchronous rendering
189	return marked.parse(processedContent, { async: false }) as string
190}
191