@@ -49,6 +49,21 @@ fetch(`/notes/courses/${course}/${noteSlug}.md`)
4949 const { meta, body } = parseFrontMatter ( md ) ;
5050 document . title = meta . title || noteSlug ;
5151
52+ const footnotes = [ ] ;
53+
54+ const bodyWithoutDefs = body . replace (
55+ / ^ \s * \[ ( [ ^ \] ] + ) \] : \s * ( .+ ) $ / gm,
56+ ( _ , num , text ) => {
57+ footnotes . push ( { num, text : text . trim ( ) } ) ;
58+ return '' ;
59+ }
60+ )
61+
62+ const bodyWithRefs = bodyWithoutDefs . replace (
63+ / \[ \^ ( d + ) \] / g,
64+ ( _ , num ) => `<sup id="fnref${ num } "><a href="#fn${ num } ">${ num } </a></sup>`
65+ ) ;
66+
5267 const back = document . getElementById ( "back-to-course" ) ;
5368 back . href = `course.html?id=${ encodeURIComponent ( course ) } ` ;
5469
@@ -59,7 +74,7 @@ fetch(`/notes/courses/${course}/${noteSlug}.md`)
5974 headingData . length = 0 ;
6075 for ( let k in slugCounts ) delete slugCounts [ k ] ;
6176
62- const dirty = marked . parse ( body ) ;
77+ const dirty = marked . parse ( bodyWithRefs ) ;
6378 const sanitize = window . DOMPurify ?. sanitize || ( s => s ) ;
6479 const contentDiv = document . createElement ( "div" ) ;
6580 contentDiv . innerHTML = sanitize ( dirty ) ;
@@ -108,6 +123,24 @@ fetch(`/notes/courses/${course}/${noteSlug}.md`)
108123 container . insertBefore ( tocNav , contentDiv ) ;
109124 }
110125
126+ if ( footnotes . length ) {
127+ const fnSec = document . createElement ( "section" ) ;
128+ fnSec . className = "footnotes" ;
129+ const hr = document . createElement ( "hr" ) ;
130+ fnSec . appendChild ( hr ) ;
131+
132+ const ol = document . createElement ( "ol" ) ;
133+ footnotes . forEach ( ( { num, text } ) => {
134+ const li = document . createElement ( "li" ) ;
135+ li . id = `fn${ num } ` ;
136+ const fnHtml = sanitize ( marked . parseInline ( text ) ) ;
137+ li . innerHTML = fnHtml ;
138+ ol . appendChild ( li ) ;
139+ } ) ;
140+ fnSec . appendChild ( ol ) ;
141+ container . appendChild ( fnSec ) ;
142+ }
143+
111144 contentDiv . querySelectorAll ( 'table' ) . forEach ( table => {
112145 const wrapper = document . createElement ( 'div' ) ;
113146 wrapper . className = 'table-wrapper' ;
0 commit comments