From 4f0adc46915ad8517bc4c1c347a5b7c86519847d Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Mon, 1 Jul 2024 11:39:08 +0100 Subject: [PATCH] Emit well-typed MDX in protocol documentation The generated protocol documentation has a few issues: 1. It creates its own anchors for linking things like request, response, and scalar types. However, docusaurus's link checker doesn't know about these generated anchors and emits false positives. Instead, create React components to generate the titles and anchors, so that we can tell docusaurus about these anchors. 2. The links to scalar types assume that we know about all of them. The google.protobuf.* types are not documented by us. Create a TypeLink component that understands this difference and links to the protobuf docs for things like Timestamp. --- docs/proto_template.tmpl | 62 +++++++++++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 14 deletions(-) diff --git a/docs/proto_template.tmpl b/docs/proto_template.tmpl index b93b521712..02be96d64b 100644 --- a/docs/proto_template.tmpl +++ b/docs/proto_template.tmpl @@ -2,23 +2,57 @@ sidebar_position: 60 toc_max_heading_level: 4 --- + +import useBrokenLinks from '@docusaurus/useBrokenLinks'; + +export const File = ({ children, id }) => { + useBrokenLinks().collectAnchor(id); + return

{children}

; +} +export const Service = ({ children, id }) => { + useBrokenLinks().collectAnchor(id); + return

{children}

; +} +export const Message = ({ children, id }) => { + useBrokenLinks().collectAnchor(id); + return

{children}

; +} +export const Extension = ({ children, id }) => { + useBrokenLinks().collectAnchor(id); + return

{children}

; +} +export const Enum = ({ children, id }) => { + useBrokenLinks().collectAnchor(id); + return

{children}

; +} +export const ProtoType = ({ children, id }) => { + useBrokenLinks().collectAnchor(id); + return {children}; +} +export const TypeLink = ({ children, type }) => { + let link = type.startsWith('google-protobuf-') ? + `https://protobuf.dev/reference/protobuf/google.protobuf/#${type.replace('google-protobuf-', '')}` : + `#${type}`; + return {children}; +} + + # Protocol documentation - + {{range .Files}} {{$file_name := .Name}} - -

Top

-## {{.Name}} +{{.Name}} + {{.Description}} {{- if .Services}} ### Services {{range .Services}} - -#### {{.Name}} +{{.Name}} + {{.Description}} | Method Name | Request Type | Response Type | Description | @@ -32,16 +66,16 @@ toc_max_heading_level: 4 {{- if .Messages}} ### Messages {{range .Messages}} - -#### {{.LongName}} +{{.LongName}} + {{.Description}} {{if .HasFields}} | Field | Type | Label | Description | | ----- | ---- | ----- | ----------- | {{range .Fields -}} - | {{.Name}} | [{{.LongType}}](#{{.FullType | anchor}}) | {{.Label}} | {{if (index .Options "deprecated"|default false)}}**Deprecated.** {{end}}{{nobr .Description}}{{if .DefaultValue}} Default: {{.DefaultValue}}{{end}} | + | {{.Name}} | {{.LongType}} | {{.Label}} | {{if (index .Options "deprecated"|default false)}}**Deprecated.** {{end}}{{nobr .Description}}{{if .DefaultValue}} Default: {{.DefaultValue}}{{end}} | {{end}} {{end}} {{- end -}} @@ -56,9 +90,9 @@ toc_max_heading_level: 4 {{end}} {{range .Enums}} - -### {{.LongName}} +{{.LongName}} + {{.Description}} | Name | Number | Description | @@ -69,9 +103,9 @@ toc_max_heading_level: 4 {{end}} {{if .HasExtensions}} - -### File-level Extensions +File-level Extensions + | Extension | Type | Base | Number | Description | | --------- | ---- | ---- | ------ | ----------- | {{range .Extensions -}} @@ -86,5 +120,5 @@ toc_max_heading_level: 4 | .proto Type | Notes | C++ | Java | Python | Go | C# | PHP | Ruby | | ----------- | ----- | --- | ---- | ------ | -- | -- | --- | ---- | {{range .Scalars -}} - | {{.ProtoType}} | {{.Notes}} | {{.CppType}} | {{.JavaType}} | {{.PythonType}} | {{.GoType}} | {{.CSharp}} | {{.PhpType}} | {{.RubyType}} | + | {{.ProtoType}} | {{.Notes}} | {{.CppType}} | {{.JavaType}} | {{.PythonType}} | {{.GoType}} | {{.CSharp}} | {{.PhpType}} | {{.RubyType}} | {{end}}