-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add table renderer to render tables as Markdown. Add tests. (#2603)
The markdown renderer we use does [not support rendering out tables](teekennedy/goldmark-markdown#19). This pull request adds a table renderer, since the raw HTML rendering is lossy and does not handle inline code blocks well. With this change, a table will properly render out inline code blocks inside table cells, and correctly handle tables as separate elements. Example from NewRelic's `docs/_index.md` page with this change applied: <img width="898" alt="Screenshot 2024-11-07 at 5 07 42 PM" src="https://github.com/user-attachments/assets/01392204-814e-4469-ac94-c0c03832a9c6">
- Loading branch information
1 parent
52a2e41
commit af9ccb5
Showing
10 changed files
with
268 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Copyright 2016-2024, Pulumi Corporation. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package parse | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/hexops/autogold/v2" | ||
"github.com/stretchr/testify/require" | ||
"github.com/yuin/goldmark" | ||
) | ||
|
||
func TestRenderTable(t *testing.T) { | ||
t.Parallel() | ||
if runtime.GOOS == "windows" { | ||
t.Skipf("Skipping on windows to avoid failing on incorrect newline handling") | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
input string | ||
expected autogold.Value | ||
}{ | ||
{ | ||
name: "basic", | ||
input: `# hi | ||
| t1 | t2 | | ||
|---|---| | ||
| r1c1 | r1c2 | | ||
| r2c1 | r2c2 | | ||
`, | ||
expected: autogold.Expect(`# hi | ||
| t1 | t2 | | ||
|------|------| | ||
| r1c1 | r1c2 | | ||
| r2c1 | r2c2 | | ||
`), | ||
}, | ||
{ | ||
name: "with-in table effects", | ||
input: ` | ||
| t1 | *t2* | | ||
|------|------| | ||
| __r1c1__ | r1c2 | | ||
| r2c1 | r2c2 | | ||
`, | ||
expected: autogold.Expect(` | ||
| t1 | *t2* | | ||
|----------|------| | ||
| **r1c1** | r1c2 | | ||
| r2c1 | r2c2 | | ||
`), | ||
}, | ||
{ | ||
name: "with-codeblock-after-text", | ||
input: "| t1 | *t2* |\n" + | ||
"|------|-------------------|\n" + | ||
"| r2c1 | r1c2 `code_block` |\n", | ||
expected: autogold.Expect("\n" + | ||
"| t1 | *t2* |\n" + | ||
"|------|-------------------|\n" + | ||
"| r2c1 | r1c2 `code_block` |\n"), | ||
}, | ||
{ | ||
name: "from-newrelic", | ||
input: readfile(t, "../test_data/table-rendering/input.md"), | ||
expected: autogold.Expect(readfile(t, "../test_data/table-rendering/expected.md")), | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
var out bytes.Buffer | ||
err := goldmark.New( | ||
goldmark.WithExtensions(TFRegistryExtension), | ||
).Convert([]byte(tt.input), &out) | ||
require.NoError(t, err) | ||
tt.expected.Equal(t, out.String()) | ||
}) | ||
} | ||
} | ||
|
||
func readfile(t *testing.T, file string) string { | ||
t.Helper() | ||
bytes, err := os.ReadFile(file) | ||
require.NoError(t, err) | ||
return string(bytes) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
| Argument | Required? | Description | | ||
|------------------------|-----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ||
| `account_id` | Required | Your New Relic account ID. The `NEW_RELIC_ACCOUNT_ID` environment variable can also be used. | | ||
| `api_key` | Required | Your New Relic Personal API key (usually prefixed with `NRAK`). The `NEW_RELIC_API_KEY` environment variable can also be used. | | ||
| `region` | Optional | The region for the data center for which your New Relic account is configured. The `NEW_RELIC_REGION` environment variable can also be used. Valid values are `US` or `EU`. Default value is `US`. | | ||
| `insecure_skip_verify` | Optional | Trust self-signed SSL certificates. If omitted, the `NEW_RELIC_API_SKIP_VERIFY` environment variable is used. | | ||
| `insights_insert_key` | Optional | Your Insights insert key used when inserting Insights events via the `newrelic_insights_event` resource. Can also use `NEW_RELIC_INSIGHTS_INSERT_KEY` environment variable. | | ||
| `cacert_file` | Optional | A path to a PEM-encoded certificate authority used to verify the remote agent's certificate. The `NEW_RELIC_API_CACERT` environment variable can also be used. | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
| Argument | Required? | Description | | ||
| ---------------------- | --------- |----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ||
| `account_id` | Required | Your New Relic account ID. The `NEW_RELIC_ACCOUNT_ID` environment variable can also be used. | | ||
| `api_key` | Required | Your New Relic Personal API key (usually prefixed with `NRAK`). The `NEW_RELIC_API_KEY` environment variable can also be used. | | ||
| `region` | Optional | The region for the data center for which your New Relic account is configured. The `NEW_RELIC_REGION` environment variable can also be used. Valid values are `US` or `EU`. Default value is `US`. | | ||
| `insecure_skip_verify` | Optional | Trust self-signed SSL certificates. If omitted, the `NEW_RELIC_API_SKIP_VERIFY` environment variable is used. | | ||
| `insights_insert_key` | Optional | Your Insights insert key used when inserting Insights events via the `newrelic_insights_event` resource. Can also use `NEW_RELIC_INSIGHTS_INSERT_KEY` environment variable. | | ||
| `cacert_file` | Optional | A path to a PEM-encoded certificate authority used to verify the remote agent's certificate. The `NEW_RELIC_API_CACERT` environment variable can also be used. | |