Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ function tiptapJsonToEditorContent(json: JSONContent): EditorContent {
const traverse = (node: JSONContent) => {
if (node.type === "text" && node.text) {
segments.push({ type: "text", text: node.text });
} else if (node.type === "hardBreak") {
// Shift+Enter creates a hard break within a paragraph
// Use two trailing spaces + newline for markdown line break (<br>)
segments.push({ type: "text", text: " \n" });
} else if (node.type === "mentionChip" && node.attrs) {
segments.push({
type: "chip",
Expand All @@ -18,6 +22,15 @@ function tiptapJsonToEditorContent(json: JSONContent): EditorContent {
label: node.attrs.label,
},
});
} else if (node.type === "doc" && node.content) {
// Add double newlines between paragraphs for markdown rendering
// (single newlines in markdown become spaces, double newlines create paragraph breaks)
for (let i = 0; i < node.content.length; i++) {
if (i > 0) {
segments.push({ type: "text", text: "\n\n" });
}
traverse(node.content[i]);
}
} else if (node.content) {
for (const child of node.content) {
traverse(child);
Expand All @@ -30,13 +43,33 @@ function tiptapJsonToEditorContent(json: JSONContent): EditorContent {
}

function editorContentToTiptapJson(content: EditorContent): JSONContent {
const paragraphContent: JSONContent[] = [];
const paragraphs: JSONContent[] = [];
let currentParagraphContent: JSONContent[] = [];

const flushParagraph = () => {
paragraphs.push({ type: "paragraph", content: currentParagraphContent });
currentParagraphContent = [];
};

for (const seg of content.segments) {
if (seg.type === "text") {
paragraphContent.push({ type: "text", text: seg.text });
const paragraphParts = seg.text.split("\n\n");
for (let i = 0; i < paragraphParts.length; i++) {
if (i > 0) {
flushParagraph();
}
const lineParts = paragraphParts[i].split(/ {2}\n|\n/);
for (let j = 0; j < lineParts.length; j++) {
if (j > 0) {
currentParagraphContent.push({ type: "hardBreak" });
}
if (lineParts[j]) {
currentParagraphContent.push({ type: "text", text: lineParts[j] });
}
}
}
} else {
paragraphContent.push({
currentParagraphContent.push({
type: "mentionChip",
attrs: {
type: seg.chip.type,
Expand All @@ -47,9 +80,15 @@ function editorContentToTiptapJson(content: EditorContent): JSONContent {
}
}

flushParagraph();

if (paragraphs.length === 0) {
paragraphs.push({ type: "paragraph", content: [] });
}

return {
type: "doc",
content: [{ type: "paragraph", content: paragraphContent }],
content: paragraphs,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export function useTiptapEditor(options: UseTiptapEditorOptions) {

const textToInsert = paths.map((p) => `"${p}"`).join(" ");

view.dispatch(view.state.tr.insertText(textToInsert + " ", pos));
view.dispatch(view.state.tr.insertText(`${textToInsert} `, pos));
return true;
}

Expand Down