Skip to content

Commit caad9cd

Browse files
committed
Fix footnote ordering in tables
Closes mdx-js/mdx#1638.
1 parent cb58ea7 commit caad9cd

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

lib/handlers/table.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ import {all} from '../traverse.js'
1616
*/
1717
export function table(h, node) {
1818
const rows = node.children
19-
let index = rows.length
19+
let index = -1
2020
const align = node.align || []
2121
/** @type {Array.<Element>} */
2222
const result = []
2323

24-
while (index--) {
24+
while (++index < rows.length) {
2525
const row = rows[index].children
2626
const name = index === 0 ? 'th' : 'td'
2727
let pos = node.align ? align.length : row.length

package.json

+6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@
4747
"devDependencies": {
4848
"@types/tape": "^4.0.0",
4949
"c8": "^7.0.0",
50+
"hast-util-to-html": "^8.0.0",
51+
"mdast-util-footnote": "^1.0.0",
52+
"mdast-util-from-markdown": "^1.0.0",
53+
"mdast-util-gfm": "^1.0.0",
54+
"micromark-extension-footnote": "^1.0.0",
55+
"micromark-extension-gfm": "^1.0.0",
5056
"prettier": "^2.0.0",
5157
"remark-cli": "^9.0.0",
5258
"remark-preset-wooorm": "^8.0.0",

test/footnote.js

+50
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import test from 'tape'
22
import {u} from 'unist-builder'
3+
import {fromMarkdown} from 'mdast-util-from-markdown'
4+
import {gfm} from 'micromark-extension-gfm'
5+
import {gfmFromMarkdown} from 'mdast-util-gfm'
6+
import {footnote} from 'micromark-extension-footnote'
7+
import {footnoteFromMarkdown} from 'mdast-util-footnote'
8+
import {toHtml} from 'hast-util-to-html'
39
import {toHast} from '../index.js'
410

511
test('Footnote', (t) => {
@@ -234,5 +240,49 @@ test('Footnote', (t) => {
234240
'should render `footnote`s (#3)'
235241
)
236242

243+
t.equal(
244+
toHtml(
245+
// @ts-expect-error: fine.
246+
toHast(
247+
fromMarkdown(
248+
`| Footnotes |
249+
| ---- |
250+
| [^1] |
251+
| [^2] |
252+
253+
[^1]: a
254+
[^2]: b`,
255+
{
256+
extensions: [gfm(), footnote()],
257+
mdastExtensions: [gfmFromMarkdown, footnoteFromMarkdown]
258+
}
259+
)
260+
)
261+
),
262+
`<table>
263+
<thead>
264+
<tr>
265+
<th>Footnotes</th>
266+
</tr>
267+
</thead>
268+
<tbody>
269+
<tr>
270+
<td><a href="#fn1" class="footnote-ref" id="fnref1" role="doc-noteref"><sup>1</sup></a></td>
271+
</tr>
272+
<tr>
273+
<td><a href="#fn2" class="footnote-ref" id="fnref2" role="doc-noteref"><sup>2</sup></a></td>
274+
</tr>
275+
</tbody>
276+
</table>
277+
<section class="footnotes" role="doc-endnotes">
278+
<hr>
279+
<ol>
280+
<li id="fn1" role="doc-endnote">a<a href="#fnref1" class="footnote-back" role="doc-backlink">↩</a></li>
281+
<li id="fn2" role="doc-endnote">b<a href="#fnref2" class="footnote-back" role="doc-backlink">↩</a></li>
282+
</ol>
283+
</section>`,
284+
'should render footnotes in tables'
285+
)
286+
237287
t.end()
238288
})

0 commit comments

Comments
 (0)