Skip to content

Commit b169404

Browse files
tarunrajputdotslashtarunslorber
authored
feat(theme-common): code block MagicComments support for Lua/Haskell -- and WebAssembly ;; (#8870)
Co-authored-by: Tarun Chauhan <[email protected]> Co-authored-by: Sébastien Lorber <[email protected]>
1 parent 08cfe4e commit b169404

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

packages/docusaurus-theme-common/src/utils/codeBlockUtils.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ const commentPatterns = {
1919
jsx: {start: '\\{\\s*\\/\\*', end: '\\*\\/\\s*\\}'},
2020
bash: {start: '#', end: ''},
2121
html: {start: '<!--', end: '-->'},
22+
lua: {start: '--', end: ''},
23+
wasm: {start: '\\;\\;', end: ''},
2224
};
2325

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

88+
case 'lua':
89+
case 'haskell':
90+
case 'sql':
91+
return getCommentPattern(['lua'], magicCommentDirectives);
92+
93+
case 'wasm':
94+
return getCommentPattern(['wasm'], magicCommentDirectives);
95+
8696
default:
87-
// All comment types
97+
// All comment types except lua and wasm
8898
return getCommentPattern(
89-
Object.keys(commentPatterns) as CommentType[],
99+
Object.keys(commentPatterns).filter(
100+
(pattern) => !['lua', 'wasm'].includes(pattern),
101+
) as CommentType[],
90102
magicCommentDirectives,
91103
);
92104
}

website/_dogfooding/_pages tests/code-block-tests.mdx

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,50 @@ export default function MyReactPage() {
267267
);
268268
}
269269
```
270+
271+
## Magic comments tests
272+
273+
```lua title="lua_sum.lua"
274+
function sum(n)
275+
-- highlight-next-line
276+
local result = 0
277+
for i = 1, n do
278+
-- highlight-start
279+
result = result + i
280+
end
281+
-- highlight-end
282+
print(result)
283+
end
284+
```
285+
286+
```haskell title="haskell.hs"
287+
stringLength :: String -> Int
288+
-- highlight-next-line
289+
stringLength [] = 0
290+
stringLength (x:xs) = 1 + stringLength xs
291+
```
292+
293+
```wasm title="sum_webAssembly.wasm"
294+
(module
295+
;; highlight-next-line
296+
(func $add (param $a i32) (param $b i32) (result i32)
297+
local.get $a
298+
;; highlight-start
299+
local.get $b
300+
i32.add)
301+
;; highlight-end
302+
(export "add" (func $add)))
303+
```
304+
305+
```sql title="sql_query.sql"
306+
-- highlight-start
307+
SELECT *
308+
FROM orders
309+
-- highlight-end
310+
WHERE customer_id IN (
311+
SELECT customer_id
312+
-- highlight-next-line
313+
FROM customers
314+
WHERE country = 'USA'
315+
)
316+
```

0 commit comments

Comments
 (0)