Skip to content

Commit 1217a60

Browse files
committed
working on tables
1 parent 3b5acdb commit 1217a60

File tree

7 files changed

+119
-2
lines changed

7 files changed

+119
-2
lines changed

app/routes/MdxRoute.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ let components = {
4040
"UrlBox": Markdown.UrlBox.make,
4141
"Video": Markdown.Video.make,
4242
"Warn": Markdown.Warn.make,
43+
"MarkdownTable": Markdown.MarkdownTable.make,
4344
}
4445

4546
// The loadAllMdx function logs out all of the file contents as it reads them, which is noisy and not useful.

docs/manual/overview.mdx

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: "Overview"
33
metaTitle: "Language Features Overview"
44
description: "A quick overview on ReScript's syntax"
5-
canonical: "/docs/manual/overview"
5+
canonical: "/docs/manual/v12.0.0/overview"
66
section: "Language Features"
77
order: 1
88
---
@@ -13,39 +13,53 @@ order: 1
1313

1414
### Semicolon
1515

16+
<MarkdownTable>
17+
{`
1618
| JavaScript | ReScript |
1719
| ---------------------------------- | -------------------- |
1820
| Rules enforced by linter/formatter | No semicolon needed! |
21+
`}</MarkdownTable>
1922

2023
### Comments
2124

25+
<MarkdownTable>
26+
{`
2227
| JavaScript | ReScript |
2328
| -------------------- | -------------------------------- |
2429
| `// Line comment` | Same |
2530
| `/* Comment */` | Same |
2631
| `/** Doc Comment */` | `/** Before Types/Values */` |
2732
| | `/*** Standalone Doc Comment */` |
33+
`}</MarkdownTable>
2834
2935
### Variable
3036
37+
<MarkdownTable>
38+
{`
3139
| JavaScript | ReScript |
3240
| ----------------------- | ------------------------------------- |
3341
| `const x = 5;` | `let x = 5` |
3442
| `var x = y;` | No equivalent (thankfully) |
3543
| `let x = 5; x = x + 1;` | `let x = ref(5); x := x.contents + 1` |
44+
`}</MarkdownTable>
3645
3746
### String & Character
3847
48+
<MarkdownTable>
49+
{`
3950
| JavaScript | ReScript |
4051
| ---------------------------- | --------------------- |
4152
| `"Hello world!"` | Same |
4253
| `'Hello world!'` | Strings must use `"` |
4354
| `"hello " + "world"` | `"hello " ++ "world"` |
4455
| `` `hello ${message}` `` | Same |
4556
| `` sql`select ${fnName};` `` | Same |
57+
`}</MarkdownTable>
4658
4759
### Boolean
4860
61+
<MarkdownTable>
62+
{`
4963
| JavaScript | ReScript |
5064
| ------------------------------------ | ---------------------------------------------- |
5165
| `true`, `false` | Same |
@@ -54,64 +68,83 @@ order: 1
5468
| `a === b`, `a !== b` | Same |
5569
| No deep equality (recursive compare) | `a == b`, `a != b` |
5670
| `a == b` | No equality with implicit casting (thankfully) |
71+
`}</MarkdownTable>
5772
5873
### Number
5974
75+
<MarkdownTable>
76+
{`
6077
| JavaScript | ReScript |
6178
| ----------- | ------------ |
6279
| `3` | Same \* |
6380
| `3.1415` | Same |
6481
| `3 + 4` | Same |
6582
| `3.0 + 4.5` | `3.0 +. 4.5` |
6683
| `5 % 3` | `mod(5, 3)` |
84+
`}</MarkdownTable>
6785
6886
\* JS has no distinction between integer and float.
6987
7088
### Object/Record
7189
90+
<MarkdownTable>
91+
{`
7292
| JavaScript | ReScript |
7393
| ------------------- | --------------------------------------- |
7494
| no types | `type point = {x: int, mutable y: int}` |
7595
| `{x: 30, y: 20}` | Same |
7696
| `point.x` | Same |
7797
| `point.y = 30;` | Same |
7898
| `{...point, x: 30}` | Same |
99+
`}</MarkdownTable>
79100
80101
### Array
81102
103+
<MarkdownTable>
104+
{`
82105
| JavaScript | ReScript |
83106
| ------------------ | --------------------- |
84107
| `[1, 2, 3]` | Same |
85108
| `myArray[1] = 10` | Same |
86109
| `[1, "Bob", true]` | `(1, "Bob", true)` \* |
87110

88111
\* ReScript does not have heterogenous arrays. Use tuples or [Untagged Variants](variant#untagged-variants) instead.
112+
`}</MarkdownTable>
89113
90114
### Null
91115
116+
<MarkdownTable>
117+
{`
92118
| JavaScript | ReScript |
93119
| ------------------- | --------- |
94120
| `null`, `undefined` | `None` \* |
121+
`}</MarkdownTable>
95122
96123
\* Again, only a spiritual equivalent; we don't have nulls, nor null bugs! But we do have an `option` type for when you actually need nullability.
97124
98125
### Function
99126
127+
<MarkdownTable>
128+
{`
100129
| JavaScript | ReScript |
101130
| ------------------------------- | ---------------------------- |
102131
| `arg => retVal` | Same |
103132
| `function named(arg) {...}` | `let named = (arg) => {...}` |
104133
| `const f = function(arg) {...}` | `let f = (arg) => {...}` |
105134
| `add(4, add(5, 6))` | Same |
135+
`}</MarkdownTable>
106136
107137
### Async Function / Await
108138
139+
<MarkdownTable>
140+
{`
109141
| JavaScript | ReScript |
110142
| --------------------------------------- | ----------------------------------------------------- |
111143
| `async (arg) => {...}` | Same |
112144
| `async function named(arg) {...}` | `let named = async (arg) => {...}` |
113145
| `await somePromise` | Same |
114146
| `async (arg): Promise<string> => {...}` | `async (arg): string => {...}` (note the return type) |
147+
`}</MarkdownTable>
115148
116149
### Blocks
117150
@@ -148,49 +181,64 @@ order: 1
148181
149182
### If-else
150183
184+
<MarkdownTable>
185+
{`
151186
| JavaScript | ReScript |
152187
| --------------------- | --------------------------------------------------------------------------------- |
153188
| `if (a) {b} else {c}` | `if a {b} else {c}` \* |
154189
| `a ? b : c` | Same |
155190
| `switch` | `switch` but [super-powered pattern matching!](pattern-matching-destructuring.md) |
191+
`}</MarkdownTable>
156192
157193
\* Our conditionals are always expressions! You can write `let result = if a {"hello"} else {"bye"}`
158194
159195
### Destructuring
160196
197+
<MarkdownTable>
198+
{`
161199
| JavaScript | ReScript |
162200
| ----------------------------- | --------------------------- |
163201
| `const {a, b} = data` | `let {a, b} = data` |
164202
| `const [a, b] = data` | `let [a, b] = data` \* |
165203
| `const {a: aa, b: bb} = data` | `let {a: aa, b: bb} = data` |
204+
`}</MarkdownTable>
166205
167206
\* Gives good compiler warning that `data` might not be of length 2.
168207
169208
### Loop
170209
210+
<MarkdownTable>
211+
{`
171212
| JavaScript | ReScript |
172213
| ------------------------------------- | ---------------------------- |
173214
| `for (let i = 0; i <= 10; i++) {...}` | `for i in 0 to 10 {...}` |
174215
| `for (let i = 10; i >= 0; i--) {...}` | `for i in 10 downto 0 {...}` |
175216
| `while (true) {...}` | `while true {...}` |
217+
`}</MarkdownTable>
176218
177219
### JSX
178220
221+
<MarkdownTable>
222+
{`
179223
| JavaScript | ReScript |
180224
| ----------------------------------------- | -------------------------- |
181225
| `<Comp message="hi" onClick={handler} />` | Same |
182226
| `<Comp message={message} />` | `<Comp message />` \* |
183227
| `<input checked />` | `<input checked=true />` |
184228
| No children spread | `<Comp>...children</Comp>` |
229+
`}</MarkdownTable>
185230
186231
\* Argument punning!
187232
188233
### Exception
189234
235+
<MarkdownTable>
236+
{`
190237
| JavaScript | ReScript |
191238
| ----------------------------------------- | ------------------------------------------------ |
192239
| `throw new SomeError(...)` | `throw(SomeException(...))` |
193240
| `try {a} catch (err) {...} finally {...}` | `try a catch { \| SomeException(err) => ...}` \* |
241+
`}</MarkdownTable>
194242
195243
\* No finally.
196244
@@ -231,6 +279,8 @@ The last expression of a block delimited by `{}` implicitly returns (including f
231279
232280
## Common Features' JS Output
233281
282+
<MarkdownTable>
283+
{`
234284
| Feature | Example | JavaScript Output |
235285
| ------------------------------- | ------------------------------------ | ------------------------------------------ |
236286
| String | `"Hello"` | `"Hello"` |
@@ -253,5 +303,6 @@ The last expression of a block delimited by `{}` implicitly returns (including f
253303
| Record | `type t = {b: int}; let a = {b: 10}` | `var a = {b: 10}` |
254304
| Multiline Comment | `/* Comment here */` | Not in output |
255305
| Single line Comment | `// Comment here` | Not in output |
306+
`}</MarkdownTable>
256307
257308
_Note that this is a cleaned-up comparison table; a few examples' JavaScript output are slightly different in reality._

package-lock.json

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
"mdast": "^2.3.2",
4646
"mdast-util-from-markdown": "^2.0.2",
4747
"mdast-util-toc": "^7.1.0",
48+
"micromark": "^4.0.2",
49+
"micromark-extension-gfm": "^3.0.0",
4850
"next": "^15.4.7",
4951
"next-mdx-remote": "^5.0.0",
5052
"react": "^19.1.0",
@@ -87,4 +89,4 @@
8789
"tailwindcss": "^4",
8890
"vite": "^7.0.6"
8991
}
90-
}
92+
}

src/components/Markdown.res

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,3 +561,43 @@ module Video = {
561561
/* Sets our preferred branded styles
562562
We most likely will never need a different ~components
563563
option on our website. */
564+
565+
module MarkdownTable = {
566+
// react-router-mdx doesn't support gfm tables natively yet, so this is a simple table wrapper for us to do it manually
567+
// If it ever adds support, we can remove this component
568+
569+
type extension
570+
type htmlExtension
571+
572+
@module("micromark-extension-gfm")
573+
external gfmHtml: unit => htmlExtension = "gfmHtml"
574+
575+
@module("micromark-extension-gfm")
576+
external gfm: unit => extension = "gfm"
577+
578+
type micromarkOptions = {
579+
extensions: array<extension>,
580+
htmlExtensions: array<htmlExtension>,
581+
}
582+
583+
@module("micromark")
584+
external micromark: (string, micromarkOptions) => string = "micromark"
585+
586+
@react.component
587+
let make = (~children) => {
588+
let html = micromark(
589+
children,
590+
{
591+
extensions: [gfm()],
592+
htmlExtensions: [gfmHtml()],
593+
},
594+
)
595+
<div
596+
dangerouslySetInnerHTML={{
597+
"__html": html,
598+
}}
599+
className="markdown-table"
600+
>
601+
</div>
602+
}
603+
}

src/components/Markdown.resi

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,8 @@ module Video: {
174174
/* Sets our preferred branded styles
175175
We most likely will never need a different ~components
176176
option on our website. */
177+
178+
module MarkdownTable: {
179+
@react.component
180+
let make: (~children: string) => React.element
181+
}

styles/_markdown.css

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,20 @@
7272
}
7373
}
7474
}
75+
}
76+
77+
.markdown-table {
78+
@apply overflow-x-auto mt-10 mb-16;
79+
80+
/* table {
81+
@apply
82+
} */
83+
84+
td {
85+
@apply border-b border-gray-20 py-3 pr-8;
86+
}
87+
88+
th {
89+
@apply py-2 pr-8 text-12 text-gray-60 uppercase font-medium tracking-wide text-left border-b-2 border-gray-20;
90+
}
7591
}

0 commit comments

Comments
 (0)