diff --git a/org/html_writer.go b/org/html_writer.go index bd8a119..a12d4bf 100644 --- a/org/html_writer.go +++ b/org/html_writer.go @@ -46,6 +46,7 @@ type HTMLWriter struct { type footnotes struct { mapping map[string]int list []*FootnoteDefinition + unused map[string]*FootnoteDefinition } var emphasisTags = map[string][]string{ @@ -89,6 +90,7 @@ func NewHTMLWriter() *HTMLWriter { TopLevelHLevel: 2, footnotes: &footnotes{ mapping: map[string]int{}, + unused: map[string]*FootnoteDefinition{}, }, } } @@ -228,7 +230,10 @@ func (w *HTMLWriter) WriteFootnotes(d *Document) { w.WriteString(`
` + "\n") w.WriteString(`
` + "\n") w.WriteString(`
` + "\n") - for i, definition := range w.footnotes.list { + + // iterate by index instead of ranging, since new footnotes can be added when writing the definitions + for i := 0; i < len(w.footnotes.list); i++ { + definition := w.footnotes.list[i] id := i + 1 if definition == nil { name := "" @@ -657,6 +662,14 @@ func (fs *footnotes) add(f FootnoteLink) int { if i, ok := fs.mapping[f.Name]; ok && f.Name != "" { return i } + + if def, ok := fs.unused[f.Name]; ok && f.Name != "" && f.Definition == nil { + // if there was an a previously unused definition with the same name, attach it to this link + // (this enables footnotes from another footnote's definition) + f.Definition = def + delete(fs.unused, f.Name) + } + fs.list = append(fs.list, f.Definition) i := len(fs.list) - 1 if f.Name != "" { @@ -668,5 +681,9 @@ func (fs *footnotes) add(f FootnoteLink) int { func (fs *footnotes) updateDefinition(f FootnoteDefinition) { if i, ok := fs.mapping[f.Name]; ok { fs.list[i] = &f + } else { + // this could either be an unused definition or one used in another footnote + // save in case it's the latter + fs.unused[f.Name] = &f } } diff --git a/org/testdata/footnotes.html b/org/testdata/footnotes.html index a81efa5..b7498a5 100644 --- a/org/testdata/footnotes.html +++ b/org/testdata/footnotes.html @@ -116,7 +116,13 @@ Rather, it will be somewhere down below in the footnotes section.

There's multiple reasons for that. Among others, doing so requires i18n (to recognize the section) and silently -hides content before and after the footnotes.

+hides content before and after the footnotes9.

+
+
+
+9 +
+

Footnotes can be linked from another footnote's definition.

diff --git a/org/testdata/footnotes.org b/org/testdata/footnotes.org index 44a9019..ab200a7 100644 --- a/org/testdata/footnotes.org +++ b/org/testdata/footnotes.org @@ -41,8 +41,11 @@ This shouldn't happen when the definition line and the line after that are empty [fn:7] There's multiple reasons for that. Among others, doing so requires i18n (to recognize the section) and silently -hides content before and after the footnotes. +hides content before and after the footnotes[fn:8]. this is not part of [fn:7] anymore as there are 2 blank lines in between! + + +[fn:8] Footnotes can be linked from another footnote's definition. diff --git a/org/testdata/footnotes.pretty_org b/org/testdata/footnotes.pretty_org index 44a9019..ab200a7 100644 --- a/org/testdata/footnotes.pretty_org +++ b/org/testdata/footnotes.pretty_org @@ -41,8 +41,11 @@ This shouldn't happen when the definition line and the line after that are empty [fn:7] There's multiple reasons for that. Among others, doing so requires i18n (to recognize the section) and silently -hides content before and after the footnotes. +hides content before and after the footnotes[fn:8]. this is not part of [fn:7] anymore as there are 2 blank lines in between! + + +[fn:8] Footnotes can be linked from another footnote's definition.