Skip to content

Commit f35d68d

Browse files
committed
fix: convert HTML to Markdown in CommentInputField using turndown
1 parent 54341ab commit f35d68d

4 files changed

Lines changed: 37 additions & 2 deletions

File tree

frontend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
"tachyons": "^4.12.0",
7171
"terra-draw": "^1.18.1",
7272
"tributejs": "^5.1.3",
73+
"turndown": "^7.2.4",
7374
"use-query-params": "^2.2.1",
7475
"webfontloader": "^1.6.28",
7576
"workbox-core": "^7.0.0",

frontend/src/components/comments/commentInput.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import HashtagPaste from './hashtagPaste';
1313
import FileRejections from './fileRejections';
1414
import DropzoneUploadStatus from './uploadStatus';
1515
import { DROPZONE_SETTINGS } from '../../config';
16-
import { formatUserNamesToLink } from '../../utils/htmlFromMarkdown';
16+
import { formatUserNamesToLink, markdownFromHtml } from '../../utils/htmlFromMarkdown';
1717
import { iconConfig } from './editorIconConfig';
1818
import messages from './messages';
1919
import { CurrentUserAvatar } from '../user/avatar';
@@ -37,6 +37,7 @@ function CommentInputField({
3737
const textareaRef = useRef();
3838
const fileInputRef = useRef(null);
3939
const isBundle = useRef(false);
40+
const lastConvertedRef = useRef(null);
4041
const [isShowPreview, setIsShowPreview] = useState(false);
4142

4243
const appendImgToComment = (url) => setComment(`${comment}\n![image](${url})\n`);
@@ -149,7 +150,21 @@ function CommentInputField({
149150
sessionStorage.setItem(sessionkey, comment);
150151
}, [comment, sessionkey]);
151152

152-
console.log(comment, 'comment');
153+
useEffect(() => {
154+
if (
155+
comment &&
156+
comment !== lastConvertedRef.current &&
157+
/<\/?(?:p|div|h[1-6]|ul|ol|li|br|strong|em|a|table|thead|tbody|tr|th|td|blockquote|pre|code|span)[\s>]/i.test(comment) &&
158+
document.activeElement !== textareaRef.current?.textarea
159+
) {
160+
const converted = markdownFromHtml(comment);
161+
if (converted !== comment) {
162+
lastConvertedRef.current = converted;
163+
setComment(converted);
164+
}
165+
}
166+
// eslint-disable-next-line react-hooks/exhaustive-deps
167+
}, [comment]);
153168

154169
return (
155170
<div {...getRootProps()}>

frontend/src/utils/htmlFromMarkdown.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { marked } from 'marked';
22
import DOMPurify from 'dompurify';
3+
import TurndownService from 'turndown';
34

45
const VIDEO_TAG_REGEXP = new RegExp(/^::youtube\[(.*)\]$/);
56

@@ -102,3 +103,9 @@ export const formatUserNamesToLink = (text) => {
102103
}
103104
return text;
104105
};
106+
107+
const turndownService = new TurndownService();
108+
109+
export const markdownFromHtml = (html) => {
110+
return turndownService.turndown(html);
111+
};

frontend/yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2139,6 +2139,11 @@
21392139
rw "^1.3.3"
21402140
tinyqueue "^3.0.0"
21412141

2142+
"@mixmark-io/domino@^2.2.0":
2143+
version "2.2.0"
2144+
resolved "https://registry.yarnpkg.com/@mixmark-io/domino/-/domino-2.2.0.tgz#4e8ec69bf1afeb7a14f0628b7e2c0f35bdb336c3"
2145+
integrity sha512-Y28PR25bHXUg88kCV7nivXrP2Nj2RueZ3/l/jdx6J9f8J4nsEGcgX0Qe6lt7Pa+J79+kPiJU3LguR6O/6zrLOw==
2146+
21422147
"@mswjs/cookies@^0.2.2":
21432148
version "0.2.2"
21442149
resolved "https://registry.yarnpkg.com/@mswjs/cookies/-/cookies-0.2.2.tgz#b4e207bf6989e5d5427539c2443380a33ebb922b"
@@ -14102,6 +14107,13 @@ tsutils@^3.21.0:
1410214107
dependencies:
1410314108
tslib "^1.8.1"
1410414109

14110+
turndown@^7.2.4:
14111+
version "7.2.4"
14112+
resolved "https://registry.yarnpkg.com/turndown/-/turndown-7.2.4.tgz#42d98202aefa8c188c997b586bc6da78bdf27ea2"
14113+
integrity sha512-I8yFsfRzmzK0WV1pNNOA4A7y4RDfFxPRxb3t+e3ui14qSGOxGtiSP6GjeX+Y6CHb7HYaFj7ECUD7VE5kQMZWGQ==
14114+
dependencies:
14115+
"@mixmark-io/domino" "^2.2.0"
14116+
1410514117
type-check@^0.4.0, type-check@~0.4.0:
1410614118
version "0.4.0"
1410714119
resolved "https://registry.yarnpkg.com/type-check/-/type-check-0.4.0.tgz#07b8203bfa7056c0657050e3ccd2c37730bab8f1"

0 commit comments

Comments
 (0)