Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 336: Replace new line with <br> in markdown tables #487

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/doc/example.docbook
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@
<entry>model_name</entry>
<entry><link linkend="string">string</link></entry>
<entry>required</entry>
<entry><para>The car model name, e.g. "Z3".</para></entry>
<entry><para>model_name represents the car model name.</para><para>This field typically contains the manufacturer name, e.g. "Porsche 911".</para></entry>
</row>

<row>
Expand Down
4 changes: 3 additions & 1 deletion examples/doc/example.html
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,9 @@ <h3 id="com.example.Model">Model</h3>
<td>model_name</td>
<td><a href="#string">string</a></td>
<td>required</td>
<td><p>The car model name, e.g. &#34;Z3&#34;. </p></td>
<td><p>model_name represents the car model name.

This field typically contains the manufacturer name, e.g. &#34;Porsche 911&#34;. </p></td>
</tr>

<tr>
Expand Down
2 changes: 1 addition & 1 deletion examples/doc/example.json
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@
},
{
"name": "model_name",
"description": "The car model name, e.g. \"Z3\".",
"description": "model_name represents the car model name.\n\nThis field typically contains the manufacturer name, e.g. \"Porsche 911\".",
"label": "required",
"type": "string",
"longType": "string",
Expand Down
12 changes: 6 additions & 6 deletions examples/doc/example.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Represents the status of a vehicle booking.
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| id | [int32](#int32) | | Unique booking status ID. |
| description | [string](#string) | | Booking status description. E.g. &#34;Active&#34;. |
| description | [string](#string) | | Booking status description. E.g. "Active". |



Expand Down Expand Up @@ -195,7 +195,7 @@ Represents a manufacturer of cars.
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| id | [int32](#int32) | required | The unique manufacturer ID. |
| code | [string](#string) | required | A manufacturer code, e.g. &#34;DKL4P&#34;. |
| code | [string](#string) | required | A manufacturer code, e.g. "DKL4P". |
| details | [string](#string) | optional | Manufacturer details (minimum orders et.c.). |
| category | [Manufacturer.Category](#com-example-Manufacturer-Category) | optional | Manufacturer category. Default: CATEGORY_EXTERNAL |

Expand All @@ -213,8 +213,8 @@ Represents a vehicle model.
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| id | [string](#string) | required | The unique model ID. |
| model_code | [string](#string) | required | The car model code, e.g. &#34;PZ003&#34;. |
| model_name | [string](#string) | required | The car model name, e.g. &#34;Z3&#34;. |
| model_code | [string](#string) | required | The car model code, e.g. "PZ003". |
| model_name | [string](#string) | required | model_name represents the car model name.<br><br>This field typically contains the manufacturer name, e.g. "Porsche 911". |
| daily_hire_rate_dollars | [sint32](#sint32) | required | Dollars per day. |
| daily_hire_rate_cents | [sint32](#sint32) | required | Cents per day. |

Expand Down Expand Up @@ -257,8 +257,8 @@ Represents a vehicle category. E.g. &#34;Sedan&#34; or &#34;Truck&#34;.

| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| code | [string](#string) | required | Category code. E.g. &#34;S&#34;. |
| description | [string](#string) | required | Category name. E.g. &#34;Sedan&#34;. |
| code | [string](#string) | required | Category code. E.g. "S". |
| description | [string](#string) | required | Category name. E.g. "Sedan". |



Expand Down
4 changes: 3 additions & 1 deletion examples/doc/example.txt
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ Represents a vehicle model.

|model_code | <<string,string>> |required |The car model code, e.g. "PZ003".

|model_name | <<string,string>> |required |The car model name, e.g. "Z3".
|model_name | <<string,string>> |required |model_name represents the car model name.

This field typically contains the manufacturer name, e.g. "Porsche 911".

|daily_hire_rate_dollars | <<sint32,sint32>> |required |Dollars per day.

Expand Down
6 changes: 5 additions & 1 deletion examples/proto/Vehicle.proto
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ extend Manufacturer {
message Model {
required string id = 1; /** The unique model ID. */
required string model_code = 2; /** The car model code, e.g. "PZ003". */
required string model_name = 3; /** The car model name, e.g. "Z3". */

// model_name represents the car model name.
//
// This field typically contains the manufacturer name, e.g. "Porsche 911".
required string model_name = 3;

required sint32 daily_hire_rate_dollars = 4; /// Dollars per day.
required sint32 daily_hire_rate_cents = 5; /// Cents per day.
Expand Down
14 changes: 13 additions & 1 deletion filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,26 @@ func ParaFilter(content string) string {

// NoBrFilter removes single CR and LF from content.
func NoBrFilter(content string) string {
paragraphs := contentToParagraphs(content)
return strings.Join(paragraphs, "\n\n")
}

// BrFilterMD removes single CR and LF from content and replaces it with unescaped
// HTML line breaks for markdown.
func BrFilterMD(content string) template.HTML {
paragraphs := contentToParagraphs(content)
return template.HTML(strings.Join(paragraphs, "<br><br>"))
}

func contentToParagraphs(content string) []string {
normalized := strings.Replace(content, "\r\n", "\n", -1)
paragraphs := multiNewlinePattern.Split(normalized, -1)
for i, p := range paragraphs {
withoutCR := strings.Replace(p, "\r", " ", -1)
withoutLF := strings.Replace(withoutCR, "\n", " ", -1)
paragraphs[i] = spacePattern.ReplaceAllString(withoutLF, " ")
}
return strings.Join(paragraphs, "\n\n")
return paragraphs
}

// AnchorFilter replaces all special characters with URL friendly dashes
Expand Down
14 changes: 14 additions & 0 deletions filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ func TestNoBrFilter(t *testing.T) {
}
}

func TestBrMdFilter(t *testing.T) {
tests := map[string]string{
"My content": "My content",
"My content \r\nHere.": "My content Here.",
"My\n content\r right\r\n here.": "My content right here.",
"My\ncontent\rright\r\nhere.": "My content right here.",
"My content.\n\nMore content.": "My content.<br><br>More content.",
}

for input, output := range tests {
require.Equal(t, html.HTML(output), BrFilterMD(input))
}
}

func TestAnchorFilter(t *testing.T) {
tests := map[string]string{
"com/example/test.proto": "com_example_test-proto",
Expand Down
1 change: 1 addition & 0 deletions renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ var funcMap = map[string]interface{}{
"p": PFilter,
"para": ParaFilter,
"nobr": NoBrFilter,
"brmd": BrFilterMD,
"anchor": AnchorFilter,
}

Expand Down