Skip to content

Commit 1f4e8ac

Browse files
authored
strip HTML and markdown from form field placeholders (#258)
1 parent dc150dd commit 1f4e8ac

7 files changed

Lines changed: 82 additions & 4 deletions

File tree

demo/examples/petstore.yaml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,9 @@ paths:
300300
schema:
301301
type: string
302302
format: binary
303+
description: |
304+
This description contains <a href="https://www.w3.org/html/">HTML</a><br />
305+
Cat pics plz
303306
/pet/findByStatus:
304307
get:
305308
tags:
@@ -838,13 +841,15 @@ paths:
838841
parameters:
839842
- name: username
840843
in: query
841-
description: The user name for login
844+
description: |
845+
This description contains <a href="https://www.w3.org/html/">HTML</a><br />The user name for login
842846
required: true
843847
schema:
844848
type: string
845849
- name: password
846850
in: query
847-
description: The password for login in clear text
851+
description: |
852+
This description contains [markdown](https://www.markdownguide.org/)<br />The password for login in clear text
848853
required: true
849854
schema:
850855
type: string

packages/docusaurus-theme-openapi/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"docusaurus-plugin-openapi": "^0.7.2",
4646
"immer": "^9.0.7",
4747
"lodash": "^4.17.20",
48+
"marked": "^11.0.0",
4849
"monaco-editor": "^0.31.1",
4950
"postman-code-generators": "^1.0.0",
5051
"postman-collection": "^4.1.0",
@@ -54,6 +55,7 @@
5455
"react-redux": "^7.2.0",
5556
"redux-devtools-extension": "^2.13.8",
5657
"refractor": "^4.8.1",
58+
"striptags": "^3.2.0",
5759
"webpack": "^5.88.1"
5860
},
5961
"peerDependencies": {

packages/docusaurus-theme-openapi/src/theme/ApiDemoPanel/FormFileUpload/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import MagicDropzone from "react-magic-dropzone";
1212

1313
import styles from "./styles.module.css";
1414
import FloatingButton from "../FloatingButton";
15+
import { stripText } from "../text";
1516

1617
type PreviewFile = { preview: string } & File;
1718

@@ -107,7 +108,7 @@ function FormFileUpload({ placeholder, onChange }: Props) {
107108
<RenderPreview file={file} />
108109
</>
109110
) : (
110-
<div className={styles.dropzoneContent}>{placeholder}</div>
111+
<div className={styles.dropzoneContent}>{stripText(placeholder)}</div>
111112
)}
112113
</MagicDropzone>
113114
</FloatingButton>

packages/docusaurus-theme-openapi/src/theme/ApiDemoPanel/FormTextInput/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import React from "react";
99

1010
import styles from "./styles.module.css";
11+
import { stripText } from "../text";
1112

1213
interface Props {
1314
value?: string;
@@ -21,7 +22,7 @@ function FormTextInput({ value, placeholder, password, onChange }: Props) {
2122
<input
2223
className={styles.input}
2324
type={password ? "password" : "text"}
24-
placeholder={placeholder}
25+
placeholder={stripText(placeholder)}
2526
value={value}
2627
onChange={onChange}
2728
autoComplete="off"
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* ============================================================================
2+
* Copyright (c) Cloud Annotations
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
* ========================================================================== */
7+
8+
import { stripText } from "./text";
9+
10+
describe("stripText function", () => {
11+
it("should return empty string when passed undefined", () => {
12+
expect(stripText(undefined)).toBe("");
13+
});
14+
15+
it("should strip markdown", () => {
16+
expect(
17+
stripText(
18+
"**This** description contains [markdown](https://www.markdownguide.org/)"
19+
)
20+
).toBe("This description contains markdown");
21+
});
22+
23+
it("should strip HTML", () => {
24+
expect(
25+
stripText(
26+
'<strong>This</strong> description contains <a href="https://www.w3.org/html/">HTML</a>'
27+
)
28+
).toBe("This description contains HTML");
29+
});
30+
31+
it("should replace newlines with space", () => {
32+
expect(stripText("one\ntwo\n\nthree")).toBe("one two three");
33+
});
34+
35+
it("should insert whitespace between HTML elements", () => {
36+
expect(
37+
stripText("<div><div>one</div></div><p>two</p><p>three</p><br />four")
38+
).toBe("one two three four");
39+
});
40+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* ============================================================================
2+
* Copyright (c) Cloud Annotations
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
* ========================================================================== */
7+
8+
import { marked } from "marked";
9+
import striptags from "striptags";
10+
11+
export function stripText(text?: string): string {
12+
if (text === undefined) {
13+
return "";
14+
}
15+
const renderer = new marked.TextRenderer();
16+
marked.use({ silent: true, renderer });
17+
const parsedMarkdown = marked.parse(text, { async: false }) as string;
18+
return striptags(parsedMarkdown, [], " ").replace(/\s+/g, " ").trim();
19+
}

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10517,6 +10517,11 @@ marked@2.0.1:
1051710517
resolved "https://registry.yarnpkg.com/marked/-/marked-2.0.1.tgz#5e7ed7009bfa5c95182e4eb696f85e948cefcee3"
1051810518
integrity sha512-5+/fKgMv2hARmMW7DOpykr2iLhl0NgjyELk5yn92iE7z8Se1IS9n3UsFm86hFXIkvMBmVxki8+ckcpjBeyo/hw==
1051910519

10520+
marked@^11.0.0:
10521+
version "11.1.0"
10522+
resolved "https://registry.yarnpkg.com/marked/-/marked-11.1.0.tgz#f2d12323e80ba8a97cc8262fe7e94fcc007476ab"
10523+
integrity sha512-fvKJWAPEafVj1dwGwcPI5mBB/0pvViL6NlCbNDG1HOIRwwAU/jeMoFxfbRLuirO1wRH7m4yPvBqD/O1wyWvayw==
10524+
1052010525
mdast-util-directive@^3.0.0:
1052110526
version "3.0.0"
1052210527
resolved "https://registry.yarnpkg.com/mdast-util-directive/-/mdast-util-directive-3.0.0.tgz#3fb1764e705bbdf0afb0d3f889e4404c3e82561f"
@@ -14802,6 +14807,11 @@ strip-json-comments@~2.0.1:
1480214807
resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-2.0.1.tgz#3c531942e908c2697c0ec344858c286c7ca0a60a"
1480314808
integrity sha512-4gB8na07fecVVkOI6Rs4e7T6NOTki5EmL7TUduTs6bu3EdnSycntVJ4re8kgZA+wx9IueI2Y11bfbgwtzuE0KQ==
1480414809

14810+
striptags@^3.2.0:
14811+
version "3.2.0"
14812+
resolved "https://registry.yarnpkg.com/striptags/-/striptags-3.2.0.tgz#cc74a137db2de8b0b9a370006334161f7dd67052"
14813+
integrity sha512-g45ZOGzHDMe2bdYMdIvdAfCQkCTDMGBazSw1ypMowwGIee7ZQ5dU0rBJ8Jqgl+jAKIv4dbeE1jscZq9wid1Tkw==
14814+
1480514815
strong-log-transformer@^2.1.0:
1480614816
version "2.1.0"
1480714817
resolved "https://registry.yarnpkg.com/strong-log-transformer/-/strong-log-transformer-2.1.0.tgz#0f5ed78d325e0421ac6f90f7f10e691d6ae3ae10"

0 commit comments

Comments
 (0)