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 Lua/Haskell -- and WebAssembly ;; #8870

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
16 changes: 14 additions & 2 deletions packages/docusaurus-theme-common/src/utils/codeBlockUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const commentPatterns = {
jsx: {start: '\\{\\s*\\/\\*', end: '\\*\\/\\s*\\}'},
bash: {start: '#', end: ''},
html: {start: '<!--', end: '-->'},
lua: {start: '--', end: ''},
wasm: {start: '\\;\\;', end: ''},
};

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

case 'lua':
case 'haskell':
case 'sql':
return getCommentPattern(['lua'], magicCommentDirectives);

case 'wasm':
return getCommentPattern(['wasm'], magicCommentDirectives);

default:
// All comment types
// All comment types except lua and wasm
return getCommentPattern(
Object.keys(commentPatterns) as CommentType[],
Object.keys(commentPatterns).filter(
(pattern) => !['lua', 'wasm'].includes(pattern),
) as CommentType[],
magicCommentDirectives,
);
}
Expand Down
46 changes: 46 additions & 0 deletions website/_dogfooding/_pages tests/code-block-tests.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,49 @@ export default function MyReactPage() {
);
}
```
## Magic comments tests
slorber marked this conversation as resolved.
Show resolved Hide resolved

slorber marked this conversation as resolved.
Show resolved Hide resolved
```lua title="lua_sum.lua"
function sum(n)
-- highlight-next-line
local result = 0
for i = 1, n do
-- highlight-start
result = result + i
end
-- highlight-end
print(result)
end
```

```haskell title="haskell.hs"
stringLength :: String -> Int
-- highlight-next-line
stringLength [] = 0
stringLength (x:xs) = 1 + stringLength xs
```

```wasm title="sum_webAssembly.wasm"
(module
;; highlight-next-line
(func $add (param $a i32) (param $b i32) (result i32)
local.get $a
;; highlight-start
local.get $b
i32.add)
;; highlight-end
(export "add" (func $add)))
```

```sql title="sql_query.sql"
-- highlight-start
SELECT *
FROM orders
-- highlight-end
WHERE customer_id IN (
SELECT customer_id
-- highlight-next-line
FROM customers
WHERE country = 'USA'
)
```