Skip to content

Commit 8debaca

Browse files
authored
add key image support if discourse template is uses smartblocks (#289)
1 parent b846abe commit 8debaca

1 file changed

Lines changed: 48 additions & 43 deletions

File tree

src/utils/createDiscourseNode.ts

Lines changed: 48 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,52 @@ const createDiscourseNode = async ({
4747
}, 1);
4848
}, 100);
4949
};
50+
const handleImageCreation = async (pageUid: string) => {
51+
const canvasSettings = Object.fromEntries(
52+
discourseNodes.map((n) => [n.type, { ...n.canvasSettings }])
53+
);
54+
const {
55+
"query-builder-alias": qbAlias = "",
56+
"key-image": isKeyImage = "",
57+
"key-image-option": keyImageOption = "",
58+
} = canvasSettings[configPageUid] || {};
59+
60+
if (isKeyImage && imageUrl) {
61+
const createOrUpdateImageBlock = async (imagePlaceholderUid?: string) => {
62+
const imageMarkdown = `![](${imageUrl})`;
63+
if (imagePlaceholderUid) {
64+
await updateBlock({
65+
uid: imagePlaceholderUid,
66+
text: imageMarkdown,
67+
});
68+
} else {
69+
await createBlock({
70+
node: { text: imageMarkdown },
71+
order: 0,
72+
parentUid: pageUid,
73+
});
74+
}
75+
};
76+
77+
if (keyImageOption === "query-builder") {
78+
if (!extensionAPI) return;
79+
80+
const parentUid = resolveQueryBuilderRef({
81+
queryRef: qbAlias,
82+
extensionAPI,
83+
});
84+
const results = await runQuery({
85+
extensionAPI,
86+
parentUid,
87+
inputs: { NODETEXT: text, NODEUID: pageUid },
88+
});
89+
const imagePlaceholderUid = results.allProcessedResults[0]?.uid;
90+
await createOrUpdateImageBlock(imagePlaceholderUid);
91+
} else {
92+
await createOrUpdateImageBlock();
93+
}
94+
}
95+
};
5096

5197
const discourseNodes = getDiscourseNodes();
5298
const specification = discourseNodes?.find(
@@ -95,49 +141,7 @@ const createDiscourseNode = async ({
95141
);
96142

97143
// Add image to page if imageUrl is provided
98-
const createOrUpdateImageBlock = async (imagePlaceholderUid?: string) => {
99-
const imageMarkdown = `![](${imageUrl})`;
100-
if (imagePlaceholderUid) {
101-
await updateBlock({
102-
uid: imagePlaceholderUid,
103-
text: imageMarkdown,
104-
});
105-
} else {
106-
await createBlock({
107-
node: { text: imageMarkdown },
108-
order: 0,
109-
parentUid: pageUid,
110-
});
111-
}
112-
};
113-
const canvasSettings = Object.fromEntries(
114-
discourseNodes.map((n) => [n.type, { ...n.canvasSettings }])
115-
);
116-
const {
117-
"query-builder-alias": qbAlias = "",
118-
"key-image": isKeyImage = "",
119-
"key-image-option": keyImageOption = "",
120-
} = canvasSettings[configPageUid] || {};
121-
122-
if (isKeyImage && imageUrl) {
123-
if (keyImageOption === "query-builder") {
124-
if (!extensionAPI) return;
125-
126-
const parentUid = resolveQueryBuilderRef({
127-
queryRef: qbAlias,
128-
extensionAPI,
129-
});
130-
const results = await runQuery({
131-
extensionAPI,
132-
parentUid,
133-
inputs: { NODETEXT: text, NODEUID: pageUid },
134-
});
135-
const imagePlaceholderUid = results.allProcessedResults[0]?.uid;
136-
await createOrUpdateImageBlock(imagePlaceholderUid);
137-
} else if (imageUrl) {
138-
await createOrUpdateImageBlock();
139-
}
140-
}
144+
await handleImageCreation(pageUid);
141145
};
142146

143147
const hasSmartBlockSyntax = (node: RoamBasicNode) => {
@@ -160,6 +164,7 @@ const createDiscourseNode = async ({
160164
srcUid: templateNode.uid,
161165
targetUid: pageUid,
162166
});
167+
await handleImageCreation(pageUid);
163168
} else {
164169
await createBlocksFromTemplate();
165170
}

0 commit comments

Comments
 (0)