Skip to content

Commit 037a4f4

Browse files
authored
[Bug] Improve support for multiline descriptions in frontmatter (#325)
* Splitlines before assigning frontmatter description * Switch to frontmatter description
1 parent 4191bca commit 037a4f4

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

packages/docusaurus-plugin-openapi-docs/src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ export default function pluginOpenAPIDocs(
159159
: `---
160160
id: {{{id}}}
161161
title: "{{{title}}}"
162-
description: "{{{description}}}"
162+
description: "{{{frontMatter.description}}}"
163163
{{^api}}
164164
sidebar_label: Introduction
165165
{{/api}}
@@ -190,7 +190,7 @@ info_path: {{{infoPath}}}
190190
const infoMdTemplate = `---
191191
id: {{{id}}}
192192
title: "{{{title}}}"
193-
description: "{{{description}}}"
193+
description: "{{{frontMatter.description}}}"
194194
sidebar_label: {{{title}}}
195195
hide_title: true
196196
custom_edit_url: null
@@ -208,8 +208,8 @@ import {useCurrentSidebarCategory} from '@docusaurus/theme-common';
208208

209209
const tagMdTemplate = `---
210210
id: {{{id}}}
211-
title: "{{{description}}}"
212-
description: "{{{description}}}"
211+
title: "{{{frontMatter.description}}}"
212+
description: "{{{frontMatter.description}}}"
213213
custom_edit_url: null
214214
---
215215

packages/docusaurus-plugin-openapi-docs/src/openapi/openapi.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,20 @@ function createItems(
9898
openapiData.tags ?? []
9999
);
100100
const tagId = kebabCase(tag.name);
101+
const splitDescription = description.match(/[^\r\n]+/g);
101102
const tagPage: PartialPage<TagPageMetadata> = {
102103
type: "tag",
103104
id: tagId,
104105
unversionedId: tagId,
105106
title: description ?? "",
106107
description: description ?? "",
107-
frontMatter: {},
108+
frontMatter: {
109+
description: splitDescription
110+
? splitDescription[0]
111+
.replace(/((?:^|[^\\])(?:\\{2})*)"/g, "$1'")
112+
.replace(/\s+$/, "")
113+
: "",
114+
},
108115
tag: {
109116
...tag,
110117
},
@@ -115,6 +122,11 @@ function createItems(
115122

116123
if (openapiData.info.description) {
117124
// Only create an info page if we have a description.
125+
const infoDescription = openapiData.info?.description;
126+
let splitDescription: any;
127+
if (infoDescription) {
128+
splitDescription = infoDescription.match(/[^\r\n]+/g);
129+
}
118130
const infoPage: PartialPage<InfoPageMetadata> = {
119131
type: "info",
120132
id: infoId,
@@ -126,7 +138,13 @@ function createItems(
126138
"$1'"
127139
)
128140
: "",
129-
frontMatter: {},
141+
frontMatter: {
142+
description: splitDescription
143+
? splitDescription[0]
144+
.replace(/((?:^|[^\\])(?:\\{2})*)"/g, "$1'")
145+
.replace(/\s+$/, "")
146+
: "",
147+
},
130148
securitySchemes: openapiData.components?.securitySchemes,
131149
info: {
132150
...openapiData.info,
@@ -208,6 +226,12 @@ function createItems(
208226
}
209227
}
210228

229+
const opDescription = operationObject.description;
230+
let splitDescription: any;
231+
if (opDescription) {
232+
splitDescription = opDescription.match(/[^\r\n]+/g);
233+
}
234+
211235
const apiPage: PartialPage<ApiPageMetadata> = {
212236
type: "api",
213237
id: baseId,
@@ -220,7 +244,13 @@ function createItems(
220244
"$1'"
221245
)
222246
: "",
223-
frontMatter: {},
247+
frontMatter: {
248+
description: splitDescription
249+
? splitDescription[0]
250+
.replace(/((?:^|[^\\])(?:\\{2})*)"/g, "$1'")
251+
.replace(/\s+$/, "")
252+
: "",
253+
},
224254
api: {
225255
...defaults,
226256
tags: operationObject.tags,

0 commit comments

Comments
 (0)