-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathconvertRoamNodeToFullContent.ts
More file actions
68 lines (65 loc) · 2.07 KB
/
Copy pathconvertRoamNodeToFullContent.ts
File metadata and controls
68 lines (65 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { toMarkdown } from "./pageToMarkdown";
import { type RoamDiscourseNodeData } from "./getAllDiscourseNodesSince";
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 type { LocalContentDataInput } from "@repo/database/inputTypes";
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: RoamDiscourseNodeData[];
}): 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";
return [
{
author_local_id: node.author_local_id,
source_local_id: node.source_local_id,
created: new Date(node.created || Date.now()).toISOString(),
last_modified: new Date(
node.last_modified || Date.now(),
).toISOString(),
text: buildFullMarkdown({ title, blocks, viewType }),
variant: "full",
scale: "document",
},
];
} catch (error) {
console.error(
`convertRoamNodeToFullContent: failed to build full markdown for ${node.source_local_id}:`,
error,
);
return [];
}
});