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
@@ -1,7 +1,7 @@
import { contentTypes } from "@repo/content-model";
import type { TreeNode } from "roamjs-components/types";
import type { CrossAppNode } from "@repo/database/crossAppContracts";
import { buildFullMarkdown } from "./convertRoamNodeToFullContent";
import { buildFullMarkdown } from "./roamToCrossAppConverters";

/**
* Example Roam page tree used to show the markdown emitted for a `full` content
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { TreeNode } from "roamjs-components/types";
import type { CrossAppNode } from "@repo/database/crossAppContracts";
import { buildFullMarkdown } from "./convertRoamNodeToFullContent";
import { buildFullMarkdown } from "./roamToCrossAppConverters";
import { contentTypes } from "@repo/content-model";

const block = (text: string, children: TreeNode[] = []): TreeNode => ({
Expand Down
60 changes: 2 additions & 58 deletions apps/roam/src/utils/convertRoamNodeToFullContent.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { toMarkdown } from "./pageToMarkdown";
import { type DiscourseNode } from "./getDiscourseNodes";
import getFullTreeByParentUid from "roamjs-components/queries/getFullTreeByParentUid";
import getPageViewType from "roamjs-components/queries/getPageViewType";
import type { TreeNode, ViewType } from "roamjs-components/types";
import { contentTypes } from "@repo/content-model";
import type { CrossAppNode } from "@repo/database/crossAppContracts";
import { crossAppNodeToDbContent } from "@repo/database/lib/crossAppConverters";
import { fullContentNodeToCrossApp } from "./roamToCrossAppConverters";
import type { LocalContentDataInput } from "@repo/database/inputTypes";

export type RoamFullContentNode = {
Expand All @@ -18,64 +12,14 @@ export type RoamFullContentNode = {
node_title?: string;
};

const FULL_MARKDOWN_OPTS = {
refs: true,
embeds: true,
simplifiedFilename: false,
removeSpecialCharacters: false,
maxFilenameLength: 64,
linkType: "alias",
allNodes: [] as DiscourseNode[],
};

export const buildFullMarkdown = ({
title,
blocks,
viewType = "bullet",
}: {
title: string;
blocks: TreeNode[];
viewType?: ViewType;
}): string => {
const body = blocks
.filter((block) => !!block.text || !!block.children?.length)
.map((block) =>
toMarkdown({ c: block, v: viewType, i: 0, opts: FULL_MARKDOWN_OPTS }),
)
.join("\n")
.trim();
return body ? `# ${title}\n\n${body}\n` : `# ${title}\n`;
};

export const convertRoamNodeToFullContent = ({
nodes,
}: {
nodes: RoamFullContentNode[];
}): LocalContentDataInput[] =>
nodes.flatMap((node) => {
try {
const title = node.node_title ?? node.text;
const blocks = getFullTreeByParentUid(node.source_local_id).children;
const viewType = getPageViewType(title) || "bullet";
const crossAppNode: CrossAppNode = {
authorId: node.author_local_id,
localId: node.source_local_id,
createdAt: new Date(node.created || Date.now()),
modifiedAt: new Date(node.last_modified || Date.now()),
nodeType: node.node_type_id,
content: {
direct: {
localId: node.source_local_id,
value: title,
},
full: {
localId: node.source_local_id,
value: buildFullMarkdown({ title, blocks, viewType }),
contentType: contentTypes.roamMarkdown,
scale: "document",
},
},
};
const crossAppNode = fullContentNodeToCrossApp(node);
const fullContent = crossAppNodeToDbContent(crossAppNode, "full");
return fullContent === undefined ? [] : [fullContent];
} catch (error) {
Expand Down
66 changes: 66 additions & 0 deletions apps/roam/src/utils/roamToCrossAppConverters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import type { CrossAppNode } from "@repo/database/crossAppContracts";
import type { RoamFullContentNode } from "./convertRoamNodeToFullContent";
import type { DiscourseNode } from "./getDiscourseNodes";
import type { TreeNode, ViewType } from "roamjs-components/types";
import { toMarkdown } from "./pageToMarkdown";
import getFullTreeByParentUid from "roamjs-components/queries/getFullTreeByParentUid";
import getPageViewType from "roamjs-components/queries/getPageViewType";
import { contentTypes } from "@repo/content-model";

const FULL_MARKDOWN_OPTS = {
refs: true,
embeds: true,
simplifiedFilename: false,
removeSpecialCharacters: false,
maxFilenameLength: 64,
linkType: "alias",
allNodes: [] as DiscourseNode[],
};

export const buildFullMarkdown = ({
title,
blocks,
viewType = "bullet",
}: {
title: string;
blocks: TreeNode[];
viewType?: ViewType;
}): string => {
const body = blocks
.filter((block) => !!block.text || !!block.children?.length)
.map((block) =>
toMarkdown({ c: block, v: viewType, i: 0, opts: FULL_MARKDOWN_OPTS }),
)
.join("\n")
.trim();
return body ? `# ${title}\n\n${body}\n` : `# ${title}\n`;
};

export const fullContentNodeToCrossApp = (
node: RoamFullContentNode,
): CrossAppNode => {
const title = node.node_title ?? node.text;
const blocks = getFullTreeByParentUid(node.source_local_id).children;
const viewType = getPageViewType(title) || "bullet";
const fullText = buildFullMarkdown({ title, blocks, viewType });

return {
authorId: node.author_local_id,
localId: node.source_local_id,
createdAt: new Date(node.created || Date.now()),
modifiedAt: new Date(node.last_modified || Date.now()),
nodeType: node.node_type_id,
content: {
direct: {
localId: node.source_local_id,
value: node.node_title ?? node.text,
},
full: {
localId: node.source_local_id,
value: fullText,
contentType: contentTypes.roamMarkdown,
scale: "document",
},
},
};
};