@@ -3,6 +3,23 @@ import { visit } from 'unist-util-visit';
33
44import { TAG_TRANSFORMS } from '../constants.mjs' ;
55
6+ /**
7+ * Checks whether a HAST node is the generated GFM footnotes section.
8+ * @param {import('hast').Element } node
9+ */
10+ const isFootnotesSection = node =>
11+ node ?. type === 'element' &&
12+ node . tagName === 'section' &&
13+ ( node . properties ?. dataFootnotes !== undefined ||
14+ node . properties ?. className ?. includes ( 'footnotes' ) ) ;
15+
16+ /**
17+ * Finds the generated page Layout node.
18+ * @param {import('hast').Root } tree
19+ */
20+ const findLayout = tree =>
21+ tree . children . find ( node => node . name === 'Layout' && node . children ) ;
22+
623/**
724 * @template {import('unist').Node} T
825 * @param {T } tree
@@ -43,14 +60,17 @@ const transformer = tree => {
4360 }
4461 } ) ;
4562
46- // Are there footnotes?
47- if ( tree . children . at ( - 1 ) . tagName === 'section' ) {
48- const section = tree . children . pop ( ) ;
49- // If so, move it into the proper location
50- // Root -> Article -> Main content
51- tree . children [ 2 ] ?. children [ 1 ] ?. children [ 0 ] ?. children ?. push (
52- ...section . children
53- ) ;
63+ const index = tree . children . findLastIndex ( isFootnotesSection ) ;
64+
65+ if ( index !== - 1 ) {
66+ const [ section ] = tree . children . splice ( index , 1 ) ;
67+ const layout = findLayout ( tree ) ;
68+
69+ if ( layout ) {
70+ layout . children . push ( section ) ;
71+ } else {
72+ tree . children . push ( section ) ;
73+ }
5474 }
5575} ;
5676
0 commit comments