feat(theme-common): code block MagicComments support for (Visual) Basic/Batch/Fortran/COBOL/ML (#9671)

This commit is contained in:
Tatsunori Uchino 2023-12-28 07:02:48 +09:00 committed by GitHub
parent d9d700c54f
commit bd7d64f455
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 100 additions and 10 deletions

View file

@ -13,18 +13,31 @@ const codeBlockTitleRegex = /title=(?<quote>["'])(?<title>.*?)\1/;
const metastringLinesRangeRegex = /\{(?<range>[\d,-]+)\}/;
// Supported types of highlight comments
const commentPatterns = {
const popularCommentPatterns = {
js: {start: '\\/\\/', end: ''},
jsBlock: {start: '\\/\\*', end: '\\*\\/'},
jsx: {start: '\\{\\s*\\/\\*', end: '\\*\\/\\s*\\}'},
bash: {start: '#', end: ''},
html: {start: '<!--', end: '-->'},
} as const;
const commentPatterns = {
...popularCommentPatterns, // shallow copy is sufficient
// minor comment styles
lua: {start: '--', end: ''},
wasm: {start: '\\;\\;', end: ''},
tex: {start: '%', end: ''},
};
vb: {start: "[']", end: ''},
rem: {start: '[Rr][Ee][Mm]\\b', end: ''},
f90: {start: '!', end: ''}, // Free format only
ml: {start: '\\(\\*', end: '\\*\\)'},
cobol: {start: '\\*>', end: ''}, // Free format only
} as const;
type CommentType = keyof typeof commentPatterns;
const popularCommentTypes = Object.keys(
popularCommentPatterns,
) as CommentType[];
export type MagicCommentConfig = {
className: string;
@ -99,15 +112,34 @@ function getAllMagicCommentDirectiveStyles(
case 'wasm':
return getCommentPattern(['wasm'], magicCommentDirectives);
case 'vb':
case 'vbnet':
case 'vba':
case 'visual-basic':
return getCommentPattern(['vb', 'rem'], magicCommentDirectives);
case 'batch':
return getCommentPattern(['rem'], magicCommentDirectives);
case 'basic': // https://github.com/PrismJS/prism/blob/master/components/prism-basic.js#L3
return getCommentPattern(['rem', 'f90'], magicCommentDirectives);
case 'fsharp':
return getCommentPattern(['js', 'ml'], magicCommentDirectives);
case 'ocaml':
case 'sml':
return getCommentPattern(['ml'], magicCommentDirectives);
case 'fortran':
return getCommentPattern(['f90'], magicCommentDirectives);
case 'cobol':
return getCommentPattern(['cobol'], magicCommentDirectives);
default:
// All comment types except lua, wasm and matlab
return getCommentPattern(
Object.keys(commentPatterns).filter(
(pattern) =>
!['lua', 'wasm', 'tex', 'latex', 'matlab'].includes(pattern),
) as CommentType[],
magicCommentDirectives,
);
// All popular comment types
return getCommentPattern(popularCommentTypes, magicCommentDirectives);
}
}