The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
import { formatRecordReference } from '@/ai/utils/format-record-reference.util';
import { formatSkillReference } from '@/ai/utils/format-skill-reference.util';
import { isDefined } from '@/utils/validation';
import { parseTipTapJsonDocument } from './parse-tiptap-json-document';
import { type TipTapDocument } from './tiptap-document';
import { type TipTapMark, TIPTAP_MARK_TYPES } from './tiptap-mark-types';
import { TIPTAP_MARKS_RENDER_ORDER } from './tiptap-marks-render-order';
import { type TipTapNode } from './tiptap-node';
import { TIPTAP_NODE_TYPES } from './tiptap-node-types';
const renderChildren = (node: TipTapNode): string =>
(node.content ?? []).map(renderTipTapNodeToMarkdown).join('');
const escapeMarkdownText = (text: string): string =>
text
.replace(/([\\`*_[\]{}<>~|#&])/g, '\\$1')
.replace(/^(\s{0,3})>(?=\s|$)/gm, '$1\\>')
.replace(/^(\s{0,3})-(?=-{2}|\s|$)/gm, '$1\\-')
.replace(/^(\s{0,3})\+(?=\s|$)/gm, '$1\\+')
.replace(/^(\s{0,3}\d+)([.)])(?=\s|$)/gm, '$1\\$2');
const MARKDOWN_DESTINATION_CHARACTER_ESCAPES: Readonly<Record<string, string>> =
{
'\\': '%5C',
'(': '%28',
')': '%29',
'<': '%3C',
'>': '%3E',
};
const escapeMarkdownDestination = (destination: string): string =>
destination.replace(/[\\()<>\s]/g, (character) =>
character in MARKDOWN_DESTINATION_CHARACTER_ESCAPES
? (MARKDOWN_DESTINATION_CHARACTER_ESCAPES[character] ?? character)
: encodeURIComponent(character),
);
const applyMark = (text: string, mark: TipTapMark): string => {
switch (mark.type) {
case TIPTAP_MARK_TYPES.BOLD:
return `**${text}**`;
case TIPTAP_MARK_TYPES.ITALIC:
return `_${text}_`;
case TIPTAP_MARK_TYPES.UNDERLINE:
return `<u>${text}</u>`;
case TIPTAP_MARK_TYPES.STRIKE:
return `~~${text}~~`;
case TIPTAP_MARK_TYPES.LINK: {
const href = mark.attrs?.href;
return typeof href === 'string'
? `[${text}](${escapeMarkdownDestination(href)})`
: text;
}
default:
return text;
}
};
const buildBacktickFence = ({
code,
minimumLength,
}: {
code: string;
minimumLength: number;
}): string =>
'`'.repeat(
Math.max(
minimumLength,
...(code.match(/`+/g) ?? []).map((backticks) => backticks.length + 1),
),
);
const renderInlineCode = (code: string): string => {
const fence = buildBacktickFence({ code, minimumLength: 1 });
const padding = code.startsWith('`') || code.endsWith('`') ? ' ' : '';
return `${fence}${padding}${code}${padding}${fence}`;
};
const renderCodeBlock = (node: TipTapNode): string => {
const code = (node.content ?? []).map((child) => child.text ?? '').join('');
const fence = buildBacktickFence({ code, minimumLength: 3 });
const language =
typeof node.attrs?.language === 'string' ? node.attrs.language : '';
return `${fence}${language}\n${code}\n${fence}\n\n`;
};
const renderTableRow = (row: TipTapNode): string =>
`| ${(row.content ?? [])
.map((cell) => renderChildren(cell).trim().replace(/\n+/g, ' '))
.join(' | ')} |`;
const renderTable = (node: TipTapNode): string => {
const [headerRow, ...bodyRows] = node.content ?? [];
if (!isDefined(headerRow)) {
return '';
}
const separatorRow = `| ${(headerRow.content ?? [])
.map(() => '---')
.join(' | ')} |`;
return `${[renderTableRow(headerRow), separatorRow, ...bodyRows.map(renderTableRow)].join('\n')}\n\n`;
};
const renderMention = (node: TipTapNode): string => {
const objectName = node.attrs?.objectNameSingular;
const recordId = node.attrs?.recordId;
const label = node.attrs?.label;
if (typeof objectName === 'string' && typeof recordId === 'string') {
return formatRecordReference({
objectNameSingular: objectName,
recordId,
displayName: typeof label === 'string' ? label : '',
});
}
if (typeof label === 'string') {
return `@${label}`;
}
return '';
};
const renderSkillTag = (node: TipTapNode): string => {
const skillId = node.attrs?.skillId;
const label = node.attrs?.label;
if (typeof skillId === 'string' && typeof label === 'string') {
return formatSkillReference({ skillId, label });
}
return typeof label === 'string' ? label : '';
};
const renderListItem = (
item: TipTapNode,
marker: string,
checkbox = '',
): string => {
const [firstLine = '', ...continuationLines] = renderChildren(item)
.trim()
.split('\n');
const indentation = ' '.repeat(marker.length);
const indentedContinuation = continuationLines
.map((line) => (line === '' ? '' : `${indentation}${line}`))
.join('\n');
return `${marker}${checkbox}${firstLine}${
indentedContinuation === '' ? '' : `\n${indentedContinuation}`
}\n`;
};
const renderTipTapNodeToMarkdown = (node: TipTapNode): string => {
switch (node.type) {
case TIPTAP_NODE_TYPES.TEXT:
if (
(node.marks ?? []).some((mark) => mark.type === TIPTAP_MARK_TYPES.CODE)
) {
return renderInlineCode(node.text ?? '');
}
return [...(node.marks ?? [])]
.sort(
(firstMark, secondMark) =>
TIPTAP_MARKS_RENDER_ORDER.indexOf(firstMark.type) -
TIPTAP_MARKS_RENDER_ORDER.indexOf(secondMark.type),
)
.reduce(
(text, mark) => applyMark(text, mark),
escapeMarkdownText(node.text ?? ''),
);
case TIPTAP_NODE_TYPES.HARD_BREAK:
return '\n';
case TIPTAP_NODE_TYPES.VARIABLE_TAG:
return typeof node.attrs?.variable === 'string'
? node.attrs.variable
: '';
case TIPTAP_NODE_TYPES.MENTION_TAG:
return renderMention(node);
case TIPTAP_NODE_TYPES.SKILL_TAG:
return renderSkillTag(node);
case TIPTAP_NODE_TYPES.HEADING: {
const level =
typeof node.attrs?.level === 'number' ? node.attrs.level : 1;
return `${'#'.repeat(Math.min(Math.max(level, 1), 6))} ${renderChildren(node)}\n\n`;
}
case TIPTAP_NODE_TYPES.PARAGRAPH:
return `${renderChildren(node)}\n\n`;
case TIPTAP_NODE_TYPES.BULLET_LIST:
return `${(node.content ?? [])
.map((item) => renderListItem(item, '- '))
.join('')}\n`;
case TIPTAP_NODE_TYPES.TASK_LIST:
return `${(node.content ?? [])
.map((item) =>
renderListItem(
item,
'- ',
item.attrs?.checked === true ? '[x] ' : '[ ] ',
),
)
.join('')}\n`;
case TIPTAP_NODE_TYPES.ORDERED_LIST: {
const start =
typeof node.attrs?.start === 'number' ? node.attrs.start : 1;
return `${(node.content ?? [])
.map((item, index) => renderListItem(item, `${start + index}. `))
.join('')}\n`;
}
case TIPTAP_NODE_TYPES.BLOCKQUOTE:
return `${renderChildren(node)
.trim()
.split('\n')
.map((line) => (line === '' ? '>' : `> ${line}`))
.join('\n')}\n\n`;
case TIPTAP_NODE_TYPES.IMAGE: {
const alt = typeof node.attrs?.alt === 'string' ? node.attrs.alt : '';
const src = typeof node.attrs?.src === 'string' ? node.attrs.src : '';
return src === ''
? escapeMarkdownText(alt)
: `})\n\n`;
}
case TIPTAP_NODE_TYPES.BUTTON: {
const label = renderChildren(node);
const href = node.attrs?.href;
return `${
typeof href === 'string'
? `[${label}](${escapeMarkdownDestination(href)})`
: label
}\n\n`;
}
case TIPTAP_NODE_TYPES.HTML:
return typeof node.attrs?.html === 'string' ? node.attrs.html : '';
case TIPTAP_NODE_TYPES.DIVIDER:
return '---\n\n';
case TIPTAP_NODE_TYPES.CODE_BLOCK:
return renderCodeBlock(node);
case TIPTAP_NODE_TYPES.TABLE:
return renderTable(node);
case TIPTAP_NODE_TYPES.FILE: {
const name = typeof node.attrs?.name === 'string' ? node.attrs.name : '';
const url = typeof node.attrs?.url === 'string' ? node.attrs.url : '';
return url === ''
? ''
: `[${escapeMarkdownText(name === '' ? url : name)}](${escapeMarkdownDestination(url)})\n\n`;
}
default:
return renderChildren(node);
}
};
export const tipTapDocumentToMarkdown = (
document: string | TipTapDocument,
): string => {
const parsedDocument =
typeof document === 'string' ? parseTipTapJsonDocument(document) : document;
if (parsedDocument === undefined) {
return typeof document === 'string' ? document : '';
}
return renderTipTapNodeToMarkdown(parsedDocument).trim();
};