Skip to content

Commit

Permalink
feat: Even more language support (#3138)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholas-codecov authored Aug 26, 2024
1 parent 93030af commit 6032eb0
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
30 changes: 29 additions & 1 deletion src/shared/utils/prismLanguageMapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,58 @@ describe('prismLanguageMapper', () => {
{ extension: 'rss', expected: 'markup' },
{ extension: 'sh', expected: 'bash' },
{ extension: 'c', expected: 'c' },
{ extension: 'h', expected: 'clike' },
{ extension: 'h', expected: 'c' },
{ extension: 'cc', expected: 'cpp' },
{ extension: 'cpp', expected: 'cpp' },
{ extension: 'hpp', expected: 'cpp' },
{ extension: 'cs', expected: 'csharp' },
{ extension: 'cshtml', expected: 'cshtml' },
{ extension: 'css', expected: 'css' },
{ extension: 'dart', expected: 'dart' },
{ extension: 'f', expected: 'fortran' },
{ extension: 'fpp', expected: 'fortran' },
{ extension: 'f90', expected: 'fortran' },
{ extension: 'fs', expected: 'fsharp' },
{ extension: 'java', expected: 'java' },
{ extension: 'jl', expected: 'julia' },
{ extension: 'js', expected: 'javascript' },
{ extension: 'cjs', expected: 'javascript' },
{ extension: 'mjs', expected: 'javascript' },
{ extension: 'jsx', expected: 'jsx' },
{ extension: 'kt', expected: 'kotlin' },
{ extension: 'kts', expected: 'kotlin' },
{ extension: 'go', expected: 'go' },
{ extension: 'gql', expected: 'graphql' },
{ extension: 'graphql', expected: 'graphql' },
{ extension: 'json', expected: 'json' },
{ extension: 'less', expected: 'less' },
{ extension: 'objc', expected: 'objectivec' },
{ extension: 'ocaml', expected: 'ocaml' },
{ extension: 'php', expected: 'php' },
{ extension: 'py', expected: 'python' },
{ extension: 'r', expected: 'r' },
{ extension: 'ps1', expected: 'powershell' },
{ extension: 'psm1', expected: 'powershell' },
{ extension: 'razor', expected: 'cshtml' },
{ extension: 'rb', expected: 'ruby' },
{ extension: 'reason', expected: 'reason' },
{ extension: 'rs', expected: 'rust' },
{ extension: 'sass', expected: 'sass' },
{ extension: 'scala', expected: 'scala' },
{ extension: 'scss', expected: 'scss' },
{ extension: 'sol', expected: 'solidity' },
{ extension: 'sql', expected: 'sql' },
{ extension: 'swift', expected: 'swift' },
{ extension: 'svelte', expected: 'markup' },
{ extension: 'ts', expected: 'typescript' },
{ extension: 'tsx', expected: 'tsx' },
{ extension: 'vb', expected: 'visual-basic' },
{ extension: 'vba', expected: 'visual-basic' },
{ extension: 'vbs', expected: 'visual-basic' },
{ extension: 'vue', expected: 'markup' },
{ extension: 'wasm', expected: 'wasm' },
{ extension: 'yaml', expected: 'yaml' },
{ extension: 'zig', expected: 'zig' },
]

it.each(testCases)(
Expand Down
37 changes: 34 additions & 3 deletions src/shared/utils/prismLanguageMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import Prism from 'prism-react-renderer/prism'
*/
require('prismjs/components/prism-csharp')
require('prismjs/components/prism-dart')
require('prismjs/components/prism-cshtml')
require('prismjs/components/prism-fortran')
require('prismjs/components/prism-fsharp')
require('prismjs/components/prism-java')
require('prismjs/components/prism-julia')
require('prismjs/components/prism-kotlin')
Expand All @@ -25,6 +28,12 @@ require('prismjs/components/prism-r')
require('prismjs/components/prism-ruby')
require('prismjs/components/prism-rust')
require('prismjs/components/prism-scala')
require('prismjs/components/prism-powershell')
require('prismjs/components/prism-ruby')
require('prismjs/components/prism-rust')
require('prismjs/components/prism-solidity')
require('prismjs/components/prism-swift')
require('prismjs/components/prism-visual-basic')
require('prismjs/components/prism-zig')

const prismSupportedLanguages = new Map<string, string>([
Expand All @@ -37,12 +46,18 @@ const prismSupportedLanguages = new Map<string, string>([
['rss', 'markup'],
['sh', 'bash'],
['c', 'c'],
['h', 'clike'],
['h', 'c'],
['cc', 'cpp'],
['cpp', 'cpp'],
['hpp', 'cpp'],
['cs', 'csharp'],
['cshtml', 'cshtml'],
['css', 'css'],
['dart', 'dart'],
['f', 'fortran'],
['fpp', 'fortran'],
['f90', 'fortran'],
['fs', 'fsharp'],
['java', 'java'],
['jl', 'julia'],
['js', 'javascript'],
Expand All @@ -60,17 +75,30 @@ const prismSupportedLanguages = new Map<string, string>([
['ocaml', 'ocaml'],
['php', 'php'],
['py', 'python'],
['R', 'r'],
['r', 'r'],
['ps1', 'powershell'],
['psm1', 'powershell'],
['razor', 'cshtml'],
['rb', 'ruby'],
['reason', 'reason'],
['rs', 'rust'],
['sass', 'sass'],
['scala', 'scala'],
['scss', 'scss'],
['sol', 'solidity'],
['sql', 'sql'],
['swift', 'swift'],
// svelte does not have full support yet - may look into this third party
// https://github.com/PrismJS/prism/issues/2090
['svelte', 'markup'],
['ts', 'typescript'],
['tsx', 'tsx'],
['vb', 'visual-basic'],
['vba', 'visual-basic'],
['vbs', 'visual-basic'],
// vue does not have full support yet
// https://github.com/PrismJS/prism/issues/1665
['vue', 'markup'],
['wasm', 'wasm'],
['yaml', 'yaml'],
['zig', 'zig'],
Expand All @@ -81,7 +109,10 @@ const DEFAULT_LANGUAGE_TYPE: Language = 'markup'
export function prismLanguageMapper(fileName: string): Language {
const fileExtension = fileName.split('.').pop() ?? ''

const supportedLanguage = prismSupportedLanguages.get(fileExtension)
// casting this to lower, as in Sentry I've seen some file extensions in uppercase
const supportedLanguage = prismSupportedLanguages.get(
fileExtension.toLowerCase()
)
// we need to cast this, because we're adding in extra languages that aren't in the prism-react-renderer package
if (supportedLanguage) return supportedLanguage as Language

Expand Down

0 comments on commit 6032eb0

Please sign in to comment.