Skip to content

Commit f265716

Browse files
committed
Fix footnote order in table cells
Related-to: mdx-js/mdx#1638. Closes remarkjs/remark-gfm#35.
1 parent b2571b9 commit f265716

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

lib/handlers/table.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@ export function table(h, node) {
2424
while (++index < rows.length) {
2525
const row = rows[index].children
2626
const name = index === 0 ? 'th' : 'td'
27-
let pos = node.align ? align.length : row.length
2827
/** @type {Array.<Content>} */
2928
const out = []
29+
let cellIndex = -1
30+
const length = node.align ? align.length : row.length
3031

31-
while (pos--) {
32-
const cell = row[pos]
33-
out[pos] = h(cell, name, {align: align[pos]}, cell ? all(h, cell) : [])
32+
while (++cellIndex < length) {
33+
const cell = row[cellIndex]
34+
out.push(
35+
h(cell, name, {align: align[cellIndex]}, cell ? all(h, cell) : [])
36+
)
3437
}
3538

3639
result[index] = h(rows[index], 'tr', wrap(out, true))

test/footnote.js

+38
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,44 @@ test('Footnote', (t) => {
375375
'should render footnotes in tables'
376376
)
377377

378+
t.equal(
379+
toHtml(
380+
// @ts-expect-error: fine.
381+
toHast(
382+
fromMarkdown(
383+
`| [^1] | [^2] |
384+
| ---- | ---- |
385+
386+
[^1]: a
387+
[^2]: b`,
388+
{
389+
extensions: [gfm()],
390+
mdastExtensions: [gfmFromMarkdown()]
391+
}
392+
)
393+
)
394+
),
395+
`<table>
396+
<thead>
397+
<tr>
398+
<th><sup><a href="#user-content-fn-1" id="user-content-fnref-1" data-footnote-ref aria-describedby="footnote-label">1</a></sup></th>
399+
<th><sup><a href="#user-content-fn-2" id="user-content-fnref-2" data-footnote-ref aria-describedby="footnote-label">2</a></sup></th>
400+
</tr>
401+
</thead>
402+
</table>
403+
<section data-footnotes class="footnotes"><h2 id="footnote-label" class="sr-only">Footnotes</h2>
404+
<ol>
405+
<li id="user-content-fn-1">
406+
<p>a <a href="#user-content-fnref-1" data-footnote-backref class="data-footnote-backref" aria-label="Back to content">↩</a></p>
407+
</li>
408+
<li id="user-content-fn-2">
409+
<p>b <a href="#user-content-fnref-2" data-footnote-backref class="data-footnote-backref" aria-label="Back to content">↩</a></p>
410+
</li>
411+
</ol>
412+
</section>`,
413+
'should render footnotes in table cells'
414+
)
415+
378416
t.equal(
379417
toHtml(
380418
// @ts-expect-error: fine.

0 commit comments

Comments
 (0)