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(theme-common): code block MagicComments support for (Visual) Basic/Batch/Fortran/COBOL/ML #9671

Merged
merged 1 commit into from
Dec 27, 2023
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
52 changes: 42 additions & 10 deletions packages/docusaurus-theme-common/src/utils/codeBlockUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down
3 changes: 3 additions & 0 deletions project-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ precaching
preconfigured
prerendered
prerendering
printfn
println
prismjs
producthunt
Expand Down Expand Up @@ -303,6 +304,7 @@ sebastienlorber
sensical
setaf
setext
setlocal
Shiki
shiki
shortcodes
Expand Down Expand Up @@ -373,6 +375,7 @@ unswizzle
upvotes
urlset
Vannicatte
vbnet
Vetter
vetter
vfile
Expand Down
55 changes: 55 additions & 0 deletions website/_dogfooding/_pages tests/code-block-tests.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,61 @@ y = times2(x);
\end{document}
```

```vbnet title="vbnet.vb"
Dim languages As New Set(Of String) From {
' highlight-start
"C#",
"Visual Basic",
"F#",
' highlight-end
"PowerShell",
' highlight-next-line
"TypeScript"
}
```

```batch title="cmd.bat"
rem highlight-start
@echo off
setlocal
Rem highlight-end
ipconfig
REM highlight-next-line
echo Docusaurus is awesome
netstat
```

```fortran title="fortran.f90"
! highlight-start
program hello
! highlight-end
implicit none
! highlight-next-line
print *, "Hello, World!"
end program hello
```

```cobol title="cobol.cob"
*> highlight-start
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
*> highlight-end
PROCEDURE DIVISION.
*> highlight-next-line
DISPLAY "Hello, World!".
END PROGRAM HELLO.
```

```fsharp title="fsharp.fsx"
(* highlight-start *)
[<EntryPoint>]
(* highlight-end *)
let main _ =
// highlight-next-line
printfn "Hello, World!"
0
```

## HTML - script + style highlighting

See https://github.com/facebook/docusaurus/issues/9517
Expand Down
Loading