fix: Handle emoji field in imported documents

closes #5810
This commit is contained in:
Tom Moor
2023-09-13 22:05:21 -04:00
parent ef22a5dc52
commit 7e17e82ac8
5 changed files with 29 additions and 25 deletions

View File

@@ -1,5 +1,6 @@
import path from "path";
import emojiRegex from "emoji-regex";
import escapeRegExp from "lodash/escapeRegExp";
import truncate from "lodash/truncate";
import mammoth from "mammoth";
import quotedPrintable from "quoted-printable";
@@ -131,6 +132,15 @@ async function confluenceToMarkdown(value: Buffer | string): Promise<string> {
return html.replace(/<br>/g, " \\n ");
}
type Props = {
user: User;
mimeType: string;
fileName: string;
content: Buffer | string;
ip?: string;
transaction?: Transaction;
};
async function documentImporter({
mimeType,
fileName,
@@ -138,14 +148,8 @@ async function documentImporter({
user,
ip,
transaction,
}: {
user: User;
mimeType: string;
fileName: string;
content: Buffer | string;
ip?: string;
transaction?: Transaction;
}): Promise<{
}: Props): Promise<{
emoji?: string;
text: string;
title: string;
state: Buffer;
@@ -177,27 +181,22 @@ async function documentImporter({
let text = await fileInfo.getMarkdown(content);
text = text.trim();
// find and extract first emoji, in the case of some imports it can be outside
// of the title, at the top of the document.
// find and extract emoji near the beginning of the document.
const regex = emojiRegex();
const matches = regex.exec(text);
const firstEmoji = matches ? matches[0] : undefined;
const textStartsWithEmoji = firstEmoji && text.startsWith(firstEmoji);
if (textStartsWithEmoji) {
text = text.replace(firstEmoji, "").trim();
const matches = regex.exec(text.slice(0, 10));
const emoji = matches ? matches[0] : undefined;
if (emoji) {
text = text.replace(emoji, "");
}
// If the first line of the imported text looks like a markdown heading
// then we can use this as the document title
// then we can use this as the document title rather than the file name.
if (text.startsWith("# ")) {
const result = parseTitle(text);
title = result.title;
text = text.replace(`# ${title}`, "").trimStart();
}
// If we parsed an emoji from _above_ the title then add it back at prefixing
if (textStartsWithEmoji) {
title = `${firstEmoji} ${title}`;
text = text
.replace(new RegExp(`#\\s+${escapeRegExp(title)}`), "")
.trimStart();
}
// Replace any <br> generated by the turndown plugin with escaped newlines
@@ -227,6 +226,7 @@ async function documentImporter({
text,
state,
title,
emoji,
};
}