Skip to content

Commit

Permalink
Fix string escaping in markdown table (#49)
Browse files Browse the repository at this point in the history
The idea behind this function is to escape newlines and pipes in particular places, but it throws a NPE because str/replace is used incorrectly. There's a way to fix the replacement map by using vector keys but I wasn't comfortable using it and has already proven error-prone.
  • Loading branch information
frenchy64 authored Jul 16, 2024
1 parent 48f7e73 commit 427377c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/flanders/markdown.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@
(defleaf ReferenceNode [text anchor jump-anchor])

(defn ready-for-table [str]
(str/replace str #"(\n|\|)" {"\n" " "
"\\|" "\\\\|"}))
(-> str
(str/replace \newline \space)
(str/replace "|" "\\\\|")))

(defn ->default [{:keys [default values]}]
(when (and default (> (count values) 1))
Expand Down
31 changes: 30 additions & 1 deletion test/flanders/markdown_test.clj
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
(ns flanders.markdown-test
(:require
[clojure.string :as str]
[clojure.test :refer [deftest is]]
[flanders.core :as f]
[flanders.markdown :as md]))

(deftest ready-for-table-test
(is (= " \\\\|" (md/ready-for-table "\n|"))))

(f/def-entity-type Actor
{}
(f/optional-entries
(f/entry :confidence f/any
:description "separated\nby\nspaces within table")))

(deftest signature-type->markdown
(is (= "### Signature\n\n() => Anything\n\n\n"
(md/->markdown (f/sig :parameters []))))
Expand All @@ -26,4 +36,23 @@
(is (= "# `Foo`\n\n### Description\n\nThe Foo.\n\n### Signature\n\n() => Anything\n\n\n"
(md/->markdown (f/sig :name "Foo"
:description "The Foo."
:parameters [])))))
:parameters []))))

(is (= ["<a id=\"top\"></a>"
"# *Actor* Object"
""
"| Property | Type | Description | Required? |"
"| -------- | ---- | ----------- | --------- |"
"|[confidence](#propertyconfidence-anything)|Anything|separated by spaces within table||"
""
""
"<a id=\"propertyconfidence-anything\"></a>"
"## Property confidence ∷ Anything"
""
"separated"
"by"
"spaces within table"
""
"* This entry is optional"]
(-> (md/->markdown Actor)
str/split-lines))))

0 comments on commit 427377c

Please sign in to comment.