Skip to content

Commit

Permalink
More edge cases for duplicate refs
Browse files Browse the repository at this point in the history
  • Loading branch information
zampino committed Jul 8, 2024
1 parent 50514a6 commit 53df7e2
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions src/nextjournal/markdown/commonmark.clj
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,10 @@
BlockFormula (swap! !ctx update-current z/append-child {:type :block-formula :text (.getLiteral ^BlockFormula node)})
FootnoteReference (swap! !ctx (fn [{:as ctx :keys [label->footnote-ref]}]
(let [label (.getLabel ^FootnoteReference node)
footnote-ref {:type :footnote-ref
:ref (count label->footnote-ref)
:label label}]
footnote-ref (or (get label->footnote-ref label)
{:type :footnote-ref
:ref (count label->footnote-ref)
:label label})]
(-> ctx
(update-current z/append-child footnote-ref)
(update :label->footnote-ref assoc label footnote-ref)))))
Expand All @@ -187,12 +188,13 @@
(-> ctx
:doc z/root
(assoc :footnotes
(into []
;; there will never be references without definitions, but the contrary may happen
(keep (fn [{:as footnote :keys [label]}]
(when (contains? label->footnote-ref label)
(assoc footnote :ref (:ref (label->footnote-ref label))))))
(-> @!ctx :footnotes z/root :content)))))))
;; there will never be references without definitions, but the contrary may happen
(vec
(sort-by :ref
(keep (fn [{:as footnote :keys [label]}]
(when (contains? label->footnote-ref label)
(assoc footnote :ref (:ref (label->footnote-ref label)))))
(-> @!ctx :footnotes z/root :content)))))))))

(defn parse
([md] (parse {} md))
Expand Down Expand Up @@ -237,4 +239,15 @@ And what.
(require '[nextjournal.markdown :as md])
(md/parse text-with-footnotes)

(parse "some https://github.com and what"))
(parse (slurp "../clerk-px23/README.md"))

(parse "Knuth's _Literate Programming_[^literateprogramming][^knuth84] emphasized the importance of focusing on human beings as consumers of computer programs. His original implementation involved authoring files that combine source code and documentation, which were then divided into two derived artifacts: source code for the computer and a typeset document in natural language to explain the program.
[^knuth84]: [Literate Programming](https://doi.org/10.1093/comjnl/27.2.97)
[^literateprogramming]: An extensive archive of related material is maintained [here](http://www.literateprogramming.com).")

(parse "this might[^reuse] here[^another] and here[^reuse] here
[^another]: stuff
[^reuse]: define here
"))

0 comments on commit 53df7e2

Please sign in to comment.