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 TeX/LaTeX/Matlab #8982

Merged
merged 3 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 9 additions & 2 deletions packages/docusaurus-theme-common/src/utils/codeBlockUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const commentPatterns = {
html: {start: '<!--', end: '-->'},
lua: {start: '--', end: ''},
wasm: {start: '\\;\\;', end: ''},
tex: {start: '%', end: ''},
};

type CommentType = keyof typeof commentPatterns;
Expand Down Expand Up @@ -85,6 +86,11 @@ function getAllMagicCommentDirectiveStyles(
// Text uses HTML, front matter uses bash
return getCommentPattern(['html', 'jsx', 'bash'], magicCommentDirectives);

case 'tex':
case 'latex':
case 'matlab':
return getCommentPattern(['tex'], magicCommentDirectives);

case 'lua':
case 'haskell':
case 'sql':
Expand All @@ -94,10 +100,11 @@ function getAllMagicCommentDirectiveStyles(
return getCommentPattern(['wasm'], magicCommentDirectives);

default:
// All comment types except lua and wasm
// All comment types except lua, wasm and matlab
return getCommentPattern(
Object.keys(commentPatterns).filter(
(pattern) => !['lua', 'wasm'].includes(pattern),
(pattern) =>
!['lua', 'wasm', 'tex', 'latex', 'matlab'].includes(pattern),
) as CommentType[],
magicCommentDirectives,
);
Expand Down
26 changes: 26 additions & 0 deletions website/_dogfooding/_pages tests/code-block-tests.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -322,3 +322,29 @@ WHERE customer_id IN (
WHERE country = 'USA'
)
```

```matlab title="matlab.m"
% highlight-start
function result = times2(n)
result = n * 2;
end
% highlight-end

x = 10;
% highlight-next-line
y = times2(x);
```

```latex title="latex.tex
slorber marked this conversation as resolved.
Show resolved Hide resolved
\begin{document}
\section{Triangles}
% highlight-next-line
\subsection{Pythagoras' Theorem}
Pythagoras's theorem is:
% highlight-start
\begin{equation}
c^2 = a^2 + b^2
\end{equation}
% highlight-end
\end{document}
```