Skip to content

Commit

Permalink
Export: Add URL link type (#247)
Browse files Browse the repository at this point in the history
* refactor frontmatter to function, escape title

* add "roam url" link type
  • Loading branch information
mdroidian committed Apr 9, 2024
1 parent 2d7eb25 commit 1eb82b9
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 40 deletions.
5 changes: 3 additions & 2 deletions src/discourseGraphsMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -607,9 +607,10 @@ const initializeDiscourseGraphsMode = async (args: OnloadArgs) => {
{
title: "link type",
Panel: SelectPanel,
description: "How to format links that appear in your export",
description:
"How to format links that appear in your export.",
options: {
items: ["alias", "wikilinks"],
items: ["alias", "wikilinks", "roam url"],
},
} as Field<SelectField>,
{
Expand Down
105 changes: 67 additions & 38 deletions src/utils/getExportTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,13 @@ const collectUids = (t: TreeNode): string[] => [
const MATCHES_NONE = /$.+^/;
const EMBED_REGEX = /{{(?:\[\[)embed(?:\]\]):\s*\(\(([\w\d-]{9,10})\)\)\s*}}/;

const toLink = (s: string, linkType: string) => {
if (linkType === "wikilinks") return `[[${s.replace(/\.md$/, "")}]]`;
if (linkType === "alias") return `[${s}](${s})`;
return s;
const toLink = (filename: string, uid: string, linkType: string) => {
const extensionRemoved = filename.replace(/\.\w+$/, "");
if (linkType === "wikilinks") return `[[${extensionRemoved}]]`;
if (linkType === "alias") return `[${filename}](${filename})`;
if (linkType === "roam url")
return `[${extensionRemoved}](https://roamresearch.com/#/app/${window.roamAlphaAPI.graph.name}/page/${uid})`;
return filename;
};

const toMarkdown = ({
Expand Down Expand Up @@ -193,7 +196,7 @@ const toMarkdown = ({
simplifiedFilename,
removeSpecialCharacters,
});
return toLink(name, linkType);
return toLink(name, c.uid, linkType);
} else if (s.name === "left" || s.name === "right") {
return "";
} else {
Expand Down Expand Up @@ -261,6 +264,41 @@ const handleDiscourseContext = async ({
return discourseResults;
};

const handleFrontmatter = ({
frontmatter,
rest,
result,
}: {
frontmatter: string[];
rest: Record<string, unknown>;
result: Result;
}) => {
const yaml = frontmatter.length
? frontmatter
: [
"title: {text}",
`url: https://roamresearch.com/#/app/${window.roamAlphaAPI.graph.name}/page/{uid}`,
`author: {author}`,
"date: {date}",
];
const resultCols = Object.keys(rest).filter((k) => !k.includes("uid"));
const yamlLines = yaml.concat(resultCols.map((k) => `${k}: {${k}}`));
const content = yamlLines
.map((s) =>
s.replace(/{([^}]+)}/g, (_, capt: string) => {
if (capt === "text") {
// Wrap title in quotes and escape additional quotes
const escapedText = result[capt].toString().replace(/"/g, '\\"');
return `"${escapedText}"`;
}
return result[capt].toString();
})
)
.join("\n");
const output = `---\n${content}\n---`;
return output;
};

type getExportTypesProps = {
results?: ExportDialogProps["results"];
exportId: string;
Expand Down Expand Up @@ -436,16 +474,8 @@ const getExportTypes = ({
tree: exportTree.children,
key: "append referenced node",
}).uid;
const yaml = frontmatter.length
? frontmatter
: [
"title: {text}",
`url: https://roamresearch.com/#/app/${window.roamAlphaAPI.graph.name}/page/{uid}`,
`author: {author}`,
"date: {date}",
];
return {
yaml,
frontmatter,
optsRefs,
optsEmbeds,
simplifiedFilename,
Expand All @@ -464,7 +494,7 @@ const getExportTypes = ({
includeDiscourseContext = false,
}) => {
const {
yaml,
frontmatter,
optsRefs,
optsEmbeds,
simplifiedFilename,
Expand All @@ -485,20 +515,6 @@ const getExportTypes = ({
await new Promise((resolve) => setTimeout(resolve));
const v = getPageViewType(text) || "bullet";
const { date, displayName } = getPageMetadata(text);
const resultCols = Object.keys(rest).filter(
(k) => !k.includes("uid")
);
const yamlLines = yaml.concat(
resultCols.map((k) => `${k}: {${k}}`)
);
const result: Result = {
...rest,
date,
text,
uid,
author: displayName,
type,
};
const treeNode = getFullTreeByParentUid(uid);

const discourseResults = await handleDiscourseContext({
Expand All @@ -521,13 +537,22 @@ const getExportTypes = ({
Array.isArray(children) && children.length
)
: [];
const content = `---\n${yamlLines
.map((s) =>
s.replace(/{([^}]+)}/g, (_, capt: string) =>
result[capt].toString()
)
)
.join("\n")}\n---\n\n${treeNode.children

const result: Result = {
...rest,
date,
text,
uid,
author: displayName,
type,
};
const yamlLines = handleFrontmatter({
frontmatter,
rest,
result,
});

const content = `${yamlLines}\n\n${treeNode.children
.map((c) =>
toMarkdown({
c,
Expand Down Expand Up @@ -558,6 +583,7 @@ const getExportTypes = ({
allNodes,
removeSpecialCharacters,
}),
t.uid,
linkType
)}`
)
Expand All @@ -577,6 +603,7 @@ const getExportTypes = ({
allNodes,
removeSpecialCharacters,
}),
r_1[0][":block/uid"] || "",
linkType
)}\n\n${toMarkdown({
c: pullBlockToTreeNode(r_1[1], ":bullet"),
Expand Down Expand Up @@ -760,7 +787,8 @@ const getExportTypes = ({
allNodes,
removeSpecialCharacters,
});
const link = toLink(filename, linkType);
const uid = t.uid || "";
const link = toLink(filename, uid, linkType);
return `**${r.label}::** ${link}`;
})
)
Expand Down Expand Up @@ -800,7 +828,8 @@ const getExportTypes = ({
allNodes,
removeSpecialCharacters,
});
const link = toLink(filename, linkType);
const uid = r[0][":block/uid"] || "";
const link = toLink(filename, uid, linkType);
const node = treeNodeToMarkdown(
pullBlockToTreeNode(r[1], ":bullet")
);
Expand Down

0 comments on commit 1eb82b9

Please sign in to comment.