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: Even more language support #3138

Merged
merged 5 commits into from
Aug 26, 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
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')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just some food for thought: is it possible to load these on-demand after matching the file extension?
its also weird that you are using require here instead of bare import statements. is there a particular reason for that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uhh was just following along with the example that they provided in the documentation for the package we're using, it seems like just replacing require with import everything works fine. Gonna quickly make the change to only load in the language required.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually my "quick change" is a bit more involved, I think I'll merge what I have now, and tidy things up with your suggestion afterwards, sound all good to you @Swatinem ?

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
Loading