Skip to content

Commit

Permalink
feat(Wikilinks): Support smart links and pages in multiple spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
andymac4182 committed Apr 11, 2023
1 parent 57e9f65 commit 20656f4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.
4 changes: 0 additions & 4 deletions src/MarkdownTransformer/index.ts
Expand Up @@ -157,10 +157,6 @@ export class MarkdownTransformer implements Transformer<Markdown> {

tokenizer.use(wikilinksPlugin);

//if (schema.nodes.task) {
// tokenizer.use(taskLists);
//}

(["nodes", "marks"] as (keyof SchemaMapping)[]).forEach((key) => {
for (const idx in pmSchemaToMdMapping[key]) {
if (schema[key][idx]) {
Expand Down
3 changes: 0 additions & 3 deletions src/MarkdownTransformer/wikilinks.ts
Expand Up @@ -48,9 +48,6 @@ export function wikilinks(state: StateInline): boolean {

let token = state.push("link_open", "a", 1);
token.attrs = [["href", href]];
if (alias) {
token.attrs.push(["title", alias]);
}
state.md.inline.tokenize(state);
token = state.push("link_close", "a", -1);

Expand Down
45 changes: 34 additions & 11 deletions src/Publisher.ts
Expand Up @@ -37,6 +37,7 @@ export interface AdfFile {
tags: string[];
pageId: string | undefined;
dontChangeParentPageId: boolean;
spaceKey?: string;
}

interface ConfluenceNode {
Expand Down Expand Up @@ -119,14 +120,14 @@ export class Publisher {
);
const confluencePagesToPublish = flattenTree(confluencePageTree);

const fileToPageIdMap: Record<string, string> = {};
const fileToPageIdMap: Record<string, AdfFile> = {};

confluencePagesToPublish.forEach((node) => {
if (!node.file.pageId) {
throw new Error("Missing Page ID");
}

fileToPageIdMap[node.file.fileName] = node.file.pageId;
fileToPageIdMap[node.file.fileName] = node.file;
});

confluencePagesToPublish.forEach((node) => {
Expand All @@ -146,14 +147,22 @@ export class Publisher {
);
const pagename = `${wikilinkUrl.pathname}.md`;

const linkPageId = fileToPageIdMap[pagename];
const confluenceUrl = `${this.settings.confluenceBaseUrl}/wiki/spaces/~557058aea5688c52b94d15aabe96def1abc413/pages/${linkPageId}${wikilinkUrl.hash}`;
console.log({
pagename,
fileToPageIdMap,
wikiurl: confluenceUrl,
});
node.marks[0].attrs.href = confluenceUrl;
const linkPage = fileToPageIdMap[pagename];
if (linkPage) {
const confluenceUrl = `${this.settings.confluenceBaseUrl}/wiki/spaces/${linkPage.spaceKey}/pages/${linkPage.pageId}${wikilinkUrl.hash}`;
node.marks[0].attrs.href = confluenceUrl;
if (node.text === wikilinkUrl.pathname) {
node.type = "inlineCard";
node.attrs = {
url: node.marks[0].attrs.href,
};
delete node.marks;
delete node.text;
return node;
}
} else {
node.marks.remove(node.marks[0]);
}
return node;
}
}
Expand Down Expand Up @@ -210,10 +219,12 @@ export class Publisher {
topPageId
);
node.file.pageId = pageDetails.id;
node.file.spaceKey = pageDetails.spaceKey;
version = pageDetails.version;
existingAdf = pageDetails.existingAdf;
} else {
node.file.pageId = parentPageId;
node.file.spaceKey = spaceKey;
version = 0;
existingAdf = "";
}
Expand Down Expand Up @@ -254,14 +265,24 @@ export class Publisher {
const contentById =
await this.confluenceClient.content.getContentById({
id: file.pageId,
expand: ["version", "body.atlas_doc_format", "ancestors"],
expand: [
"version",
"body.atlas_doc_format",
"ancestors",
"space",
],
});

if (!contentById.space?.key) {
throw new Error("Missing Space Key");
}

return {
id: contentById.id,
title: file.pageTitle,
version: contentById?.version?.number ?? 1,
existingAdf: contentById?.body?.atlas_doc_format?.value,
spaceKey: contentById.space.key,
};
}

Expand Down Expand Up @@ -299,6 +320,7 @@ export class Publisher {
title: file.pageTitle,
version: currentPage?.version?.number ?? 1,
existingAdf: currentPage?.body?.atlas_doc_format?.value,
spaceKey,
};
} else {
console.log("Creating page");
Expand Down Expand Up @@ -329,6 +351,7 @@ export class Publisher {
title: file.pageTitle,
version: pageDetails?.version?.number ?? 1,
existingAdf: pageDetails?.body?.atlas_doc_format?.value,
spaceKey,
};
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/mdToADF.ts
Expand Up @@ -33,10 +33,7 @@ export default class MdToADF {
node.marks[0].type === "link" &&
node.marks[0].attrs
) {
if (
!node.marks[0].attrs.title &&
node.marks[0].attrs.href === node.text
) {
if (node.marks[0].attrs.href === node.text) {
node.type = "inlineCard";
node.attrs = { url: node.marks[0].attrs.href };
delete node.marks;
Expand Down

0 comments on commit 20656f4

Please sign in to comment.