pulumi/pkg/codegen/docs
Justin Van Patten 7162491d0b
[docsgen] Fix rendering of deprecated messages and text in description lists (#13773)
A change was made a while back to remove the use of shortcodes. As part
of that, the shortcode used to render markdown was replaced by a
function call that used goldmark to render markdown. The resulting HTML
from that wraps the text in paragraph tags.

This causes some problems for rendering the docs. For deprecations, the
deprecation message now shows up outside of the "Deprecated" box. This
happens because the rendered markdown was being wrapped in `<p></p>`
tags, leading to `<p class="resource-deprecated">Deprecated:
<p>message</p></p>`, which does not render correctly because paragraph
tags cannot be nested.

Also, in description lists, rendered markdown text was being wrapped in
paragraph tags (e.g. `<dd><p>text</p></dd>`), causing it to render
differently from text not wrapped in paragraph tags (e.g.
`<dd>text</dd>`).

This change addresses these issues.

- First, `<div class="resource-deprecated">` is used rather than `<p>`
to contain the deprecation information.

- Second, the `markdownify` function will now trim unnecessary paragraph
tags.

Fixes https://github.com/pulumi/pulumi-hugo/issues/2832
Fixes https://github.com/pulumi/registry/issues/3008

---

## Before

<img width="713" alt="Screen Shot 2023-08-24 at 5 21 44 PM"
src="https://github.com/pulumi/pulumi/assets/710598/1358cc74-dd8a-4bc0-bed8-603b1439b2aa">

## After

<img width="721" alt="Screen Shot 2023-08-24 at 5 21 58 PM"
src="https://github.com/pulumi/pulumi/assets/710598/ad501e68-1bac-486c-a723-7db2b3442c8a">

## Before

<img width="719" alt="Screen Shot 2023-08-24 at 5 22 28 PM"
src="https://github.com/pulumi/pulumi/assets/710598/6ca40b85-9008-429b-83c3-2f0d681752f3">

## After

<img width="714" alt="Screen Shot 2023-08-24 at 5 24 02 PM"
src="https://github.com/pulumi/pulumi/assets/710598/eec1dacf-8c24-470e-b1e3-da843b4aeaee">
2023-08-26 17:36:28 +00:00
..
templates [docsgen] Fix rendering of deprecated messages and text in description lists (#13773) 2023-08-26 17:36:28 +00:00
README.md Replace Hugo shortcodes (#9491) 2022-04-29 15:04:15 -07:00
examples.go all: Assert => Assertf 2023-03-03 14:37:43 -08:00
gen.go [docsgen] Fix rendering of deprecated messages and text in description lists (#13773) 2023-08-26 17:36:28 +00:00
gen_function.go Use slice.Prealloc instead of make([]T, 0, ...) 2023-06-29 11:27:50 +01:00
gen_kubernetes.go sdk/go: Remove 'nolint' directives from package docs 2023-01-06 09:06:47 -08:00
gen_method.go Use slice.Prealloc instead of make([]T, 0, ...) 2023-06-29 11:27:50 +01:00
gen_test.go all: Reformat with gofumpt 2023-03-03 09:00:24 -08:00
package_tree.go Use slice.Prealloc instead of make([]T, 0, ...) 2023-06-29 11:27:50 +01:00
package_tree_test.go pkg/codegen/docs: Fix data race in tests 2023-01-07 10:01:32 -08:00
utils.go Use slice.Prealloc instead of make([]T, 0, ...) 2023-06-29 11:27:50 +01:00
utils_test.go sdk/go: Remove 'nolint' directives from package docs 2023-01-06 09:06:47 -08:00

README.md

Docs generator

This generator generates resource-level docs by utilizing the Pulumi schema.

Crash course on templates

The templates use Go's built-in html/template package to process templates with data. The driver for this doc generator (e.g. tfbridge for TF-based providers) then persists each file from memory onto the disk as .md files.

Although we are using the html/template package, it has the same exact interface as the text/template package, except for some HTML specific things. Therefore, all of the functions available in the text/template package are also available with the html/template package.

  • Data can be injected using {{.PropertyName}}.
  • Nested properties can be accessed using the dot notation, i.e. {{.Property1.Property2}}.
  • Templates can inject other templates using the {{template "template_name"}} directive.
    • For this to work, you will need to first define the named template using {{define "template_name"}}.
  • You can pass data to nested templates by simply passing an argument after the template's name.
  • To remove whitespace from injected values, use the - in the template tags.
    • For example, {{if .SomeBool}} some text {{- else}} some other text {{- end}}. Note the use of - to eliminate whitespace from the enclosing text.
    • Read more here.
  • To render un-encoded content use the custom global function htmlSafe.
    • Note: This should only be used if you know for sure you are not injecting any user-generated content, as it by-passes the HTML encoding.
  • To render strings to Markdown, use the custom global function markdownify.
  • To print regular strings, that share the same syntax as the Go templating engine, use the built-in global function print function.

Learn more from here: https://curtisvermeeren.github.io/2017/09/14/Golang-Templates-Cheatsheet

Modifying templates and updating tests

We run tests that validate our template-rendering output. If you need to make change that produces a set of Markdown files that differs from the set that we use in our tests (see codegen/testing/test/testdata/**/*.md), your pull-request checks will fail, and to get them to pass, you'll need to modify the test data to match the output produced by your change.

For minor diffs, you can just update the test files manually and include those updates with your PR. But for large diffs, you may want to regenerate the full set. To do that, from the root of the repo, run:

cd pkg/codegen/docs && PULUMI_ACCEPT=true go test . && cd -